class RemiLib::BitReader

Overview

Encapsulates an IO to enable the reading of individual bits.

Defined in:

remilib/bitreader.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(stream : IO) #

Creates a new BitReader that will read data from stream. This will always call IO#read_byte exactly once to populate the initial #byte.


[View source]

Instance Method Detail

def advanceToNextByte : Nil #

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.


[View source]
def bitpos #

Returns the current bit position within the current byte.


[View source]
def byte : UInt8 #

Returns the current byte that the BitReader is reading bits from.


[View source]
def byte=(val : UInt8) #

Sets what the BitReader considers the last byte read. This does not affect the underlying stream.


[View source]
def byte? : UInt8 | Nil #

Returns the current byte that the BitReader is reading bits from, or nil if were no more bytes to read.


[View source]
def countOnes(*, discardFirstZero : Bool = false) : Int #

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.


[View source]
def countZeros(*, discardFirstOne : Bool = false) : Int #

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.


[View source]
def peek(count : Int) : Int64 #

Peeks at count bits, returning an Int64.


[View source]
def peek?(count : Int) : Int64 | Nil #

Peeks at count bits, if possible. On success, this returns an Int64, otherwise this returns nil.


[View source]
def peekBytes(count : Int) : Array(UInt8) #

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.


[View source]
def pos #

Returns the current byte position in the underlying IO.


[View source]
def pos=(value) : self #

Returns the current byte position in the underlying IO. Returns self.


[View source]
def read(dest : Bytes, offset : Int, count : Int) : Int #

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.


[View source]
def read(dest : Array(UInt8), offset : Int, count : Int) : Int forall T #

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.


[View source]
def read(count : Int) : Int64 #

Reads count bits, then returns the value as an Int64.


[View source]
def read(dest : Bytes | Array(UInt8)) : Int #

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.


[View source]
def read?(count : Int) : Int64 | Nil #

Try to read count bits, then returns the value as an Int64?. If there were not count bits available, this returns nil.


[View source]
def readByteArray(count : Int) : Array(UInt8) #

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.


[View source]
def readBytes(count : Int32) : Bytes #

Reads count bytes into a new array, then returns that Bytes. If there are not count bytes remaining, this will raise an IO::EOFError.


[View source]
def readInt128 : Int128 #

[View source]
def readInt128BE : Int128 #

[View source]
def readInt16 : Int16 #

[View source]
def readInt16BE : Int16 #

[View source]
def readInt32 : Int32 #

[View source]
def readInt32BE : Int32 #

[View source]
def readInt64 : Int64 #

[View source]
def readInt64BE : Int64 #

[View source]
def readString(sizeInBytes : Int) : String #

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.


[View source]
def readString!(sizeInBytes : Int) : String #

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.


[View source]
def readUInt128 : UInt128 #

[View source]
def readUInt128BE : UInt128 #

[View source]
def readUInt16 : UInt16 #

[View source]
def readUInt16BE : UInt16 #

[View source]
def readUInt32 : UInt32 #

[View source]
def readUInt32BE : UInt32 #

[View source]
def readUInt64 : UInt64 #

[View source]
def readUInt64BE : UInt64 #

[View source]
def reinitialize(stream : IO) #

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.


[View source]
def rewind : self #

Rewinds the stream to the beginning so that the next bit read is the very first bit of the stream. Returns self.


[View source]