Class: Zstd::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/extzstd.rb,
ext/extzstd_stream.c

Constant Summary collapse

INSIZE =
SIZET2NUM(ZSTD_CStreamInSize())
OUTSIZE =
SIZET2NUM(ZSTD_CStreamOutSize())

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(outport, compression_parameters = nil, predict = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'ext/extzstd_stream.c', line 94

static VALUE
enc_init(int argc, VALUE argv[], VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
     * ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
     * ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
     *                                              ZSTD_parameters params, unsigned long long pledgedSrcSize);
     */

    VALUE outport, params, predict;
    switch (argc) {
    case 1:
        outport = argv[0];
        params = predict = Qnil;
        break;
    case 2:
        outport = argv[0];
        params = argv[1];
        predict = Qnil;
        break;
    case 3:
        outport = argv[0];
        params = argv[1];
        predict = argv[2];
        break;
    default:
        rb_error_arity(argc, 1, 3);
    }

    struct encoder *p = getencoder(self);
    if (p->context) {
        rb_raise(rb_eTypeError,
                "initialized already - #<%s:%p>",
                rb_obj_classname(self), (void *)self);
    }

    AUX_TRY_WITH_GC(
            p->context = ZSTD_createCStream(),
            "failed ZSTD_createCStream()");

    const void *predictp;
    size_t predictsize;
    if (NIL_P(predict)) {
        predictp = NULL;
        predictsize = 0;
    } else {
        rb_check_type(predict, RUBY_T_STRING);
        predict = rb_str_new_frozen(predict);
        RSTRING_GETMEM(predict, predictp, predictsize);
    }

    if (extzstd_params_p(params)) {
        ZSTD_parameters *paramsp = extzstd_getparams(params);
        size_t s = ZSTD_initCStream_advanced(p->context, predictp, predictsize, *paramsp, -1);
        extzstd_check_error(s);
    } else {
        size_t s = ZSTD_initCStream_usingDict(p->context, predictp, predictsize, aux_num2int(params, 1));
        extzstd_check_error(s);
    }

    p->predict = predict;
    p->outport = outport;

    return self;
}

Class Method Details

.encode(src, params = nil, dest: nil, dict: nil) ⇒ Object Also known as: compress



102
103
104
# File 'lib/extzstd.rb', line 102

def self.encode(src, params = nil, dest: nil, dict: nil)
  ContextLess.encode(src, dest || "".b, nil, dict, params)
end

.open(outport, *args) ⇒ Object

call-seq:

open(outport, level = nil, dict = nil) -> zstd encoder
open(outport, encode_params, dict = nil) { |encoder| ... } -> yield returned value


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/extzstd.rb', line 90

def self.open(outport, *args)
  e = new(outport, *args)

  return e unless block_given?

  begin
    yield e
  ensure
    e.close unless e.eof?
  end
end

Instance Method Details

#closeObject Also known as: end, finish



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'ext/extzstd_stream.c', line 211

static VALUE
enc_close(VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
     */

    struct encoder *p = encoder_context(self);
    aux_str_buf_recycle(p->destbuf, ZSTD_CStreamOutSize());
    rb_str_set_len(p->destbuf, 0);
    rb_obj_infect(p->destbuf, self);
    ZSTD_outBuffer output = { RSTRING_PTR(p->destbuf), rb_str_capacity(p->destbuf), 0 };
    size_t s = ZSTD_endStream(p->context, &output);
    extzstd_check_error(s);
    rb_str_set_len(p->destbuf, output.pos);

    AUX_FUNCALL(p->outport, id_op_lsh, p->destbuf);

    p->reached_eof = 1;

    return Qnil;
}

#eofObject Also known as: eof?



234
235
236
237
238
# File 'ext/extzstd_stream.c', line 234

static VALUE
enc_eof(VALUE self)
{
    return (encoder_context(self)->reached_eof == 0 ? Qfalse : Qtrue);
}

#reset(pledged_srcsize) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
# File 'ext/extzstd_stream.c', line 240

static VALUE
enc_reset(VALUE self, VALUE pledged_srcsize)
{
    /*
     * ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
     */

    size_t s = ZSTD_resetCStream(encoder_context(self)->context, NUM2ULL(pledged_srcsize));
    extzstd_check_error(s);
    return self;
}

#sizeofObject



252
253
254
255
256
257
258
259
260
261
262
# File 'ext/extzstd_stream.c', line 252

static VALUE
enc_sizeof(VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
     */

    size_t s = ZSTD_sizeof_CStream(encoder_context(self)->context);
    extzstd_check_error(s);
    return SIZET2NUM(s);
}

#syncObject Also known as: flush



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'ext/extzstd_stream.c', line 190

static VALUE
enc_sync(VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
     */

    struct encoder *p = encoder_context(self);
    aux_str_buf_recycle(p->destbuf, ZSTD_CStreamOutSize());
    rb_str_set_len(p->destbuf, 0);
    rb_obj_infect(p->destbuf, self);
    ZSTD_outBuffer output = { RSTRING_PTR(p->destbuf), rb_str_capacity(p->destbuf), 0 };
    size_t s = ZSTD_flushStream(p->context, &output);
    extzstd_check_error(s);
    rb_str_set_len(p->destbuf, output.pos);

    AUX_FUNCALL(p->outport, id_op_lsh, p->destbuf);

    return self;
}

#write(src) ⇒ Object Also known as: <<, update



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'ext/extzstd_stream.c', line 161

static VALUE
enc_write(VALUE self, VALUE src)
{
    /*
     * ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
     */

    struct encoder *p = encoder_context(self);
    src = rb_String(src);
    ZSTD_inBuffer input = { RSTRING_PTR(src), RSTRING_LEN(src), 0 };

    while (input.pos < input.size) {
        p->destbuf = aux_str_buf_recycle(p->destbuf, ZSTD_CStreamOutSize() * 2);
        rb_str_set_len(p->destbuf, 0);
        rb_obj_infect(self, src);
        rb_obj_infect(p->destbuf, self);
        ZSTD_outBuffer output = { RSTRING_PTR(p->destbuf), rb_str_capacity(p->destbuf), 0 };
        size_t s = ZSTD_compressStream(p->context, &output, &input);
        extzstd_check_error(s);
        rb_str_set_len(p->destbuf, output.pos);

        // TODO: