hbutils.binary.buffer

CBufferType

class hbutils.binary.buffer.CBufferType(size: int)[source]
Overview:

Bytes type.

__init__(size: int)[source]

Constructor of CBufferType.

Parameters:

size – Size of the buffer.

read(file: BinaryIO) → bytes[source]

Read bytes value.

Parameters:

file – Binary file, io.BytesIO is supported as well.

Returns:

Bytes value.

property size

Size of the buffer.

write(file: BinaryIO, val: Union[bytearray, bytes])[source]

Write bytes value to binary IO object. If the length is not enough, \\ will be filled afterwards to reach the given size.

Parameters:
  • file – Binary file, io.BytesIO is supported as well.

  • val – Bytes value to write.

c_buffer

hbutils.binary.buffer.c_buffer(size: int)hbutils.binary.buffer.CBufferType[source]
Overview:

Reading and writing bytes with given size.

Parameters:

size – Size of the buffer.

Returns:

CBufferType object.

Examples::
>>> import io
>>> from hbutils.binary import c_buffer
>>>
>>> with io.BytesIO(b'\xde\xad\xbe\xef\x00\x12\x34\x56\x78\x00') as file:
...     print(c_buffer(1).read(file))
...     print(c_buffer(2).read(file))
...     print(c_buffer(3).read(file))
...     print(c_buffer(4).read(file))
b'\xde'
b'\xad\xbe'
b'\xef\x00\x12'
b'4Vx\x00'
>>> with io.BytesIO() as file:
...     c_buffer(1).write(file, b'\xde')
...     c_buffer(2).write(file, b'\xad\xbe')
...     c_buffer(3).write(file, b'\xef\x00\x12')
...     c_buffer(4).write(file, b'4Vx')  # length is 3
...     print(file.getvalue())
b'\xde\xad\xbe\xef\x00\x124Vx\x00'