Class (GI Struct)

GLib-2.0GLibBytesSince 2.32

A simple reference counted data type representing an immutable sequence of zero or more bytes from an unspecified origin.

The purpose of a GLib.Bytes is to keep the memory region that it holds alive for as long as anyone holds a reference to the bytes. When the last reference count is dropped, the memory is released. Multiple unrelated callers can use byte data in the GLib.Bytes without coordinating their activities, resting assured that the byte data will not change or move while they hold a reference.

A GLib.Bytes can come from many different origins that may have different procedures for freeing the memory region. Examples are memory from GLib.malloc, from memory slices, from a GLib.MappedFile or memory from other allocators.

GLib.Bytes work well as keys in GLib.HashTable. Use GLib.Bytes.equal and GLib.Bytes.hash as parameters to GLib.HashTable.new or GLib.HashTable.new_full. GLib.Bytes can also be used as keys in a GLib.Tree by passing the GLib.Bytes.compare function to GLib.Tree.new.

The data pointed to by this bytes must not be modified. For a mutable array of bytes see GLib.ByteArray. Use GLib.Bytes.unref_to_array to create a mutable array for a GLib.Bytes sequence. To create an immutable GLib.Bytes from a mutable GLib.ByteArray, use the GLib.ByteArray.free_to_bytes function.

2.32

Index

Constructors

Properties

$gtype: GType<GLib.Bytes>

Methods

  • Compares the two GLib.Bytes values.

    This function can be used to sort GLib.Bytes instances in lexicographical order.

    If bytes1 and bytes2 have different length but the shorter one is a prefix of the longer one then the shorter one is considered to be less than the longer one. Otherwise the first byte where both differ is used for comparison. If bytes1 has a smaller value at that position it is considered less, otherwise greater than bytes2.

    Parameters

    • bytes2: Uint8Array<ArrayBufferLike> | GLib.Bytes

      a pointer to a GLib.Bytes to compare with bytes1

    Returns number

    a negative value if bytes1 is less than bytes2, a positive value if bytes1 is greater than bytes2, and zero if bytes1 is equal to bytes2

  • Compares the two GLib.Bytes values being pointed to and returns TRUE if they are equal.

    This function can be passed to GLib.HashTable.new as the key_equal_func parameter, when using non-NULL GLib.Bytes pointers as keys in a GLib.HashTable.

    Parameters

    • bytes2: Uint8Array<ArrayBufferLike> | GLib.Bytes

      a pointer to a GLib.Bytes to compare with bytes1

    Returns boolean

    TRUE if the two keys match.

  • Get the byte data in the GLib.Bytes.

    This data should not be modified.

    This function will always return the same pointer for a given GLib.Bytes.

    NULL may be returned if size is 0. This is not guaranteed, as the GLib.Bytes may represent an empty string with data non-NULL and size as 0. NULL will not be returned if size is non-zero.

    Returns Uint8Array<ArrayBufferLike>

    a pointer to the byte data

  • Gets a pointer to a region in bytes.

    The region starts at offset many bytes from the start of the data and contains n_elements many elements of element_size size.

    n_elements may be zero, but element_size must always be non-zero. Ideally, element_size is a static constant (eg: sizeof a struct).

    This function does careful bounds checking (including checking for arithmetic overflows) and returns a non-NULL pointer if the specified region lies entirely within the bytes. If the region is in some way out of range, or if an overflow has occurred, then NULL is returned.

    Note: it is possible to have a valid zero-size region. In this case, the returned pointer will be equal to the base pointer of the data of bytes, plus offset. This will be non-NULL except for the case where bytes itself was a zero-sized region. Since it is unlikely that you will be using this function to check for a zero-sized region in a zero-sized bytes, NULL effectively always means ‘error’.

    Parameters

    • element_size: number

      a non-zero element size

    • offset: number

      an offset to the start of the region within the bytes

    • n_elements: number

      the number of elements in the region

    Returns any

    the requested region, or NULL in case of an error

  • Creates an integer hash code for the byte data in the GLib.Bytes.

    This function can be passed to GLib.HashTable.new as the key_hash_func parameter, when using non-NULL GLib.Bytes pointers as keys in a GLib.HashTable.

    Returns number

    a hash value corresponding to the key.

  • Releases a reference on bytes.

    This may result in the bytes being freed. If bytes is NULL, it will return immediately.

    Returns void

  • Unreferences the bytes, and returns a new mutable GLib.ByteArray containing the same byte data.

    As an optimization, the byte data is transferred to the array without copying if this was the last reference to bytes and bytes was created with GLib.Bytes.new, GLib.Bytes.new_take or GLib.ByteArray.free_to_bytes and the buffer was larger than the size GLib.Bytes may internalize within its allocation. In all other cases the data is copied.

    Do not use it if bytes contains more than G_MAXUINT bytes. GLib.ByteArray stores the length of its data in guint, which may be shorter than gsize, that bytes is using.

    Returns Uint8Array

    a new mutable GLib.ByteArray containing the same byte data

  • Unreferences the bytes, and returns a pointer the same byte data contents.

    As an optimization, the byte data is returned without copying if this was the last reference to bytes and bytes was created with GLib.Bytes.new, GLib.Bytes.new_take or GLib.ByteArray.free_to_bytes and the buffer was larger than the size GLib.Bytes may internalize within its allocation. In all other cases the data is copied.

    Returns Uint8Array

    a pointer to the same byte data, which should be freed with GLib.free