Class: Origami::Filter::Utils::BitWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/origami/filters.rb

Overview

Class used to forge a String from a stream of bits. Internally used by some filters.

Instance Method Summary collapse

Constructor Details

#initializeBitWriter

Returns a new instance of BitWriter.



75
76
77
78
79
# File 'lib/origami/filters.rb', line 75

def initialize
  @data = ''.b
  @last_byte = nil
  @ptr_bit = 0
end

Instance Method Details

#finalObject

Finalizes the stream.



108
109
110
111
112
113
114
# File 'lib/origami/filters.rb', line 108

def final
  @data << @last_byte.chr if @last_byte
  @last_byte = nil
  @p = 0

  self
end

#sizeObject

Returns the data size in bits.



101
102
103
# File 'lib/origami/filters.rb', line 101

def size
  (@data.size << 3) + @ptr_bit
end

#to_sObject

Outputs the stream as a String.



119
120
121
# File 'lib/origami/filters.rb', line 119

def to_s
  @data.dup
end

#write(data, length) ⇒ Object

Writes data represented as Fixnum to a length number of bits.



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/origami/filters.rb', line 84

def write(data, length)
  return BitWriterError, "Invalid data length" unless (length > 0) && (length >= data.bit_length)

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

  write_bits(data, length)

  self
end