Sha256: c76e2607032d2e1cf0c66e51fce2e6f54b76daaac7a84451d281915753eb87e7

Contents?: true

Size: 1.28 KB

Versions: 14

Compression:

Stored size: 1.28 KB

Contents

# A simple stateful class for keeping track of a CRC32 value through multiple writes
class ZipTricks::StreamCRC32
  # Compute a CRC32 value from an IO object. The object should respond to `read` and `eof?`
  #
  # @param io[IO] the IO to read the data from
  # @return [Fixnum] the computed CRC32 value
  def self.from_io(io)
    crc = new
    crc << io.read(1024 * 512) until io.eof?
    crc.to_i
  end

  # Creates a new streaming CRC32 calculator
  def initialize
    @crc = Zlib.crc32('')
  end

  # Append data to the CRC32. Updates the contained CRC32 value in place.
  #
  # @param blob[String] the string to compute the CRC32 from
  # @return [self]
  def <<(blob)
    @crc = Zlib.crc32_combine(@crc, Zlib.crc32(blob), blob.bytesize)
    self
  end

  # Returns the CRC32 value computed so far
  #
  # @return [Fixnum] the updated CRC32 value for all the blobs so far
  def to_i
    @crc
  end

  # Appends a known CRC32 value to the current one, and combines the
  # contained CRC32 value in-place.
  #
  # @param crc32[Fixnum] the CRC32 value to append
  # @param blob_size[Fixnum] the size of the daata the `crc32` is computed from
  # @return [Fixnum] the updated CRC32 value for all the blobs so far
  def append(crc32, blob_size)
    @crc = Zlib.crc32_combine(@crc, crc32, blob_size)
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
zip_tricks-4.4.2 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.4.1 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.4.0 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.3.0 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.2.4 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.2.3 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.2.2 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.2.1 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.2.0 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.1.0 lib/zip_tricks/stream_crc32.rb
zip_tricks-4.0.0 lib/zip_tricks/stream_crc32.rb
zip_tricks-3.1.1 lib/zip_tricks/stream_crc32.rb
zip_tricks-3.1.0 lib/zip_tricks/stream_crc32.rb
zip_tricks-3.0.0 lib/zip_tricks/stream_crc32.rb