buffer - Memory buffer manager¶
Overview¶
This module provides a raw memory buffer manager with convenient methods to read/write data of various data type.
MemoryBuffer¶
- class firebird.base.buffer.MemoryBuffer(init: int | bytes, size: int | None = None, *, factory: ~typing.Type[~firebird.base.buffer.BufferFactory] = <class 'firebird.base.buffer.BytesBufferFactory'>, eof_marker: int | None = None, max_size: int | ~firebird.base.types.Sentinel = Sentinel('UNLIMITED'), byteorder: ~firebird.base.types.ByteOrder = ByteOrder.LITTLE)[source]¶
Bases:
object
Generic memory buffer manager.
- Parameters:
- __init__(init: int | bytes, size: int | None = None, *, factory: ~typing.Type[~firebird.base.buffer.BufferFactory] = <class 'firebird.base.buffer.BytesBufferFactory'>, eof_marker: int | None = None, max_size: int | ~firebird.base.types.Sentinel = Sentinel('UNLIMITED'), byteorder: ~firebird.base.types.ByteOrder = ByteOrder.LITTLE)[source]¶
- Parameters:
init (int | bytes) – Must be an integer which specifies the size of the array, or a
bytes
object which will be used to initialize the array items.size (int | None) – Size of the array. The argument value is used only when
init
is abytes
object.factory (Type[BufferFactory]) – Factory object used to create/resize the internal memory buffer.
eof_marker (int | None) – Value that indicates the end of data. Could be None.
max_size (int | Sentinel) – If specified, the buffer couldn’t grow beyond specified number of bytes.
byteorder (ByteOrder) – The byte order used to read/write numbers.
- clear() None [source]¶
Fills the buffer with zeros and resets the position in buffer to zero.
- Return type:
None
- is_eof() bool [source]¶
Return True when positioned past the end of buffer or on
eof_marker
(if defined).- Return type:
- read_bytes() bytes [source]¶
Read content of binary cluster (2 bytes data length followed by data).
- Return type:
- read_pascal_string(*, encoding: str = 'ascii', errors: str = 'strict') str [source]¶
Read Pascal string (1 byte length followed by string data).
- read_sized_int(*, signed: bool = False) int [source]¶
Read number cluster (2 byte length followed by data).
- read_sized_string(*, encoding: str = 'ascii', errors: str = 'strict') str [source]¶
Read string (2 byte length followed by data).
- read_string(*, encoding: str = 'ascii', errors: str = 'strict') str [source]¶
Read null-terminated string.
- resize(size: int) None [source]¶
Resize buffer to specified length.
- Parameters:
size (int) –
- Return type:
None
- write_bigint(value: int) None [source]¶
Write tagged 8 byte number (c_ulonglong).
- Parameters:
value (int) –
- Return type:
None
- write_int(value: int) None [source]¶
Write 4 byte number (c_uint).
- Parameters:
value (int) –
- Return type:
None
- write_number(value: int, size: int, *, signed: bool = False) None [source]¶
Write number with specified size (in bytes).
- write_pascal_string(value: str, *, encoding: str = 'ascii', errors: str = 'strict') None [source]¶
Write tagged Pascal string (2 byte length followed by data).
- write_short(value: int) None [source]¶
Write 2 byte number (c_ushort).
- Parameters:
value (int) –
- Return type:
None
- write_sized_string(value: str, *, encoding: str = 'ascii', errors: str = 'strict') None [source]¶
Write string (2 byte length followed by data).
- write_string(value: str, *, encoding: str = 'ascii', errors: str = 'strict') None [source]¶
Write zero-terminated string.
- factory: BufferFactory¶
-
- Type:
Buffer factory instance used by manager [default
Buffer factories¶
- class firebird.base.buffer.BufferFactory(*args, **kwargs)[source]¶
Bases:
Protocol
BufferFactory Protocol definition.
- clear(buffer: Any) None [source]¶
Fills the buffer with zero.
- Argument:
buffer: A memory buffer previously created by
BufferFactory.create()
method.
- Parameters:
buffer (Any) –
- Return type:
None
- class firebird.base.buffer.BytesBufferFactory[source]¶
Bases:
object
Buffer factory for
bytearray
buffers.- clear(buffer: bytearray) None [source]¶
Fills the buffer with zero.
- Parameters:
buffer (bytearray) –
- Return type:
None
- create(init_or_size: int | bytes, size: int | None = None) bytearray [source]¶
This function creates a mutable character buffer. The returned object is a
bytearray
.- Parameters:
- Return type:
Important
Although arguments are the same as for
ctypes.create_string_buffer
, the behavior is different when new buffer is initialized from bytes:If there are more bytes than specified
size
, this function copies onlysize
bytes into new buffer. Thecreate_string_buffer
raises an excpetion.Unlike
create_string_buffer
whensize
is NOT specified, the buffer is NOT made one item larger than its length so that the last element in the array is a NUL termination character.
- class firebird.base.buffer.CTypesBufferFactory[source]¶
Bases:
object
Buffer factory for
ctypes
array ofc_char
buffers.- clear(buffer: bytearray, init: int = 0) None [source]¶
Fills the buffer with specified value (default).
- create(init_or_size: int | bytes, size: int | None = None) bytearray [source]¶
This function creates a
ctypes
mutable character buffer. The returned object is an array ofctypes.c_char
.- Parameters:
- Return type:
Important
Although arguments are the same as for
ctypes.create_string_buffer
, the behavior is different when new buffer is initialized from bytes:If there are more bytes than specified
size
, this function copies onlysize
bytes into new buffer. Thecreate_string_buffer
raises an excpetion.Unlike
create_string_buffer
whensize
is NOT specified, the buffer is NOT made one item larger than its length so that the last element in the array is a NUL termination character.