Class: Omnizip::Algorithms::Zstandard::FSE::BitStream

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ BitStream

Initialize bitstream with data

Parameters:

  • data (String)

    The compressed bitstream 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_positionInteger (readonly)

Returns Current bit position (from end).

Returns:

  • (Integer)

    Current bit position (from end)



39
40
41
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 39

def bit_position
  @bit_position
end

#dataString (readonly)

Returns The compressed data.

Returns:

  • (String)

    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_byteObject

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

Returns:

  • (Boolean)


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

Parameters:

  • count (Integer)

    Number of bits to peek

Returns:

  • (Integer)

    The peeked bits



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.

Parameters:

  • count (Integer)

    Number of bits to read

Returns:

  • (Integer)

    The read bits



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_bitsInteger

Get remaining bits

Returns:

  • (Integer)


87
88
89
# File 'lib/omnizip/algorithms/zstandard/fse/bitstream.rb', line 87

def remaining_bits
  @bit_position
end