class Bitcoin::BitStreamWriter

Constants

MAX_BIT

Attributes

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

Public Class Methods

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

Public Instance Methods

flush() click to toggle source
# File lib/bitcoin/bit_stream.rb, line 29
def flush
  return if offset == 0
  self.stream << buffer.itb
  self.offset = 0
  self.buffer = 0
end
write(data, nbits) click to toggle source
# File lib/bitcoin/bit_stream.rb, line 17
def write(data, nbits)
  raise "nbits must be between 0 and 64" if nbits < 0 || nbits > 64
  while nbits > 0
    bits = [8 - offset, nbits].min
    tmp = (data << (64 - nbits)) & 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
    self.buffer |= (tmp >> (64 - 8 + offset))
    self.offset += bits
    nbits -= bits
    flush if offset == 8
  end
end