GLib.Bytes¶
Fields¶
None
Methods¶
| class | new (data) |
| class | new_take (data) |
compare (bytes2) |
|
equal (bytes2) |
|
get_data () |
|
get_size () |
|
hash () |
|
new_from_bytes (offset, length) |
|
ref () |
|
unref () |
|
unref_to_array () |
|
unref_to_data () |
Details¶
-
class
GLib.Bytes¶ A simple refcounted data type representing an immutable sequence of zero or more bytes from an unspecified origin.
The purpose of a
GLib.Bytesis 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 theGLib.Byteswithout coordinating their activities, resting assured that the byte data will not change or move while they hold a reference.A
GLib.Bytescan come from many different origins that may have different procedures for freeing the memory region. Examples are memory fromGLib.malloc(), from memory slices, from aGLib.MappedFileor memory from other allocators.GLib.Byteswork well as keys inGLib.HashTable. UseGLib.Bytes.equal() andGLib.Bytes.hash() as parameters to g_hash_table_new() or g_hash_table_new_full().GLib.Bytescan also be used as keys in aGLib.Treeby passing theGLib.Bytes.compare() function to g_tree_new().The data pointed to by this bytes must not be modified. For a mutable array of bytes see
GLib.ByteArray. UseGLib.Bytes.unref_to_array() to create a mutable array for aGLib.Bytessequence. To create an immutableGLib.Bytesfrom a mutableGLib.ByteArray, use theGLib.ByteArray.free_to_bytes() function.New in version 2.32.
-
classmethod
new(data)[source]¶ Parameters: data ( bytesorNone) – the data to be used for the bytesReturns: a new GLib.BytesReturn type: GLib.BytesCreates a new
GLib.Bytesfrom data.data is copied. If size is 0, data may be
None.New in version 2.32.
-
classmethod
new_take(data)[source]¶ Parameters: data ( bytesorNone) – the data to be used for the bytesReturns: a new GLib.BytesReturn type: GLib.BytesCreates a new
GLib.Bytesfrom data.After this call, data belongs to the bytes and may no longer be modified by the caller.
GLib.free() will be called on data when the bytes is no longer in use. Because of this data must have been created by a call toGLib.malloc(),GLib.malloc0() orGLib.realloc() or by one of the many functions that wrap these calls (such as g_new(),GLib.strdup(), etc).For creating
GLib.Byteswith memory from other allocators, see g_bytes_new_with_free_func().data may be
Noneif size is 0.New in version 2.32.
-
compare(bytes2)[source]¶ Parameters: bytes2 ( GLib.Bytes) – a pointer to aGLib.Bytesto compare with selfReturns: a negative value if self is less than bytes2, a positive value if self is greater than bytes2, and zero if self is equal to bytes2 Return type: intCompares the two
GLib.Bytesvalues.This function can be used to sort
GLib.Bytesinstances in lexicographical order.If self 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 self has a smaller value at that position it is considered less, otherwise greater than bytes2.
New in version 2.32.
-
equal(bytes2)[source]¶ Parameters: bytes2 ( GLib.Bytes) – a pointer to aGLib.Bytesto compare with selfReturns: Trueif the two keys match.Return type: boolCompares the two
GLib.Bytesvalues being pointed to and returnsTrueif they are equal.This function can be passed to g_hash_table_new() as the key_equal_func parameter, when using non-
NoneGLib.Bytespointers as keys in aGLib.HashTable.New in version 2.32.
-
get_data()[source]¶ Returns: a pointer to the byte data, or NoneReturn type: bytesorNoneGet 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.Nonemay be returned if size is 0. This is not guaranteed, as theGLib.Bytesmay represent an empty string with data non-Noneand size as 0.Nonewill not be returned if size is non-zero.New in version 2.32.
-
get_size()[source]¶ Returns: the size Return type: intGet the size of the byte data in the
GLib.Bytes.This function will always return the same value for a given
GLib.Bytes.New in version 2.32.
-
hash()[source]¶ Returns: a hash value corresponding to the key. Return type: intCreates an integer hash code for the byte data in the
GLib.Bytes.This function can be passed to g_hash_table_new() as the key_hash_func parameter, when using non-
NoneGLib.Bytespointers as keys in aGLib.HashTable.New in version 2.32.
-
new_from_bytes(offset, length)[source]¶ Parameters: Returns: a new
GLib.BytesReturn type: Creates a
GLib.Byteswhich is a subsection of anotherGLib.Bytes. The offset + length may not be longer than the size of self.A reference to self will be held by the newly created
GLib.Bytesuntil the byte data is no longer needed.Since 2.56, if offset is 0 and length matches the size of self, then self will be returned with the reference count incremented by 1. If self is a slice of another
GLib.Bytes, then the resultingGLib.Byteswill reference the sameGLib.Bytesinstead of self. This allows consumers to simplify the usage ofGLib.Byteswhen asynchronously writing to streams.New in version 2.32.
-
ref()[source]¶ Returns: the GLib.BytesReturn type: GLib.BytesIncrease the reference count on self.
New in version 2.32.
-
unref()[source]¶ Releases a reference on self. This may result in the bytes being freed. If self is
None, it will return immediately.New in version 2.32.
-
unref_to_array()[source]¶ Returns: a new mutable GLib.ByteArraycontaining the same byte dataReturn type: bytesUnreferences the bytes, and returns a new mutable
GLib.ByteArraycontaining 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() orGLib.ByteArray.free_to_bytes(). In all other cases the data is copied.New in version 2.32.
-
unref_to_data()[source]¶ Returns: a pointer to the same byte data, which should be freed with GLib.free()Return type: bytesUnreferences 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() orGLib.ByteArray.free_to_bytes(). In all other cases the data is copied.New in version 2.32.
-
classmethod