Class: Bitcoin::BitStreamReader
- Inherits:
-
Object
- Object
- Bitcoin::BitStreamReader
- Defined in:
- lib/bitcoin/bit_stream.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
-
#initialize(payload) ⇒ BitStreamReader
constructor
A new instance of BitStreamReader.
-
#read(nbits) ⇒ Object
offset.
Constructor Details
#initialize(payload) ⇒ BitStreamReader
Returns a new instance of BitStreamReader.
44 45 46 47 48 |
# File 'lib/bitcoin/bit_stream.rb', line 44 def initialize(payload) @offset = 8 @buffer = 0 @stream = StringIO.new(payload) end |
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
41 42 43 |
# File 'lib/bitcoin/bit_stream.rb', line 41 def buffer @buffer end |
#offset ⇒ Object
Returns the value of attribute offset.
42 43 44 |
# File 'lib/bitcoin/bit_stream.rb', line 42 def offset @offset end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
40 41 42 |
# File 'lib/bitcoin/bit_stream.rb', line 40 def stream @stream end |
Instance Method Details
#read(nbits) ⇒ Object
offset
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/bitcoin/bit_stream.rb', line 51 def read(nbits) raise 'nbits must be between 0 and 64' if nbits < 0 || nbits > 64 data = 0 while nbits > 0 if offset == 8 raise IOError, 'stream is empty.' if stream.eof? self.buffer = stream.read(1).bth.to_i(16) self.offset = 0 end bits = [8 - offset, nbits].min data <<= bits tmp = (buffer << offset) & 255 data = data | (tmp >> (8 - bits)) self.offset += bits nbits -= bits end data end |