Class: Omnizip::Algorithms::BZip2::Bz2::BitReader

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/algorithms/bzip2/bz2.rb

Overview

MSB-first bit reader.

Instance Method Summary collapse

Constructor Details

#initialize(data, pos = 0) ⇒ BitReader

Returns a new instance of BitReader.



617
618
619
620
621
622
# File 'lib/omnizip/algorithms/bzip2/bz2.rb', line 617

def initialize(data, pos = 0)
  @data = data
  @pos = pos
  @bits = 0
  @nbits = 0
end

Instance Method Details

#read48Object



642
643
644
# File 'lib/omnizip/algorithms/bzip2/bz2.rb', line 642

def read48
  (read_bits(24) << 24) | read_bits(24)
end

#read_bitObject



624
625
626
627
628
629
630
631
632
633
634
# File 'lib/omnizip/algorithms/bzip2/bz2.rb', line 624

def read_bit
  if @nbits.zero?
    raise Omnizip::DecompressionError, "unexpected end of bitstream" if @pos >= @data.bytesize

    @bits = @data.getbyte(@pos)
    @pos += 1
    @nbits = 8
  end
  @nbits -= 1
  @bits.nobits?(1 << @nbits) ? 0 : 1
end

#read_bits(n) ⇒ Object



636
637
638
639
640
# File 'lib/omnizip/algorithms/bzip2/bz2.rb', line 636

def read_bits(n)
  v = 0
  n.times { v = (v << 1) | read_bit }
  v
end