class BufferedBitstreamReader

Wraps a Bitstream and a DataStream.

Buffers the data from the data stream into the bit stream as required and then reads from the bitstream.

Public

Constructors

BufferedBitstreamReader

BufferedBitstreamReader(Bitstream *bitstream, const SPtr<DataStream> &dataStream, uint32_t preloadSize, uint32_t maxBufferSize)

Constructs a new instance of the object.

bitstream
Bitstream into which to load the buffered data.
dataStream
Data stream from which to read the data.
preloadSize
Determines the size of the chunk to preload, when we reach the end of buffered data. In bytes.
maxBufferSize
Maximum size of the buffer before it is cleared.

Methods

ReadBits

uint64_t ReadBits(Bitstream::QuantType *data, uint64_t count)

Reads bits from the stream into the provided buffer from the current cursor location and advances the cursor.

outData
Buffer to read the data from. Must have enough capacity to store bits.
count
Number of bits to read.

Returns: Number of bits read.

ReadBytes

uint32_t ReadBytes(Bitstream::QuantType *data, uint32_t count)

Reads bytes from the stream into the provided buffer from the current cursor location and advances the cursor.

outData
Buffer to read the data from. Must have enough capacity to store bytes.
count
Number of bytes to read.

Returns: Number of bytes read.

ReadVarInt

uint32_t ReadVarInt(uint32_t &value)

Decodes a 32-bit integer value encoded as a base-128 varint from the stream.

Read is performed at the current cursor location and advances the cursor. Varints are a method of serializing integers using one or more bytes, where smaller values use less bytes. Returns the number of bits written.

Skip

void Skip(int64_t count)

Skip a defined number of bits, moving the read/write cursor by this amount.

This can also be a negative value, in which case the file pointer rewinds a defined number of bits. Note the cursor can never skip past the capacity of the buffer, and will be clamped.

SkipBytes

void SkipBytes(int32_t count)

Same as skip() except is uses number of bytes instead of number of bits as the parameter.

Seek

void Seek(uint64_t pos)

Repositions the read/write cursor to the specified bit.

Note the cursor can never skip past the capacity of the buffer, and will be clamped.

Tell

uint64_t Tell() const

Returns the current read/write cursor position, in bits.

Align

void Align(uint32_t count = 1)

Aligns the read/write cursor to a byte boundary. determines the alignment in bytes.

Note the requested alignment might not be achieved if count > 1 and it would move the cursor past the capacity of the buffer, as the cursor will be clamped to buffer end regardless of alignment.

Returns number of bits skipped due to alignment.

Preload

void Preload(uint32_t count)

Preloads the specified number of bytes into the bitstream from the data stream.

ClearBuffered

void ClearBuffered(bool force)

Clears buffered data behind the current cursor location.

force
If false the buffer will only be cleared if its current size is over the maximum buffer size limit. Otherwise it will always be cleared.

GetDataStream

const SPtr<DataStream> &GetDataStream() const

Returns the underlying data stream.

GetBitstream

Bitstream &GetBitstream() const

Returns the underlying bitstream.

Private

Fields

mCursor

uint64_t mCursor

mBufferedRangeStart

uint64_t mBufferedRangeStart

mBufferedRangeEnd

uint64_t mBufferedRangeEnd

mBitstream

Bitstream * mBitstream

mDataStream

SPtr<DataStream> mDataStream

mMemBitstream

Bitstream mMemBitstream

mLength

uint64_t mLength

mPreloadSize

uint64_t mPreloadSize

mMaxBufferSize

uint64_t mMaxBufferSize

mIsMapped

bool mIsMapped