:right-sidebar: True ByteArray =================================================================== .. currentmodule:: gi.repository.GLib .. class:: ByteArray(*args, **kwargs) :no-contents-entry: Contains the public fields of a ``GByteArray``\. Methods ------- .. rst-class:: interim-class .. class:: ByteArray :no-index: .. classmethod:: append(array: list[int], data: list[int]) -> list[int] Adds the given bytes to the end of the ``GByteArray``\. The array will grow in size automatically if necessary. :param array: a byte array :param data: the byte data to be added .. classmethod:: free(array: list[int], free_segment: bool) -> list[int] | None Frees the memory allocated by the ``GByteArray``\. If ``free_segment`` is true it frees the actual byte data. If the reference count of ``array`` is greater than one, the ``GByteArray`` wrapper is preserved but the size of ``array`` will be set to zero. :param array: a byte array :param free_segment: if true, the actual byte data is freed as well .. classmethod:: free_to_bytes(array: list[int]) -> ~gi.repository.GLib.Bytes Transfers the data from the ``GByteArray`` into a new immutable :obj:`~gi.repository.GLib.Bytes`\. The ``GByteArray`` is freed unless the reference count of ``array`` is greater than one, in which the ``GByteArray`` wrapper is preserved but the size of ``array`` will be set to zero. This is identical to using :obj:`~gi.repository.GLib.Bytes.new_take` and :obj:`~gi.repository.GLib.ByteArray.free` together. .. versionadded:: 2.32 :param array: a byte array .. classmethod:: new() -> list[int] Creates a new ``GByteArray`` with a reference count of 1. .. classmethod:: new_take(data: list[int]) -> list[int] Creates a byte array containing the ``data``\. After this call, ``data`` belongs to the ``GByteArray`` and may no longer be modified by the caller. The memory of ``data`` has to be dynamically allocated and will eventually be freed with :obj:`~gi.repository.GLib.free`\. Do not use it if ``len`` is greater than ```G_MAXUINT`` `__\. ``GByteArray`` stores the length of its data in ``guint``\, which may be shorter than ``gsize``\. .. versionadded:: 2.32 :param data: the byte data for the array .. classmethod:: prepend(array: list[int], data: list[int]) -> list[int] Adds the given data to the start of the ``GByteArray``\. The array will grow in size automatically if necessary. :param array: a byte array :param data: the byte data to be added .. classmethod:: remove_index(array: list[int], index_: int) -> list[int] Removes the byte at the given index from a ``GByteArray``\. The following bytes are moved down one place. :param array: a byte array :param index_: the index of the byte to remove .. classmethod:: remove_index_fast(array: list[int], index_: int) -> list[int] 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 the ``GByteArray``\. But it is faster than :obj:`~gi.repository.GLib.ByteArray.remove_index`\. :param array: a byte array :param index_: the index of the byte to remove .. classmethod:: remove_range(array: list[int], index_: int, length: int) -> list[int] Removes the given number of bytes starting at the given index from a ``GByteArray``\. The following elements are moved to close the gap. .. versionadded:: 2.4 :param array: a byte array :param index_: the index of the first byte to remove :param length: the number of bytes to remove .. classmethod:: set_size(array: list[int], length: int) -> list[int] Sets the size of the ``GByteArray``\, expanding it if necessary. :param array: a byte array :param length: the new size of the ``GByteArray`` .. classmethod:: sized_new(reserved_size: int) -> list[int] Creates a new ``GByteArray`` with ``reserved_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. :param reserved_size: the number of bytes preallocated .. classmethod:: sort(array: list[int], compare_func: ~collections.abc.Callable[[~typing.Any, ~typing.Any], 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. :param array: a byte array :param compare_func: the comparison function .. classmethod:: sort_with_data(array: list[int], compare_func: ~collections.abc.Callable[[~typing.Any, ~typing.Any, ~typing.Any], int], user_data: ~typing.Any = None) -> None Like :obj:`~gi.repository.GLib.ByteArray.sort`\, but the comparison function takes an extra user data argument. :param array: a byte array :param compare_func: the comparison function :param user_data: the data to pass to ``compare_func`` Fields ------ .. rst-class:: interim-class .. class:: ByteArray :no-index: .. attribute:: data A pointer to the element data. The data may be moved as elements are added to the ``GByteArray`` .. attribute:: len The number of elements in the ``GByteArray``