Class: ZipKit::StreamCRC32
- Inherits:
-
Object
- Object
- ZipKit::StreamCRC32
- Includes:
- WriteShovel
- Defined in:
- lib/zip_kit/stream_crc32.rb
Overview
A simple stateful class for keeping track of a CRC32 value through multiple writes
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.
Methods included from WriteShovel
Constructor Details
#initialize ⇒ StreamCRC32
Creates a new streaming CRC32 calculator
31 32 33 |
# File 'lib/zip_kit/stream_crc32.rb', line 31 def initialize @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?
20 21 22 23 24 25 26 27 28 |
# File 'lib/zip_kit/stream_crc32.rb', line 20 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) : +"" crc = new crc << io.read(CRC_BUF_SIZE, blob) until io.eof? crc.to_i end |
Instance Method Details
#<<(blob) ⇒ self
Append data to the CRC32. Updates the contained CRC32 value in place.
39 40 41 42 |
# File 'lib/zip_kit/stream_crc32.rb', line 39 def <<(blob) @crc = Zlib.crc32(blob, @crc) self end |
#append(crc32, blob_size) ⇒ Fixnum
Appends a known CRC32 value to the current one, and combines the contained CRC32 value in-place.
57 58 59 |
# File 'lib/zip_kit/stream_crc32.rb', line 57 def append(crc32, blob_size) @crc = Zlib.crc32_combine(@crc, crc32, blob_size) end |
#to_i ⇒ Fixnum
Returns the CRC32 value computed so far
47 48 49 |
# File 'lib/zip_kit/stream_crc32.rb', line 47 def to_i @crc end |