Class: Zstd::Encoder

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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



108
109
110
111
# File 'lib/extzstd.rb', line 108

def initialize(outport, params = nil, dict = nil)
  encoder = StreamEncoder.new(params, dict)
  super encoder, outport, "".force_encoding(Encoding::BINARY), [true]
end

Instance Attribute Details

#destbufObject

Returns the value of attribute destbuf

Returns:

  • (Object)

    the current value of destbuf



83
84
85
# File 'lib/extzstd.rb', line 83

def destbuf
  @destbuf
end

#encoderObject

Returns the value of attribute encoder

Returns:

  • (Object)

    the current value of encoder



83
84
85
# File 'lib/extzstd.rb', line 83

def encoder
  @encoder
end

#outportObject

Returns the value of attribute outport

Returns:

  • (Object)

    the current value of outport



83
84
85
# File 'lib/extzstd.rb', line 83

def outport
  @outport
end

#statusObject

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



83
84
85
# File 'lib/extzstd.rb', line 83

def status
  @status
end

Class Method Details

.open(outport, *args) ⇒ Object

call-seq:

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


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/extzstd.rb', line 70

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

  return e unless block_given?

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

Instance Method Details

#closeObject Also known as: end, finish



211
212
213
214
215
216
217
# File 'ext/extzstd_stream.c', line 211

def close
  return nil if eof?
  encoder.end(destbuf, StreamEncoder::OUTSIZE)
  outport << destbuf
  status[0] = false
  nil
end

#eofObject Also known as: eof?



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

def eof
  !status[0]
end

#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

Raises:

  • (IOError)


161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/extzstd_stream.c', line 161

def write(buf)
  raise IOError, "closed stream" if eof?

  off = 0
  rest = buf.bytesize
  outsize = StreamEncoder::OUTSIZE
  while off && off < rest
    off = encoder.update(buf, off, destbuf, outsize)
    outport << destbuf
  end

  self
end