class ZipTricks::StreamCRC32
A simple stateful class for keeping track of a CRC32 value through multiple writes
Constants
- CRC_BUF_SIZE
- STRINGS_HAVE_CAPACITY_SUPPORT
Public Class Methods
Compute a CRC32 value from an IO object. The object should respond to `read` and `eof?`
@param io the IO to read the data from @return [Fixnum] the computed CRC32 value
# File lib/zip_tricks/stream_crc32.rb, line 18 def self.from_io(io) # If we can specify the string capacity upfront we will not have to resize # the string during operation. This saves time but is only available on # recent Ruby 2.x versions. blob = STRINGS_HAVE_CAPACITY_SUPPORT ? String.new('', capacity: CRC_BUF_SIZE) : String.new('') crc = new crc << io.read(CRC_BUF_SIZE, blob) until io.eof? crc.to_i end
Creates a new streaming CRC32 calculator
# File lib/zip_tricks/stream_crc32.rb, line 29 def initialize @crc = Zlib.crc32 end
Public Instance Methods
Append data to the CRC32. Updates the contained CRC32 value in place.
@param blob the string to compute the CRC32 from @return [self]
# File lib/zip_tricks/stream_crc32.rb, line 37 def <<(blob) @crc = Zlib.crc32(blob, @crc) self end
Appends a known CRC32 value to the current one, and combines the contained CRC32 value in-place.
@param crc32 the CRC32 value to append @param blob_size the size of the daata the `crc32` is computed from @return [Fixnum] the updated CRC32 value for all the blobs so far
# File lib/zip_tricks/stream_crc32.rb, line 55 def append(crc32, blob_size) @crc = Zlib.crc32_combine(@crc, crc32, blob_size) end
Returns the CRC32 value computed so far
@return [Fixnum] the updated CRC32 value for all the blobs so far
# File lib/zip_tricks/stream_crc32.rb, line 45 def to_i @crc end