Sha256: fa665525d57fcb372b988ec136b57ad2fa54141283c14c5f6d8200b0968b0989

Contents?: true

Size: 1.32 KB

Versions: 16

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

# 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.
class ZipTricks::Streamer::StoredWriter
  # 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.
  CRC32_BUFFER_SIZE = 64 * 1024

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

  # Writes the given data to the contained IO object.
  #
  # @param data[String] data to be written
  # @return self
  def <<(data)
    @io << data
    @crc << data
    self
  end

  # Returns the amount of data written and the CRC32 checksum. The return value
  # can be directly used as the argument to {Streamer#update_last_entry_and_write_data_descriptor}
  #
  # @param data[String] data to be written
  # @return [Hash] a hash of `{crc32, compressed_size, uncompressed_size}`
  def finish
    {crc32: @crc.to_i, compressed_size: @io.tell, uncompressed_size: @io.tell}
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
zip_tricks-4.8.3 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-5.3.1 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-4.8.2 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-5.3.0 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-5.2.0 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-4.8.1 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-5.1.1 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-5.1.0 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-5.0.0 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-4.8.0 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-4.7.4 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-4.7.3 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-4.7.2 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-4.7.1 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-4.7.0 lib/zip_tricks/streamer/stored_writer.rb
zip_tricks-4.6.0 lib/zip_tricks/streamer/stored_writer.rb