Sha256: 5a0a55fb6418b20609d8800e6d127ab49862990da3e9d09627623df99eec39a4

Contents?: true

Size: 1.67 KB

Versions: 12

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

# 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.
class ZipTricks::WriteBuffer
  # Creates a new WriteBuffer bypassing into a given writable object
  #
  # @param writable[#<<] An object that responds to `#<<` with string as argument
  # @param buffer_size[Integer] How many bytes to buffer
  def initialize(writable, buffer_size)
    @buf = StringIO.new
    @buffer_size = buffer_size
    @writable = writable
  end

  # 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
  #
  # @param data[String] data to be written
  # @return self
  def <<(data)
    @buf << data
    flush! if @buf.size > @buffer_size
    self
  end

  # Explicitly flushes the buffer if it contains anything
  #
  # @return self
  def flush!
    @writable << @buf.string if @buf.size > 0
    @buf.truncate(0)
    @buf.rewind
    self
  end

  # 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
  #
  # @return [Integer] the return value of `writable#to_i`
  def to_i
    flush!
    @writable.to_i
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
zip_tricks-5.4.0 lib/zip_tricks/write_buffer.rb
zip_tricks-4.8.3 lib/zip_tricks/write_buffer.rb
zip_tricks-5.3.1 lib/zip_tricks/write_buffer.rb
zip_tricks-4.8.2 lib/zip_tricks/write_buffer.rb
zip_tricks-5.3.0 lib/zip_tricks/write_buffer.rb
zip_tricks-5.2.0 lib/zip_tricks/write_buffer.rb
zip_tricks-4.8.1 lib/zip_tricks/write_buffer.rb
zip_tricks-5.1.1 lib/zip_tricks/write_buffer.rb
zip_tricks-5.1.0 lib/zip_tricks/write_buffer.rb
zip_tricks-5.0.0 lib/zip_tricks/write_buffer.rb
zip_tricks-4.8.0 lib/zip_tricks/write_buffer.rb
zip_tricks-4.7.4 lib/zip_tricks/write_buffer.rb