class HexaPDF::Utils::BitStreamWriter
remaining bits (again as a string).
Once all data has been written, the #finalize method must be called to get the last
method returns those 16 bits as string and removes them from the internal cache.
stream using the #write method. Every time when at least 16 bits are available, the #write
This class allows one to write integers with a variable width of up to 16 bit to a bit
Helper class for writing out variable length integers one after another as bit stream.
def finalize
def finalize result = [@bit_cache].pack('N')[0...(@available_bits / 8.0).ceil] initialize result end
def initialize # :nodoc:
def initialize # :nodoc: @bit_cache = 0 @available_bits = 0 end
def write(int, bits)
Returns a 16bit binary string if enough bits are available or an empty binary string
Writes the integer +int+ with a width of +bits+ to the bit stream.
def write(int, bits) @available_bits += bits @bit_cache |= int << (32 - @available_bits) if @available_bits >= 16 @available_bits -= 16 result = (@bit_cache >> 24).chr << ((@bit_cache >> 16) & 0xFF).chr @bit_cache = (@bit_cache & 0xFFFF) << 16 result else ''.b end end