class Archive::Zip::Codec::Store::Compress
Archive::Zip::Codec::Store::Compress is simply a writable, IO-like wrapper around a writable, IO-like object which provides a CRC32 checksum of the data written through it as well as the count of the total amount of data. A close method is also provided which can optionally close the delegate object. In addition a convenience method is provided for generating DataDescriptor objects based on the data which is passed through this object.
Instances of this class should only be accessed via the Archive::Zip::Codec::Store#compressor method.
Public Class Methods
Creates a new instance of this class using io as a data sink. io must be writable and must provide a write method as IO does or errors will be raised when performing write operations.
The flush_size attribute is set to 0
by default under
the assumption that io is already buffered.
# File lib/archive/zip/codec/store.rb, line 46 def initialize(io) @io = io @crc32 = 0 @uncompressed_size = 0 # Assume that the delegate IO object is already buffered. self.flush_size = 0 end
Creates a new instance of this class with the given argument using new and then passes the instance to the given block. The close method is guaranteed to be called after the block completes.
Equivalent to new if no block is given.
# File lib/archive/zip/codec/store.rb, line 29 def self.open(io) store_io = new(io) return store_io unless block_given? begin yield(store_io) ensure store_io.close unless store_io.closed? end end
Public Instance Methods
Closes this object so that further write operations will fail. If
close_delegate is true
, the delegate object used as a
data sink will also be closed using its close method.
# File lib/archive/zip/codec/store.rb, line 57 def close(close_delegate = true) super() @io.close if close_delegate nil end
Returns an instance of Archive::Zip::DataDescriptor with information regarding the data which has passed through this object to the delegate object. The close or flush methods should be called before using this method in order to ensure that any possibly buffered data is flushed to the delegate object; otherwise, the contents of the data descriptor may be inaccurate.
# File lib/archive/zip/codec/store.rb, line 69 def data_descriptor DataDescriptor.new( @crc32, @uncompressed_size, @uncompressed_size ) end
Private Instance Methods
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.
# File lib/archive/zip/codec/store.rb, line 86 def unbuffered_seek(offset, whence = IO::SEEK_SET) unless offset == 0 && ((whence == IO::SEEK_SET && @io.respond_to?(:rewind)) || whence == IO::SEEK_CUR) then raise Errno::EINVAL end case whence when IO::SEEK_SET @io.rewind @crc32 = 0 @uncompressed_size = 0 when IO::SEEK_CUR @uncompressed_size end end
Writes string to the delegate object and returns the number of bytes actually written. Updates the uncompressed_size and crc32 attributes as a side effect.
# File lib/archive/zip/codec/store.rb, line 106 def unbuffered_write(string) bytes_written = @io.write(string) @uncompressed_size += bytes_written @crc32 = Zlib.crc32(string.slice(0, bytes_written), @crc32) bytes_written end