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



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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'ext/extzstd_stream.c', line 95

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);
    }

    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);
    }

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

    if (extzstd_params_p(params)) {
        ZSTD_parameters *paramsp = extzstd_getparams(params);
        ZSTD_CCtx *zstd = p->context;
        p->context = NULL; // 

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'ext/extzstd_stream.c', line 234

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?



257
258
259
260
261
# File 'ext/extzstd_stream.c', line 257

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

#reset(pledged_srcsize) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'ext/extzstd_stream.c', line 263

static VALUE
enc_reset(VALUE self, VALUE pledged_srcsize)
{
    /*
     * ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset);
     */

    size_t s = ZSTD_CCtx_reset(encoder_context(self)->context, ZSTD_reset_session_only);
    extzstd_check_error(s);

    if (pledged_srcsize == Qnil) {
        ZSTD_CCtx_setPledgedSrcSize(encoder_context(self)->context, ZSTD_CONTENTSIZE_UNKNOWN);
    } else {
        ZSTD_CCtx_setPledgedSrcSize(encoder_context(self)->context, NUM2ULL(pledged_srcsize));
    }

    return self;
}

#sizeofObject



282
283
284
285
286
287
288
289
290
291
292
# File 'ext/extzstd_stream.c', line 282

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



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 213

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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/extzstd_stream.c', line 184

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) {
        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: