Class: ZipTricks::StreamCRC32
- Inherits:
-
Object
- Object
- ZipTricks::StreamCRC32
- Defined in:
- lib/zip_tricks/stream_crc32.rb
Overview
A simple stateful class for keeping track of a CRC32 value through multiple writes
Constant Summary collapse
- BUFFER_SIZE =
1024 * 1024 * 5
Class Method Summary collapse
-
.from_io(io) ⇒ Fixnum
Compute a CRC32 value from an IO object.
Instance Method Summary collapse
-
#<<(blob) ⇒ self
Append data to the CRC32.
-
#append(crc32, blob_size) ⇒ Fixnum
Appends a known CRC32 value to the current one, and combines the contained CRC32 value in-place.
-
#initialize ⇒ StreamCRC32
constructor
Creates a new streaming CRC32 calculator.
-
#to_i ⇒ Fixnum
Returns the CRC32 value computed so far.
Constructor Details
#initialize ⇒ StreamCRC32
Creates a new streaming CRC32 calculator
18 19 20 21 |
# File 'lib/zip_tricks/stream_crc32.rb', line 18 def initialize @buf = StringIO.new @crc = Zlib.crc32('') end |
Class Method Details
.from_io(io) ⇒ Fixnum
Compute a CRC32 value from an IO object. The object should respond to read and eof?
11 12 13 14 15 |
# File 'lib/zip_tricks/stream_crc32.rb', line 11 def self.from_io(io) crc = new crc << io.read(BUFFER_SIZE) until io.eof? crc.to_i end |
Instance Method Details
#<<(blob) ⇒ self
Append data to the CRC32. Updates the contained CRC32 value in place.
27 28 29 30 31 |
# File 'lib/zip_tricks/stream_crc32.rb', line 27 def <<(blob) @buf << blob buf_flush if @buf.size > BUFFER_SIZE self end |
#append(crc32, blob_size) ⇒ Fixnum
Appends a known CRC32 value to the current one, and combines the contained CRC32 value in-place.
47 48 49 50 |
# File 'lib/zip_tricks/stream_crc32.rb', line 47 def append(crc32, blob_size) buf_flush if @buf.size > 0 @crc = Zlib.crc32_combine(@crc, crc32, blob_size) end |
#to_i ⇒ Fixnum
Returns the CRC32 value computed so far
36 37 38 39 |
# File 'lib/zip_tricks/stream_crc32.rb', line 36 def to_i buf_flush if @buf.size > 0 @crc end |