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.



79
80
81
82
83
# File 'lib/origami/filters.rb', line 79

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

Instance Method Details

#finalObject

Finalizes the stream.



112
113
114
115
116
117
118
# File 'lib/origami/filters.rb', line 112

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

    self
end

#sizeObject

Returns the data size in bits.



105
106
107
# File 'lib/origami/filters.rb', line 105

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

#to_sObject

Outputs the stream as a String.



123
124
125
# File 'lib/origami/filters.rb', line 123

def to_s
    @data.dup
end

#write(data, length) ⇒ Object

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



88
89
90
91
92
93
94
95
96
97
98
99
100
# 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