Class: Zstd::Encoder

Inherits:
Struct
  • Object
show all
Defined in:
lib/extzstd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(outport, params = nil, dict = nil) ⇒ Encoder

call-seq:

initialize(outport, level = nil, dict = nil)
initialize(outport, encode_params, dict = nil)

outport need has .<< method.



87
88
89
90
# File 'lib/extzstd.rb', line 87

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



62
63
64
# File 'lib/extzstd.rb', line 62

def destbuf
  @destbuf
end

#encoderObject

Returns the value of attribute encoder

Returns:

  • (Object)

    the current value of encoder



62
63
64
# File 'lib/extzstd.rb', line 62

def encoder
  @encoder
end

#outportObject

Returns the value of attribute outport

Returns:

  • (Object)

    the current value of outport



62
63
64
# File 'lib/extzstd.rb', line 62

def outport
  @outport
end

#statusObject

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



62
63
64
# File 'lib/extzstd.rb', line 62

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


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

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



98
99
100
101
102
103
104
# File 'lib/extzstd.rb', line 98

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

#eofObject Also known as: eof?



92
93
94
# File 'lib/extzstd.rb', line 92

def eof
  !status[0]
end

#flushObject

Raises:

  • (IOError)


122
123
124
125
126
127
128
129
130
# File 'lib/extzstd.rb', line 122

def flush
  raise IOError, "closed stream" if eof?

  off = 0
  encoder.flush(destbuf, StreamEncoder::OUTSIZE)
  outport << destbuf

  self
end

#write(buf) ⇒ Object Also known as: <<

Raises:

  • (IOError)


106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/extzstd.rb', line 106

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