class RemiLib::BitReader
- RemiLib::BitReader
- Reference
- Object
Overview
Encapsulates an IO
to enable the reading of individual bits.
Defined in:
remilib/bitreader.crConstructors
-
.new(stream : IO)
Creates a new
BitReader
that will read data fromstream
.
Instance Method Summary
-
#advanceToNextByte : Nil
Advances this
BitReader
to the next byte boundary, discarding bits as it goes. -
#bitpos
Returns the current bit position within the current byte.
-
#byte : UInt8
Returns the current byte that the
BitReader
is reading bits from. -
#byte=(val : UInt8)
Sets what the
BitReader
considers the last byte read. -
#byte? : UInt8 | Nil
Returns the current byte that the
BitReader
is reading bits from, ornil
if were no more bytes to read. -
#countOnes(*, discardFirstZero : Bool = false) : Int
Reads bits until a
0
is encountered, counting the number of ones that are read. -
#countZeros(*, discardFirstOne : Bool = false) : Int
Reads bits until a
1
is encountered, counting the number of zeros that are read. -
#peek(count : Int) : Int64
Peeks at
count
bits, returning anInt64
. -
#peek?(count : Int) : Int64 | Nil
Peeks at
count
bits, if possible. -
#peekBytes(count : Int) : Array(UInt8)
Peeks
count
bytes into a new array, then returns that array. -
#pos
Returns the current byte position in the underlying
IO
. -
#pos=(value) : self
Returns the current byte position in the underlying
IO
. -
#read(dest : Bytes, offset : Int, count : Int) : Int
Reads
count
bytes intodest
starting atdest[offset]
. -
#read(dest : Array(UInt8), offset : Int, count : Int) : Int forall T
Reads
count
bytes intodest
starting atdest[offset]
. -
#read(count : Int) : Int64
Reads
count
bits, then returns the value as anInt64
. -
#read(dest : Bytes | Array(UInt8)) : Int
Reads
dest.size
bytes intodest
. -
#read?(count : Int) : Int64 | Nil
Try to read
count
bits, then returns the value as anInt64?
. -
#readByteArray(count : Int) : Array(UInt8)
Reads
count
bytes into a new array, then returns that array. -
#readBytes(count : Int32) : Bytes
Reads
count
bytes into a new array, then returns thatBytes
. - #readInt128 : Int128
- #readInt128BE : Int128
- #readInt16 : Int16
- #readInt16BE : Int16
- #readInt32 : Int32
- #readInt32BE : Int32
- #readInt64 : Int64
- #readInt64BE : Int64
-
#readString(sizeInBytes : Int) : String
Reads up to
sizeInBytes
, then attempts to convert those bytes into a string. -
#readString!(sizeInBytes : Int) : String
Reads up to
sizeInBytes
, then attempts to convert those bytes into a string. - #readUInt128 : UInt128
- #readUInt128BE : UInt128
- #readUInt16 : UInt16
- #readUInt16BE : UInt16
- #readUInt32 : UInt32
- #readUInt32BE : UInt32
- #readUInt64 : UInt64
- #readUInt64BE : UInt64
- #reinitialize(stream : IO)
-
#rewind : self
Rewinds the stream to the beginning so that the next bit read is the very first bit of the stream.
Constructor Detail
Creates a new BitReader
that will read data from stream
. This will
always call IO#read_byte
exactly once to populate the initial #byte
.
Instance Method Detail
Advances this BitReader
to the next byte boundary, discarding bits as it
goes. If the reader is already on a byte boundary, this does nothing.
Sets what the BitReader
considers the last byte read. This does not
affect the underlying stream.
Returns the current byte that the BitReader
is reading bits from, or
nil
if were no more bytes to read.
Reads bits until a 0
is encountered, counting the number of ones that
are read. This returns the number of zeros that were read before the 0
was encountered.
If discardFirstZero
is true
, then the first 0
bit is read and
discarded before returning. Otherwise it remains unread.
Reads bits until a 1
is encountered, counting the number of zeros that
are read. This returns the number of zeros that were read before the 1
was encountered.
If discardFirstOne
is true
, then the first 1
bit is read and
discarded before returning. Otherwise it remains unread.
Peeks at count
bits, if possible. On success, this returns an Int64
,
otherwise this returns nil
.
Peeks count
bytes into a new array, then returns that array. The number
of elements in the returned array may be less than count
if the end of
the file was reached.
Reads count
bytes into dest
starting at dest[offset]
. The
BitReader
must be on a byte boundary, or this will raise a
NotOnByteError
. This returns the number of bytes read, which maybe 0 if
nothing was read. The current #byte
is always the first byte put into
dest
.
Reads count
bytes into dest
starting at dest[offset]
. The
BitReader
must be on a byte boundary, or this will raise a
NotOnByteError
. This returns the number of bytes read, which maybe 0 if
nothing was read. The current #byte
is always the first byte put into
dest
.
Reads dest.size
bytes into dest
. The BitReader
must be on a byte
boundary, or this will raise a NotOnByteError
. This returns the number
of bytes read, which maybe 0 if nothing was read. The current #byte
is
always the first byte put into dest
.
Try to read count
bits, then returns the value as an Int64?
. If there
were not count
bits available, this returns nil
.
Reads count
bytes into a new array, then returns that array. The number
of elements in the returned array may be less than count
if the end of
the file was reached.
Reads count
bytes into a new array, then returns that Bytes
. If there
are not count
bytes remaining, this will raise an IO::EOFError
.
Reads up to sizeInBytes
, then attempts to convert those bytes into a
string. On success, this returns the new string, which may be smaller
than sizeInBytes
if there was not enough data left to read.
Reads up to sizeInBytes
, then attempts to convert those bytes into a
string. On success, this returns the new string. This raises an
IO::EOFError
if there was not enough data left to read.
Reinitializes this BitReader
with a new IO
. This completely resets
this instance. This will always call IO#read_byte
exactly once to
populate the initial #byte
.
Rewinds the stream to the beginning so that the next bit read is the very
first bit of the stream. Returns self
.