Class Bytes
- All Implemented Interfaces:
Proxy
The purpose of a GBytes
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 GBytes
without coordinating
their activities, resting assured that the byte data will not change or
move while they hold a reference.
A GBytes
can come from many different origins that may have
different procedures for freeing the memory region. Examples are
memory from g_malloc(), from memory slices, from a GMappedFile
or
memory from other allocators.
GBytes
work well as keys in GHashTable
. Use g_bytes_equal() and
g_bytes_hash() as parameters to g_hash_table_new() or g_hash_table_new_full().
GBytes
can also be used as keys in a GTree
by passing the g_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 GByteArray
. Use g_bytes_unref_to_array() to create a
mutable array for a GBytes
sequence. To create an immutable GBytes
from
a mutable GByteArray
, use the g_byte_array_free_to_bytes() function.
-
Constructor Summary
ConstructorDescriptionBytes()
CallsBytes(byte[])
with data =null
Bytes
(@org.jetbrains.annotations.Nullable byte[] data) Creates a newGBytes
fromdata
.Bytes
(MemorySegment address) Create a Bytes proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionint
Compares the twoGBytes
values.boolean
Compares the twoGBytes
values being pointed to and returnstrue
if they are equal.byte[]
getData()
Get the byte data in theGBytes
.getRegion
(long elementSize, long offset, long nElements) Gets a pointer to a region in this Bytes.long
getSize()
Get the size of the byte data in theGBytes
.static Type
getType()
Get the GType of the Bytes classint
hash()
Creates an integer hash code for the byte data in theGBytes
.newFromBytes
(long offset, long length) Creates aGBytes
which is a subsection of anotherGBytes
.ref()
Increase the reference count on this Bytes.static Bytes
static_
(@org.jetbrains.annotations.Nullable byte[] data) Creates a newGBytes
from static data.static Bytes
take
(@org.jetbrains.annotations.Nullable byte[] data) Creates a newGBytes
fromdata
.void
unref()
Releases a reference on this Bytes.byte[]
Unreferences the bytes, and returns a new mutableGByteArray
containing the same byte data.byte[]
Unreferences the bytes, and returns a pointer the same byte data contents.static Bytes
withFreeFunc
(@org.jetbrains.annotations.Nullable byte[] data, @Nullable MemorySegment userData) Creates aGBytes
fromdata
.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Bytes
Create a Bytes proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Bytes
public Bytes(@Nullable @org.jetbrains.annotations.Nullable byte[] data) Creates a newGBytes
fromdata
.data
is copied. Ifsize
is 0,data
may benull
.- Parameters:
data
- the data to be used for the bytes
-
Bytes
public Bytes()CallsBytes(byte[])
with data =null
-
-
Method Details
-
getType
-
static_
Creates a newGBytes
from static data.data
must be static (ie: never modified or freed). It may benull
ifsize
is 0.- Parameters:
data
- the data to be used for the bytes- Returns:
- a new
GBytes
-
take
Creates a newGBytes
fromdata
.After this call,
data
belongs to theGBytes
and may no longer be modified by the caller. The memory ofdata
has to be dynamically allocated and will eventually be freed with g_free().For creating
GBytes
with memory from other allocators, see g_bytes_new_with_free_func().data
may benull
ifsize
is 0.- Parameters:
data
- the data to be used for the bytes- Returns:
- a new
GBytes
-
withFreeFunc
public static Bytes withFreeFunc(@Nullable @org.jetbrains.annotations.Nullable byte[] data, @Nullable @Nullable MemorySegment userData) Creates aGBytes
fromdata
.When the last reference is dropped,
freeFunc
will be called with theuserData
argument.data
must not be modified after this call is made untilfreeFunc
has been called to indicate that the bytes is no longer in use.data
may benull
ifsize
is 0.- Parameters:
data
- the data to be used for the bytesuserData
- data to pass tofreeFunc
- Returns:
- a new
GBytes
-
compare
Compares the twoGBytes
values.This function can be used to sort GBytes instances in lexicographical order.
If this Bytes 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 this Bytes has a smaller value at that position it is considered less, otherwise greater thanbytes2
.- Parameters:
bytes2
- a pointer to aGBytes
to compare with this Bytes- Returns:
- a negative value if this Bytes is less than
bytes2
, a positive value if this Bytes is greater thanbytes2
, and zero if this Bytes is equal tobytes2
-
equal
Compares the twoGBytes
values being pointed to and returnstrue
if they are equal.This function can be passed to g_hash_table_new() as the
keyEqualFunc
parameter, when using non-null
GBytes
pointers as keys in aGHashTable
.- Parameters:
bytes2
- a pointer to aGBytes
to compare with this Bytes- Returns:
true
if the two keys match.
-
getData
public byte[] getData()Get the byte data in theGBytes
. This data should not be modified.This function will always return the same pointer for a given
GBytes
.null
may be returned ifsize
is 0. This is not guaranteed, as theGBytes
may represent an empty string withdata
non-null
andsize
as 0.null
will not be returned ifsize
is non-zero.- Returns:
- a pointer to the byte data, or
null
-
getRegion
Gets a pointer to a region in this Bytes.The region starts at
offset
many bytes from the start of the data and containsnElements
many elements ofelementSize
size.nElements
may be zero, butelementSize
must always be non-zero. Ideally,elementSize
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 this Bytes. If the region is in some way out of range, or if an overflow has occurred, thennull
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 this Bytes, plus
offset
. This will be non-null
except for the case where this 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 this Bytes,null
effectively always means "error".- Parameters:
elementSize
- a non-zero element sizeoffset
- an offset to the start of the region within the this BytesnElements
- the number of elements in the region- Returns:
- the requested region, or
null
in case of an error
-
getSize
public long getSize()Get the size of the byte data in theGBytes
.This function will always return the same value for a given
GBytes
.- Returns:
- the size
-
hash
public int hash()Creates an integer hash code for the byte data in theGBytes
.This function can be passed to g_hash_table_new() as the
keyHashFunc
parameter, when using non-null
GBytes
pointers as keys in aGHashTable
.- Returns:
- a hash value corresponding to the key.
-
newFromBytes
Creates aGBytes
which is a subsection of anotherGBytes
. Theoffset
+length
may not be longer than the size of this Bytes.A reference to this Bytes will be held by the newly created
GBytes
until the byte data is no longer needed.Since 2.56, if
offset
is 0 andlength
matches the size of this Bytes, then this Bytes will be returned with the reference count incremented by 1. If this Bytes is a slice of anotherGBytes
, then the resultingGBytes
will reference the sameGBytes
instead of this Bytes. This allows consumers to simplify the usage ofGBytes
when asynchronously writing to streams.- Parameters:
offset
- offset which subsection starts atlength
- length of subsection- Returns:
- a new
GBytes
-
ref
-
unref
public void unref()Releases a reference on this Bytes. This may result in the bytes being freed. If this Bytes isnull
, it will return immediately. -
unrefToArray
public byte[] unrefToArray()Unreferences the bytes, and returns a new mutableGByteArray
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 g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all other cases the data is copied.
Do not use it if this Bytes contains more than
G_MAXUINT
bytes.GByteArray
stores the length of its data inguint
, which may be shorter thangsize
, that this Bytes is using.- Returns:
- a new mutable
GByteArray
containing the same byte data
-
unrefToData
public byte[] unrefToData()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 g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all other cases the data is copied.
- Returns:
- a pointer to the same byte data, which should be freed with g_free()
-