Class: PureRubyZip::Bitstream

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Bitstream

Returns a new instance of Bitstream.



18
19
20
21
# File 'lib/pure_ruby_zip.rb', line 18

def initialize(data)
  @data = data
  @bit_index = 0
end

Instance Attribute Details

#bit_indexObject

Returns the value of attribute bit_index.



16
17
18
# File 'lib/pure_ruby_zip.rb', line 16

def bit_index
  @bit_index
end

#byteObject

Returns the value of attribute byte.



15
16
17
# File 'lib/pure_ruby_zip.rb', line 15

def byte
  @byte
end

#fileObject

Returns the value of attribute file.



17
18
19
# File 'lib/pure_ruby_zip.rb', line 17

def file
  @file
end

Instance Method Details

#read_bitObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/pure_ruby_zip.rb', line 22

def read_bit
  res = ((@data[0].codepoints.first >> @bit_index) & 1) == 1
  if @bit_index == 7
    @data = @data[1..-1]
    @bit_index = 0
  else
    @bit_index += 1
  end
  res
end

#read_int(n_bits) ⇒ Object



32
33
34
35
36
# File 'lib/pure_ruby_zip.rb', line 32

def read_int(n_bits)
  res = 0
  (0..(n_bits - 1)).each { |i| res += (read_bit ? 1 : 0) << i }
  res
end