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, size=None, *, factory=<class 'firebird.base.buffer.BytesBufferFactory'>, eof_marker=None, max_size=Sentinel('UNLIMITED'), byteorder=<ByteOrder.LITTLE: 'little'>)¶ Bases:
object
Generic memory buffer manager.
-
__init__
(init, size=None, *, factory=<class 'firebird.base.buffer.BytesBufferFactory'>, eof_marker=None, max_size=Sentinel('UNLIMITED'), byteorder=<ByteOrder.LITTLE: 'little'>)¶ - Parameters
init (
Union
[int
,bytes
]) – Must be an integer which specifies the size of the array, or abytes
object which will be used to initialize the array items.size (
Optional
[int
]) – Size of the array. The argument value is used only wheninit
is abytes
object.factory (
Type
[BufferFactory
]) – Factory object used to create/resize the internal memory buffer.eof_marker (
Optional
[int
]) – Value that indicates the end of data. Could be None.max_size (
Union
[int
,Sentinel
]) – If specified, the buffer couldn’t grow beyond specified number of bytes.byteorder (
ByteOrder
) – The byte order used to read/write numbers.
-
is_eof
()¶ Return True when positioned past the end of buffer or on
eof_marker
(if defined).- Return type
-
read_bytes
()¶ Read content of binary cluster (2 bytes data length followed by data).
- Return type
-
read_pascal_string
(*, encoding='ascii')¶ Read Pascal string (1 byte length followed by string data).
- Return type
-
read_sized_int
(*, signed=False)¶ Read number cluster (2 byte length followed by data).
- Return type
-
read_sized_string
(*, encoding='ascii')¶ Read string (2 byte length followed by data).
- Return type
-
write_number
(value, size, *, signed=False)¶ Write number with specified size (in bytes).
- Return type
-
write_pascal_string
(value, *, encoding='ascii')¶ Write tagged Pascal string (2 byte length followed by data).
- Return type
-
factory
: BufferFactory¶ -
- Type
Buffer factory instance used by manager [default
-
property
last_data
¶ Index of first non-zero byte when searched from the end of buffer.
- Return type
-
Buffer factories¶
Buffer factory protocol¶
-
class
firebird.base.buffer.
BufferFactory
(*args, **kwargs)¶ Bases:
typing.Protocol
BufferFactory Protocol definition
-
clear
(buffer)¶ Fills the buffer with zero.
- Argument:
buffer: A memory buffer previously created by
BufferFactory.create()
method.
- Return type
-
create
(init_or_size, size=None)¶ This function must create and return a mutable character buffer.
-
bytes factory¶
-
class
firebird.base.buffer.
BytesBufferFactory
¶ Bases:
object
Buffer factory for
bytearray
buffers.-
create
(init_or_size, size=None)¶ This function creates a mutable character buffer. The returned object is a
bytearray
.- Parameters
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.
- Return type
-
ctypes factory¶
-
class
firebird.base.buffer.
CTypesBufferFactory
¶ Bases:
object
Buffer factory for
ctypes
array ofc_char
buffers.-
create
(init_or_size, size=None)¶ This function creates a
ctypes
mutable character buffer. The returned object is an array ofctypes.c_char
.- Parameters
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.
- Return type
-