zserio.bitreader module

The module implements abstraction for reading data to the bit stream.

class zserio.bitreader.BitStreamReader(buffer: bytes, bitsize: int | None = None)[source]

Bases: object

Bit stream reader.

Constructs bit stream reader from bytes buffer.

Because bit buffer size does not have to be byte aligned (divisible by 8), it’s possible that not all bits of the last byte are used. In this case, only most significant bits of the corresponded size are used.

Parameters:
  • buffer – Bytes-like buffer to read as a bit stream.

  • bitsize – Number of bits stored in buffer to use.

Raises:

PythonRuntimeException – If bitsize is out of range.

alignto(alignment: int) None[source]

Aligns the bit position according to the aligning value.

Parameters:

alignment – An aligning value to use.

Raises:

PythonRuntimeException – If the aligning moves behind the stream.”

property bitposition: int

Gets current bit position.

Returns:

Current bit position.

property buffer_bitsize: int

Gets size of the underlying buffer in bits.

Returns:

Buffer bit size.

classmethod from_bitbuffer(bitbuffer: BitBuffer) BitStreamReader[source]

Constructs bit stream reader from bit buffer.

Parameters:

bitbuffer – Bit buffer to read as a bit stream.

classmethod from_file(filename: str) BitStreamReader[source]

Constructs bit stream reader from file.

Parameters:

filename – Filename to read as a bit stream.

read_bitbuffer() BitBuffer[source]

Reads a bit buffer from the stream.

Returns:

Read bit buffer.

Raises:

PythonRuntimeException – If the reading goes behind the stream.

read_bits(numbits: int) int[source]

Reads given number of bits from the bit stream as an unsigned integer.

Parameters:

numbits – Number of bits to read.

Returns:

Read bits as an unsigned integer.

Raises:

PythonRuntimeException – If the numbits is invalid number of the reading goes behind the stream.

read_bits_unchecked(numbits: int) int[source]

Reads given number of bits from the bit stream as an unsigned integer.

This method does not check that numbits >= 0 and assumes that it’s ensured by the caller.

Parameters:

numbits – Number of bits to read.

Returns:

Read bits as an unsigned integer.

Raises:

PythonRuntimeException – If the numbits is invalid number of the reading goes behind the stream.

read_bool() bool[source]

Reads single bit as a bool value.

Returns:

Read bool values.

Raises:

PythonRuntimeException – If the reading goes behind the stream.

read_bytes() bytearray[source]

Reads bytes from the stream.

Returns:

Read bytes.

Raises:

PythonRuntimeException – If the reading goes behind the stream.

read_float16() float[source]

Read 16-bits from the stream as a float value encoded according to IEEE 754 binary16.

Returns:

Read float value.

Raises:

PythonRuntimeException – If the reading goes behind the stream.

read_float32() float[source]

Read 32-bits from the stream as a float value encoded according to IEEE 754 binary32.

Returns:

Read float value.

Raises:

PythonRuntimeException – If the reading goes behind the stream.

read_float64() float[source]

Read 64-bits from the stream as a float value encoded according to IEEE 754 binary64.

Returns:

Read float value.

Raises:

PythonRuntimeException – If the reading goes behind the stream.

read_signed_bits(numbits: int) int[source]

Reads given number of bits from the bit stream as a signed integer.

Parameters:

numbits – Number of bits to read

Returns:

Read bits as a signed integer.

Raises:

PythonRuntimeException – If the numbits is invalid number of the reading goes behind the stream.

read_signed_bits_unchecked(numbits: int) int[source]

Reads given number of bits from the bit stream as a signed integer.

This method does not check that numbits >= 0 and assumes that it’s ensured by the caller.

Parameters:

numbits – Number of bits to read

Returns:

Read bits as a signed integer.

Raises:

PythonRuntimeException – If the numbits is invalid number of the reading goes behind the stream.

read_string() str[source]

Reads string from the stream.

Returns:

Read string.

Raises:

PythonRuntimeException – If the reading goes behind the stream.

read_varint() int[source]

Reads variable signed integer value (up to 9 bytes) from the bit stream.

Returns:

Variable signed integer value (up to 9 bytes).

read_varint16() int[source]

Reads variable 16-bit signed integer value from the bit stream.

Returns:

Variable 16-bit signed integer value.

read_varint32() int[source]

Reads variable 32-bit signed integer value from the bit stream.

Returns:

Variable 32-bit signed integer value.

read_varint64() int[source]

Reads variable 64-bit signed integer value from the bit stream.

Returns:

Variable 64-bit signed integer value.

read_varsize() int[source]

Reads variable size integer value from the bit stream.

Returns:

Variable size integer value.

Raises:

PythonRuntimeException – If read variable size integer is out of range.

read_varuint() int[source]

Reads variable unsigned integer value (up to 9 bytes) from the bit stream.

Returns:

Variable unsigned integer value (up to 9 bytes).

read_varuint16() int[source]

Reads variable 16-bit unsigned integer value from the bit stream.

Returns:

Variable 16-bit unsigned integer value.

read_varuint32() int[source]

Reads variable 32-bit unsigned integer value from the bit stream.

Returns:

Variable 32-bit unsigned integer value.

read_varuint64() int[source]

Reads variable 64-bit unsigned integer value from the bit stream.

Returns:

Variable 64-bit unsigned integer value.