Class: BinData::IO::Read
Overview
Create a new IO Read wrapper around io. io must provide #read, #pos if reading the current stream position and #seek if setting the current stream position. If io is a string it will be automatically wrapped in an StringIO object.
The IO can handle bitstreams in either big or little endian format.
M byte1 L M byte2 L
S 76543210 S S fedcba98 S
B B B B
In big endian format:
readbits(6), readbits(5) #=> [765432, 10fed]
In little endian format:
readbits(6), readbits(5) #=> [543210, a9876]
Instance Method Summary collapse
-
#initialize(io) ⇒ Read
constructor
A new instance of Read.
-
#offset ⇒ Object
Returns the current offset of the io stream.
-
#read_all_bytes ⇒ Object
Reads all remaining bytes from the stream.
-
#readbits(nbits, endian) ⇒ Object
Reads exactly
nbitsbits from the stream. -
#readbytes(n) ⇒ Object
Reads exactly
nbytes fromio. -
#reset_read_bits ⇒ Object
Discards any read bits so the stream becomes aligned at the next byte boundary.
-
#seekbytes(n) ⇒ Object
Seek
nbytes from the current position in the io stream. -
#with_buffer(n) ⇒ Object
Sets a buffer of
nbytes on the io stream.
Constructor Details
#initialize(io) ⇒ Read
Returns a new instance of Read.
239 240 241 242 243 244 245 246 |
# File 'lib/bindata/io.rb', line 239 def initialize(io) super(io) # bits when reading @rnbits = 0 @rval = 0 @rendian = nil end |
Instance Method Details
#offset ⇒ Object
Returns the current offset of the io stream. Offset will be rounded up when reading bitfields.
259 260 261 |
# File 'lib/bindata/io.rb', line 259 def offset offset_raw end |
#read_all_bytes ⇒ Object
Reads all remaining bytes from the stream.
284 285 286 287 |
# File 'lib/bindata/io.rb', line 284 def read_all_bytes reset_read_bits read end |
#readbits(nbits, endian) ⇒ Object
Reads exactly nbits bits from the stream. endian specifies whether the bits are stored in :big or :little endian format.
291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/bindata/io.rb', line 291 def readbits(nbits, endian) if @rendian != endian # don't mix bits of differing endian reset_read_bits @rendian = endian end if endian == :big read_big_endian_bits(nbits) else read_little_endian_bits(nbits) end end |
#readbytes(n) ⇒ Object
Reads exactly n bytes from io.
If the data read is nil an EOFError is raised.
If the data read is too short an IOError is raised.
274 275 276 277 278 279 280 281 |
# File 'lib/bindata/io.rb', line 274 def readbytes(n) reset_read_bits str = read(n) raise EOFError, "End of file reached" if str.nil? raise IOError, "data truncated" if str.size < n str end |
#reset_read_bits ⇒ Object
Discards any read bits so the stream becomes aligned at the next byte boundary.
307 308 309 310 |
# File 'lib/bindata/io.rb', line 307 def reset_read_bits @rnbits = 0 @rval = 0 end |
#seekbytes(n) ⇒ Object
Seek n bytes from the current position in the io stream.
264 265 266 267 |
# File 'lib/bindata/io.rb', line 264 def seekbytes(n) reset_read_bits seek(n) end |
#with_buffer(n) ⇒ Object
Sets a buffer of n bytes on the io stream. Any reading or seeking calls inside the block will be contained within this buffer.
250 251 252 253 254 255 |
# File 'lib/bindata/io.rb', line 250 def with_buffer(n) with_buffer_common(n) do yield read end end |