Class: ZipTricks::WriteBuffer
- Inherits:
-
Object
- Object
- ZipTricks::WriteBuffer
- Defined in:
- lib/zip_tricks/write_buffer.rb
Overview
Some operations (such as CRC32) benefit when they are performed on larger chunks of data. In certain use cases, it is possible that the consumer of ZipTricks is going to be writing small chunks in rapid succession, so CRC32 is going to have to perform a lot of CRC32 combine operations - and this adds up. Since the CRC32 value is usually not needed until the complete output has completed we can buffer at least some amount of data before computing CRC32 over it.
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Appends the given data to the write buffer, and flushes the buffer into the writable if the buffer size exceeds the
buffer_size
given at initialization. -
#flush! ⇒ Object
Explicitly flushes the buffer if it contains anything.
-
#initialize(writable, buffer_size) ⇒ WriteBuffer
constructor
Creates a new WriteBuffer bypassing into a given writable object.
-
#to_i ⇒ Integer
Flushes the buffer and returns the result of
#to_i
of the containedwritable
.
Constructor Details
#initialize(writable, buffer_size) ⇒ WriteBuffer
Creates a new WriteBuffer bypassing into a given writable object
15 16 17 18 19 |
# File 'lib/zip_tricks/write_buffer.rb', line 15 def initialize(writable, buffer_size) @buf = StringIO.new @buffer_size = buffer_size @writable = writable end |
Instance Method Details
#<<(data) ⇒ Object
Appends the given data to the write buffer, and flushes the buffer into the
writable if the buffer size exceeds the buffer_size
given at initialization
26 27 28 29 30 |
# File 'lib/zip_tricks/write_buffer.rb', line 26 def <<(data) @buf << data flush! if @buf.size > @buffer_size self end |
#flush! ⇒ Object
Explicitly flushes the buffer if it contains anything
35 36 37 38 39 40 |
# File 'lib/zip_tricks/write_buffer.rb', line 35 def flush! @writable << @buf.string if @buf.size > 0 @buf.truncate(0) @buf.rewind self end |
#to_i ⇒ Integer
Flushes the buffer and returns the result of #to_i
of the contained writable
.
Primarily facilitates working with StreamCRC32 objects where you finish the
computation by retrieving the CRC as an integer
47 48 49 50 |
# File 'lib/zip_tricks/write_buffer.rb', line 47 def to_i flush! @writable.to_i end |