class HexaPDF::Utils::BitStreamWriter
Helper class for writing out variable length integers one after another as bit stream.
This class allows one to write integers with a variable width of up to 16 bit to a bit stream using the write
method. Every time when at least 16 bits are available, the write
method returns those 16 bits as string and removes them from the internal cache.
Once all data has been written, the finalize
method must be called to get the last remaining bits (again as a string).
Public Instance Methods
finalize()
click to toggle source
Retrieves the final (zero padded) bits as a string.
# File lib/hexapdf/utils/bit_stream.rb, line 125 def finalize result = [@bit_cache].pack('N')[0...(@available_bits / 8.0).ceil] initialize result end
write(int, bits)
click to toggle source
Writes the integer int
with a width of bits
to the bit stream.
Returns a 16bit binary string if enough bits are available or an empty binary string otherwise.
# File lib/hexapdf/utils/bit_stream.rb, line 111 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