Class: Zstd::Decoder

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

Constant Summary collapse

STATUS_CLOSED =
nil
STATUS_READY =
0
STATUS_INPORT_EOF =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inport, dict = nil) ⇒ Decoder

Returns a new instance of Decoder.

Raises:



162
163
164
165
166
167
168
169
170
# File 'lib/extzstd.rb', line 162

def initialize(inport, dict = nil)
  raise Error, "require .read method - <%s:0x%08x>" % [inport.class, inport.object_id << 1] unless inport.respond_to?(:read)
  @decoder = StreamDecoder.new(dict)
  @inport = inport
  @readbuf = StringIO.new(Aux::EMPTY_BUFFER.dup)
  @destbuf = StringIO.new(Aux::EMPTY_BUFFER.dup)
  @status = STATUS_READY
  @pos = 0
end

Instance Attribute Details

#decoderObject (readonly)

Returns the value of attribute decoder.



134
135
136
# File 'lib/extzstd.rb', line 134

def decoder
  @decoder
end

#destbufObject (readonly)

Returns the value of attribute destbuf.



134
135
136
# File 'lib/extzstd.rb', line 134

def destbuf
  @destbuf
end

#inportObject (readonly)

Returns the value of attribute inport.



134
135
136
# File 'lib/extzstd.rb', line 134

def inport
  @inport
end

#posObject (readonly)

Returns the value of attribute pos.



134
135
136
# File 'lib/extzstd.rb', line 134

def pos
  @pos
end

#readbufObject (readonly)

Returns the value of attribute readbuf.



134
135
136
# File 'lib/extzstd.rb', line 134

def readbuf
  @readbuf
end

#statusObject (readonly)

Returns the value of attribute status.



134
135
136
# File 'lib/extzstd.rb', line 134

def status
  @status
end

Class Method Details

.open(inport, dict = nil) ⇒ Object

call-seq:

open(inport, dict = nil) -> decoder
open(inport, dict = nil) { |decoder| ... } -> yield returned value
inport

String instance or read method haved Object.



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/extzstd.rb', line 148

def self.open(inport, dict = nil)
  inport = StringIO.new(inport) if inport.kind_of?(String)

  dec = new(inport, dict)

  return dec unless block_given?

  begin
    yield(dec)
  ensure
    dec.close rescue nil
  end
end

Instance Method Details

#closeObject



172
173
174
175
176
177
178
# File 'lib/extzstd.rb', line 172

def close
  inport.close rescue nil if inport.respond_to?(:close)
  readbuf.truncate 0
  destbuf.truncate 0
  @status = STATUS_CLOSED
  nil
end

#eofObject Also known as: eof?



180
181
182
# File 'lib/extzstd.rb', line 180

def eof
  !status
end

#read(size = nil, dest = Aux::EMPTY_BUFFER.dup) ⇒ Object



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
212
213
# File 'lib/extzstd.rb', line 186

def read(size = nil, dest = Aux::EMPTY_BUFFER.dup)
  dest ||= Aux::EMPTY_BUFFER.dup
  size &&= size.to_i
  Aux.change_binary(dest) do
    #dest.clear
    dest[0 .. -1] = Aux::EMPTY_BUFFER unless dest.empty? # keep allocated heap
    return dest unless !size || size > 0

    d = Aux::EMPTY_BUFFER.dup
    until size && size <= 0
      if destbuf.eof?
        break unless fetch
      end

      destbuf.read(size, d)
      dest << d

      size -= d.bytesize if size
    end
  end

  if dest.empty?
    nil
  else
    @pos += dest.bytesize
    dest
  end
end