Class: ZipKit::Streamer::DeflatedWriter
- Inherits:
-
Object
- Object
- ZipKit::Streamer::DeflatedWriter
- Includes:
- WriteShovel, ZlibCleanup
- Defined in:
- lib/zip_kit/streamer/deflated_writer.rb
Overview
Sends writes to the given io compressed using a Zlib::Deflate. Also
registers data passing through it in a CRC32 checksum calculator. Is made to be completely
interchangeable with the StoredWriter 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
-
#<<(data) ⇒ Object
Writes the given data into the deflater, and flushes the deflater after having written more than FLUSH_EVERY_N_BYTES bytes of data.
-
#finish ⇒ Hash
Returns the amount of data received for writing, the amount of compressed data written and the CRC32 checksum.
-
#initialize(io) ⇒ DeflatedWriter
constructor
A new instance of DeflatedWriter.
- #release_resources_on_failure! ⇒ Object
Methods included from ZlibCleanup
#safely_dispose_of_incomplete_deflater
Methods included from WriteShovel
Constructor Details
#initialize(io) ⇒ DeflatedWriter
Returns a new instance of DeflatedWriter.
16 17 18 19 20 21 |
# File 'lib/zip_kit/streamer/deflated_writer.rb', line 16 def initialize(io) @compressed_io = io @deflater = ::Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, -::Zlib::MAX_WBITS) @crc = ZipKit::StreamCRC32.new @crc_buf = ZipKit::WriteBuffer.new(@crc, CRC32_BUFFER_SIZE) end |
Instance Method Details
#<<(data) ⇒ Object
Writes the given data into the deflater, and flushes the deflater after having written more than FLUSH_EVERY_N_BYTES bytes of data
28 29 30 31 32 |
# File 'lib/zip_kit/streamer/deflated_writer.rb', line 28 def <<(data) @deflater.deflate(data) { |chunk| @compressed_io << chunk } @crc_buf << data self end |
#finish ⇒ Hash
Returns the amount of data received for writing, the amount of compressed data written and the CRC32 checksum. The return value can be directly used as the argument to ZipKit::Streamer#update_last_entry_and_write_data_descriptor
39 40 41 42 43 44 45 |
# File 'lib/zip_kit/streamer/deflated_writer.rb', line 39 def finish @compressed_io << @deflater.finish until @deflater.finished? @crc_buf.flush {crc32: @crc.to_i, compressed_size: @deflater.total_out, uncompressed_size: @deflater.total_in} ensure @deflater.close end |
#release_resources_on_failure! ⇒ Object
47 48 49 |
# File 'lib/zip_kit/streamer/deflated_writer.rb', line 47 def release_resources_on_failure! safely_dispose_of_incomplete_deflater(@deflater) end |