class HexaPDF::Utils::BitStreamReader

data can later be appended.
method. The data from where these bits are read, can be set on intialization and additional
This class allows one to read integers with a variable width from a bit stream using the #read
Helper class for reading variable length integers from a bit stream.

def append_data(str)

Appends some data to the string from where bits are read.
def append_data(str)
  @data.slice!(0, @pos)
  @data << str
  @pos = 0
  self
end

def initialize(data = +'')

Creates a new object, optionally providing the string from where the bits should be read.
def initialize(data = +'')
  @data = data.force_encoding(Encoding::BINARY)
  @pos = 0
  @bit_cache = 0
  @available_bits = 0
end

def read(bits)

Returns +nil+ if not enough bits are available for reading.

Reads +bits+ number of bits.
def read(bits)
  while @available_bits < bits
    @bit_cache = (@bit_cache << 8) | (@data.getbyte(@pos) || return)
    @pos += 1
    @available_bits += 8
  end
  @available_bits -= bits
  result = (@bit_cache >> @available_bits)
  @bit_cache &= (1 << @available_bits) - 1
  result
end

def read?(bits)

Returns +true+ if +bits+ number of bits can be read.
def read?(bits)
  remaining_bits >= bits
end

def remaining_bits

Returns the number of remaining bits that can be read.
def remaining_bits
  (@data.length - @pos) * 8 + @available_bits
end