Parent

Class/Module Index [+]

Quicksearch

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 53
def initialize
  @data = ''
  @last_byte = nil
  @ptr_bit = 0
end

Public Instance Methods

final() click to toggle source

Finalizes the stream.

# File lib/origami/filters.rb, line 109
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 102
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 120
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 62
def write(data, length)
  return BitWriterError, "Invalid data length" unless length > 0 and (1 << length) > data

  # optimization for aligned byte writing
  if length == 8 and @last_byte.nil? and @ptr_bit == 0
    @data << data.chr
    return self
  end

  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

  self
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.