class Bitcoin::BitStreamReader

Attributes

buffer[RW]
offset[RW]
stream[R]

Public Class Methods

new(payload) click to toggle source
# File lib/bitcoin/bit_stream.rb, line 44
def initialize(payload)
  @offset = 8
  @buffer = 0
  @stream = StringIO.new(payload)
end

Public Instance Methods

read(nbits) click to toggle source

offset

# File lib/bitcoin/bit_stream.rb, line 51
def read(nbits)
  raise 'nbits must be between 0 and 64' if nbits < 0 || nbits > 64
  data = 0
  while nbits > 0
    if offset == 8
      raise IOError, 'stream is empty.' if stream.eof?
      self.buffer = stream.read(1).bth.to_i(16)
      self.offset = 0
    end
    bits = [8 - offset, nbits].min
    data <<= bits
    tmp = (buffer << offset) & 255
    data = data | (tmp >> (8 - bits))
    self.offset += bits
    nbits -= bits
  end
  data
end