ByteArray
- class ByteArray(*args, **kwargs)
- Constructors:
ByteArray()
Methods
- class ByteArray
- append(array: Sequence[int], data: int, len: int) bytes
Adds the given bytes to the end of the
GByteArray
. The array will grow in size automatically if necessary.- Parameters:
array – a
GByteArray
data – the byte data to be added
len – the number of bytes to add
- free(array: Sequence[int], free_segment: bool) int
Frees the memory allocated by the
GByteArray
. Iffree_segment
isTrue
it frees the actual byte data. If the reference count ofarray
is greater than one, theGByteArray
wrapper is preserved but the size ofarray
will be set to zero.- Parameters:
array – a
GByteArray
free_segment – if
True
the actual byte data is freed as well
- free_to_bytes(array: Sequence[int]) Bytes
Transfers the data from the
GByteArray
into a new immutableBytes
.The
GByteArray
is freed unless the reference count ofarray
is greater than one, theGByteArray
wrapper is preserved but the size ofarray
will be set to zero.This is identical to using
new_take()
andfree()
together.Added in version 2.32.
- Parameters:
array – a
GByteArray
- new_take(data: Sequence[int]) bytes
Creates a byte array containing the
data
. After this call,data
belongs to theGByteArray
and may no longer be modified by the caller. The memory ofdata
has to be dynamically allocated and will eventually be freed withfree()
.Do not use it if
len
is greater than%G_MAXUINT
.GByteArray
stores the length of its data inguint
, which may be shorter thangsize
.Added in version 2.32.
- Parameters:
data – byte data for the array
- prepend(array: Sequence[int], data: int, len: int) bytes
Adds the given data to the start of the
GByteArray
. The array will grow in size automatically if necessary.- Parameters:
array – a
GByteArray
data – the byte data to be added
len – the number of bytes to add
- remove_index(array: Sequence[int], index_: int) bytes
Removes the byte at the given index from a
GByteArray
. The following bytes are moved down one place.- Parameters:
array – a
GByteArray
index – the index of the byte to remove
- remove_index_fast(array: Sequence[int], index_: int) bytes
Removes the byte at the given index from a
GByteArray
. The last element in the array is used to fill in the space, so this function does not preserve the order of theGByteArray
. But it is faster thanremove_index()
.- Parameters:
array – a
GByteArray
index – the index of the byte to remove
- remove_range(array: Sequence[int], index_: int, length: int) bytes
Removes the given number of bytes starting at the given index from a
GByteArray
. The following elements are moved to close the gap.Added in version 2.4.
- Parameters:
array – a
GByteArray
index – the index of the first byte to remove
length – the number of bytes to remove
- set_size(array: Sequence[int], length: int) bytes
Sets the size of the
GByteArray
, expanding it if necessary.- Parameters:
array – a
GByteArray
length – the new size of the
GByteArray
- sized_new(reserved_size: int) bytes
Creates a new
GByteArray
withreserved_size
bytes preallocated. This avoids frequent reallocation, if you are going to add many bytes to the array. Note however that the size of the array is still 0.- Parameters:
reserved_size – number of bytes preallocated
- sort(array: Sequence[int], compare_func: Callable[[None, None], int]) None
Sorts a byte array, using
compare_func
which should be a qsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater than second arg).If two array elements compare equal, their order in the sorted array is undefined. If you want equal elements to keep their order (i.e. you want a stable sort) you can write a comparison function that, if two elements would otherwise compare equal, compares them by their addresses.
- Parameters:
array – a
GByteArray
compare_func – comparison function