libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
fly::BitStreamReader Class Reference

#include <bit_stream_reader.hpp>

Inheritance diagram for fly::BitStreamReader:
Collaboration diagram for fly::BitStreamReader:

Public Member Functions

 BitStreamReader (std::istream &stream) noexcept
 
bool read_word (word_type &word)
 
bool read_byte (byte_type &byte)
 
template<detail::BitStreamInteger DataType>
byte_type read_bits (DataType &bits, byte_type size)
 
template<detail::BitStreamInteger DataType>
byte_type peek_bits (DataType &bits, byte_type size)
 
void discard_bits (byte_type size)
 
bool fully_consumed () const
 
byte_type header () const
 
- Public Member Functions inherited from fly::detail::BitStream
virtual ~BitStream ()
 

Additional Inherited Members

- Protected Member Functions inherited from fly::detail::BitStream
 BitStream (std::streambuf *stream_buffer, byte_type starting_position) noexcept
 
template<detail::BitStreamInteger DataType>
DataType bit_mask (const DataType bits)
 
- Protected Attributes inherited from fly::detail::BitStream
std::streambuf * m_stream_buffer
 
buffer_type m_buffer {0}
 
byte_type m_position {0}
 

Detailed Description

Implementation of the BitStream interface for reading from a binary stream.

The stream is read in a lazy manner; bytes are not read from the stream until they are needed. The number of bytes read from the stream at once is defined by the size of buffer_type. That buffer is stored in-memory until it has been entirely consumed by the caller, at which point it is refilled.

Author
Timothy Flynn (trfly.nosp@m.nn89.nosp@m.@pm.m.nosp@m.e)
Version
July 7, 2019

Constructor & Destructor Documentation

◆ BitStreamReader()

fly::BitStreamReader::BitStreamReader ( std::istream &  stream)
explicitnoexcept

Constructor. Decode the header byte from the stream. If the header byte is invalid, the stream's fail bit is set.

Parameters
streamThe stream to read binary data from.

Member Function Documentation

◆ discard_bits()

void fly::BitStreamReader::discard_bits ( byte_type  size)
inline

Discard a number of bits from the byte buffer. Should only be used after a successful call to peek_bits.

Parameters
sizeThe number of bits to discard.

◆ fully_consumed()

bool fly::BitStreamReader::fully_consumed ( ) const

Check if the stream has reached end-of-file and the byte buffer has been fully consumed.

Returns
True if the stream has been fully consumed.

◆ header()

byte_type fly::BitStreamReader::header ( ) const
Returns
The header byte decoded from the stream.

◆ peek_bits()

template<detail::BitStreamInteger DataType>
byte_type fly::BitStreamReader::peek_bits ( DataType &  bits,
byte_type  size 
)

Read a number of bits from the byte buffer without discarding those bits. There is no guarantee that the requested number of bits will actually be peeked, as there may be less than that number available between the byte buffer and stream. If any bits were peeked, the least-significant bits in the provided data type will be filled, starting from the position pointed to by the requested number of bits.

Fill the buffer from the stream if the number of bits to peek exceeds the number of bits available.

Parameters
bitsThe location to store the peeked bits.
sizeThe number of bits to peek.
Returns
The number of bits successfully peeked.

◆ read_bits()

template<detail::BitStreamInteger DataType>
byte_type fly::BitStreamReader::read_bits ( DataType &  bits,
byte_type  size 
)

Read a number of bits from the byte buffer. There is no guarantee that the requested number of bits will actually be read, as there may be less than that number available between the byte buffer and stream. If any bits were read, the least-significant bits in the provided data type will be filled, starting from the position pointed to by the requested number of bits.

Fill the buffer from the stream if the number of bits to read exceeds the number of bits available.

Template Parameters
DataTypeThe data type of the location to store the read bits.
Parameters
bitsThe location to store the read bits.
sizeThe number of bits to read.
Returns
The number of bits successfully read.

◆ read_byte()

bool fly::BitStreamReader::read_byte ( byte_type &  byte)

Read a full byte from the byte buffer.

Fill the buffer from the stream if the number of bits to read exceeds the number of bits available.

Parameters
byteThe location to store the read byte.
Returns
True if the byte was successfully read and filling the byte buffer was successful (if needed).

◆ read_word()

bool fly::BitStreamReader::read_word ( word_type &  word)

Read a multibyte word from the byte buffer.

Fill the buffer from the stream if the number of bits to read exceeds the number of bits available.

Parameters
wordThe location to store the read word.
Returns
True if the word was successfully read and filling the byte buffer was successful (if needed).

The documentation for this class was generated from the following files: