Class: ZipTricks::Streamer::StoredWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_tricks/streamer/stored_writer.rb

Overview

Sends writes to the given io, and also registers all the data passing through it in a CRC32 checksum calculator. Is made to be completely interchangeable with the DeflatedWriter in terms of interface.

Constant Summary collapse

CRC32_BUFFER_SIZE =

The amount of bytes we will buffer before computing the intermediate CRC32 checksums. Benchmarks show that the optimum is 64KB (see `bench/buffered_crc32_bench.rb), if that is exceeded Zlib is going to perform internal CRC combine calls which will make the speed go down again.

64 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ StoredWriter

Returns a new instance of StoredWriter.



13
14
15
16
17
# File 'lib/zip_tricks/streamer/stored_writer.rb', line 13

def initialize(io)
  @io = ZipTricks::WriteAndTell.new(io)
  @crc_compute = ZipTricks::StreamCRC32.new
  @crc = ZipTricks::WriteBuffer.new(@crc_compute, CRC32_BUFFER_SIZE)
end

Instance Method Details

#<<(data) ⇒ Object

Writes the given data to the contained IO object.

Parameters:

  • data (String)

    data to be written

Returns:

  • self



23
24
25
26
27
# File 'lib/zip_tricks/streamer/stored_writer.rb', line 23

def <<(data)
  @io << data
  @crc << data
  self
end

#finishHash

Returns the amount of data written and the CRC32 checksum. The return value can be directly used as the argument to ZipTricks::Streamer#update_last_entry_and_write_data_descriptor

Returns:

  • (Hash)

    a hash of {crc32, compressed_size, uncompressed_size}



33
34
35
36
# File 'lib/zip_tricks/streamer/stored_writer.rb', line 33

def finish
  @crc.flush
  {crc32: @crc_compute.to_i, compressed_size: @io.tell, uncompressed_size: @io.tell}
end