Class: Archive::Zip::Codec::Store::Reader

Inherits:
IO::LikeHelpers::DelegatedIO
  • Object
show all
Defined in:
lib/archive/zip/codec/store/reader.rb

Overview

Archive::Zip::Codec::Store#decompressor method.

Instance Method Summary collapse

Constructor Details

#initialize(delegate, autoclose: true) ⇒ Reader

Creates a new instance of this class using io as a data source. io must be readable and provide a read method as an IO instance would or errors will be raised when performing read operations.

This class has extremely limited seek capabilities. It is possible to seek with an offset of 0 and a whence of IO::SEEK_CUR. As a result, the pos and tell methods also work as expected.

Due to certain optimizations within IO::Like#seek and if there is data in the read buffer, the seek method can be used to seek forward from the current stream position up to the end of the buffer. Unless it is known definitively how much data is in the buffer, it is best to avoid relying on this behavior.

If io also responds to rewind, then the rewind method of this class can be used to reset the whole stream back to the beginning. Using seek of this class to seek directly to offset 0 using IO::SEEK_SET for whence will also work in this case.

Any other seeking attempts, will raise Errno::EINVAL exceptions.



40
41
42
43
44
# File 'lib/archive/zip/codec/store/reader.rb', line 40

def initialize(delegate, autoclose: true)
  super
  @crc32 = 0
  @uncompressed_size = 0
end

Instance Method Details

#data_descriptorObject

Returns an instance of Archive::Zip::DataDescriptor with information regarding the data which has passed through this object from the delegate object. It is recommended to call the close method before calling this in order to ensure that no further read operations change the state of this object.



51
52
53
54
55
56
57
# File 'lib/archive/zip/codec/store/reader.rb', line 51

def data_descriptor
  DataDescriptor.new(
    @crc32,
    @uncompressed_size,
    @uncompressed_size
  )
end

#read(length, buffer: nil, buffer_offset: 0) ⇒ Object

Returns at most length bytes from the delegate object. Updates the uncompressed_size and crc32 attributes as a side effect.



61
62
63
64
65
66
67
68
69
70
# File 'lib/archive/zip/codec/store/reader.rb', line 61

def read(length, buffer: nil, buffer_offset: 0)
  result = super
  return result if Symbol === result

  buffer = result if buffer.nil?
  @uncompressed_size += buffer.bytesize
  @crc32 = Zlib.crc32(buffer, @crc32)

  result
end

#seek(amount, whence = IO::SEEK_SET) ⇒ Object

Allows resetting this object and the delegate object back to the beginning of the stream or reporting the current position in the stream.

Raises Errno::EINVAL unless offset is 0 and whence is either IO::SEEK_SET or IO::SEEK_CUR. Raises Errno::EINVAL if whence is IO::SEEK_SEK and the delegate object does not respond to the rewind method.

Raises:

  • (Errno::ESPIPE)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/archive/zip/codec/store/reader.rb', line 79

def seek(amount, whence = IO::SEEK_SET)
  assert_open
  raise Errno::ESPIPE if amount != 0 || whence == IO::SEEK_END

  case whence
  when IO::SEEK_SET
    result = super
    return result if Symbol === result
    @crc32 = 0
    @uncompressed_size = 0
    result
  when IO::SEEK_CUR
    @uncompressed_size
  else
    raise Errno::EINVAL
  end
end