class Origami::Filter::Utils::BitWriter
Class used to forge a String
from a stream of bits. Internally used by some filters.
Public Class Methods
new()
click to toggle source
# File lib/origami/filters.rb, line 79 def initialize @data = ''.b @last_byte = nil @ptr_bit = 0 end
Public Instance Methods
final()
click to toggle source
Finalizes the stream.
# File lib/origami/filters.rb, line 112 def final @data << @last_byte.chr if @last_byte @last_byte = nil @p = 0 self end
size()
click to toggle source
Returns the data size in bits.
# File lib/origami/filters.rb, line 105 def size (@data.size << 3) + @ptr_bit end
to_s()
click to toggle source
Outputs the stream as a String
.
# File lib/origami/filters.rb, line 123 def to_s @data.dup end
write(data, length)
click to toggle source
Writes data represented as Fixnum to a length number of bits.
# File lib/origami/filters.rb, line 88 def write(data, length) return BitWriterError, "Invalid data length" unless length > 0 and length >= data.bit_length # optimization for aligned byte writing if length == 8 and @last_byte.nil? and @ptr_bit == 0 @data << data.chr return self end write_bits(data, length) self end
Private Instance Methods
write_bits(data, length)
click to toggle source
Write the bits into the internal data.
# File lib/origami/filters.rb, line 132 def write_bits(data, length) while length > 0 if length >= 8 - @ptr_bit length -= 8 - @ptr_bit @last_byte ||= 0 @last_byte |= (data >> length) & ((1 << (8 - @ptr_bit)) - 1) data &= (1 << length) - 1 @data << @last_byte.chr @last_byte = nil @ptr_bit = 0 else @last_byte ||= 0 @last_byte |= (data & ((1 << length) - 1)) << (8 - @ptr_bit - length) @ptr_bit += length if @ptr_bit == 8 @data << @last_byte.chr @last_byte = nil @ptr_bit = 0 end length = 0 end end end