Class: Resedit::BitStream
- Inherits:
-
Object
- Object
- Resedit::BitStream
- Defined in:
- lib/resedit/convert/bitstream.rb
Instance Attribute Summary collapse
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
Instance Method Summary collapse
- #eof? ⇒ Boolean
- #finish ⇒ Object
-
#initialize(bytes = nil, chunksize = 1) ⇒ BitStream
constructor
A new instance of BitStream.
- #read(bits) ⇒ Object
- #write(val, bits = 8) ⇒ Object
Constructor Details
#initialize(bytes = nil, chunksize = 1) ⇒ BitStream
Returns a new instance of BitStream.
7 8 9 10 11 12 13 14 |
# File 'lib/resedit/convert/bitstream.rb', line 7 def initialize(bytes=nil, chunksize=1) @bytes = bytes ? bytes : '' @chunksize = chunksize @bytes.force_encoding(Encoding::ASCII_8BIT) @buf = 0 @bsize = 0 @pos = 0 end |
Instance Attribute Details
#bytes ⇒ Object (readonly)
Returns the value of attribute bytes.
5 6 7 |
# File 'lib/resedit/convert/bitstream.rb', line 5 def bytes @bytes end |
Instance Method Details
#eof? ⇒ Boolean
16 |
# File 'lib/resedit/convert/bitstream.rb', line 16 def eof?; @pos>=@bytes.length end |
#finish ⇒ Object
47 48 49 50 51 52 |
# File 'lib/resedit/convert/bitstream.rb', line 47 def finish() if @bsize!=0 write(0, @chunksize*8-@bsize) end return @bytes end |
#read(bits) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/resedit/convert/bitstream.rb', line 18 def read(bits) while @bsize < bits for _ in 1..@chunksize raise "End of stream" if @pos==@bytes.length @buf |= (@pos<@bytes.length ? (@bytes[@pos].ord & 0xFF) : 0) << @bsize @pos += 1 @bsize += 8 end end mask = (1 << bits) - 1 ret = @buf & mask @buf >>= bits @bsize -= bits return ret end |
#write(val, bits = 8) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/resedit/convert/bitstream.rb', line 34 def write(val, bits=8) mask = (1 << bits) - 1 @buf |= (val & mask) << @bsize @bsize += bits if @bsize >= @chunksize*8 for _ in 1..@chunksize @bytes += (@buf & 0xFF).chr @bsize -= 8 @buf>>=8 end end end |