Class: Omnizip::Algorithms::Zstandard::FSE::BitStream
- Inherits:
-
Object
- Object
- Omnizip::Algorithms::Zstandard::FSE::BitStream
- Defined in:
- lib/omnizip/algorithms/zstandard/fse/bitstream.rb
Overview
FSE bitstream reader (RFC 8878 Section 4.1)
Reads FSE-encoded bitstreams which are read in reverse order (from end to beginning) according to RFC 8878.
The bitstream is consumed from the end toward the beginning, with bits read from LSB to MSB within each byte.
Instance Attribute Summary collapse
-
#bit_position ⇒ Integer
readonly
Current bit position (from end).
-
#data ⇒ String
readonly
The compressed data.
Instance Method Summary collapse
-
#align_to_byte ⇒ Object
Align to byte boundary (skip remaining bits in current byte).
-
#exhausted? ⇒ Boolean
Check if bitstream is exhausted.
-
#initialize(data) ⇒ BitStream
constructor
Initialize bitstream with data.
-
#peek_bits(count) ⇒ Integer
Peek at bits without consuming them.
-
#read_bits(count) ⇒ Integer
Read bits from the stream (in reverse order).
-
#remaining_bits ⇒ Integer
Get remaining bits.
Constructor Details
#initialize(data) ⇒ BitStream
Initialize bitstream with data
44 45 46 47 |
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 44 def initialize(data) @data = data.dup.force_encoding(Encoding::BINARY) @bit_position = data.bytesize * 8 end |
Instance Attribute Details
#bit_position ⇒ Integer (readonly)
Returns Current bit position (from end).
39 40 41 |
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 39 def bit_position @bit_position end |
#data ⇒ String (readonly)
Returns The compressed data.
36 37 38 |
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 36 def data @data end |
Instance Method Details
#align_to_byte ⇒ Object
Align to byte boundary (skip remaining bits in current byte)
92 93 94 |
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 92 def align_to_byte @bit_position = ((@bit_position + 7) / 8) * 8 end |
#exhausted? ⇒ Boolean
Check if bitstream is exhausted
80 81 82 |
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 80 def exhausted? @bit_position <= 0 end |
#peek_bits(count) ⇒ Integer
Peek at bits without consuming them
70 71 72 73 74 75 |
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 70 def peek_bits(count) saved_position = @bit_position result = read_bits(count) @bit_position = saved_position result end |
#read_bits(count) ⇒ Integer
Read bits from the stream (in reverse order)
Bits are read from LSB to MSB, starting from the end of the stream.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 55 def read_bits(count) return 0 if count.zero? result = 0 count.times do |i| bit = read_single_bit result |= (bit << i) end result end |
#remaining_bits ⇒ Integer
Get remaining bits
87 88 89 |
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 87 def remaining_bits @bit_position end |