Package org.gnome.glib
Class ByteArray
java.lang.Object
io.github.jwharm.javagi.base.ProxyInstance
org.gnome.glib.ByteArray
-
Constructor Summary
ConstructorDescriptionAllocate a new ByteArray.Allocate a new ByteArray.ByteArray
(MemorySegment address) Create a ByteArray proxy instance for the provided memory address.ByteArray
(MemorySegment data, int len) Allocate a new ByteArray with the fields set to the provided values.ByteArray
(MemorySegment data, int len, Arena arena) Allocate a new ByteArray with the fields set to the provided values. -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
append
(byte[] array, MemorySegment data, int len) Adds the given bytes to the end of theGByteArray
.static MemorySegment
free
(byte[] array, boolean freeSegment) Frees the memory allocated by theGByteArray
.static Bytes
freeToBytes
(byte[] array) Transfers the data from theGByteArray
into a new immutableGBytes
.static MemoryLayout
The memory layout of the native struct.static Type
getType()
Get the GType of the ByteArray classstatic byte[]
new_()
Creates a newGByteArray
with a reference count of 1.static byte[]
newTake
(byte[] data) Creates a byte array containing thedata
.static byte[]
prepend
(byte[] array, MemorySegment data, int len) Adds the given data to the start of theGByteArray
.readData()
Read the value of the fielddata
.int
readLen()
Read the value of the fieldlen
.static byte[]
ref
(byte[] array) Atomically increments the reference count ofarray
by one.static byte[]
removeIndex
(byte[] array, int index) Removes the byte at the given index from aGByteArray
.static byte[]
removeIndexFast
(byte[] array, int index) Removes the byte at the given index from aGByteArray
.static byte[]
removeRange
(byte[] array, int index, int length) Removes the given number of bytes starting at the given index from aGByteArray
.static byte[]
setSize
(byte[] array, int length) Sets the size of theGByteArray
, expanding it if necessary.static byte[]
sizedNew
(int reservedSize) Creates a newGByteArray
withreservedSize
bytes preallocated.static void
sort
(byte[] array, CompareFunc compareFunc) Sorts a byte array, usingcompareFunc
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).static void
sortWithData
(byte[] array, CompareDataFunc compareFunc) Like g_byte_array_sort(), but the comparison function takes an extra user data argument.static MemorySegment
Frees the data in the array and resets the size to zero, while the underlying array is preserved for use elsewhere and returned to the caller.static void
unref
(byte[] array) Atomically decrements the reference count ofarray
by one.void
writeData
(MemorySegment data) Write a value in the fielddata
.void
writeLen
(int len) Write a value in the fieldlen
.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
ByteArray
Create a ByteArray proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
ByteArray
Allocate a new ByteArray.- Parameters:
arena
- to control the memory allocation scope
-
ByteArray
public ByteArray()Allocate a new ByteArray. The memory is allocated withArena.ofAuto()
. -
ByteArray
Allocate a new ByteArray with the fields set to the provided values.- Parameters:
data
- value for the fielddata
len
- value for the fieldlen
arena
- to control the memory allocation scope
-
ByteArray
Allocate a new ByteArray with the fields set to the provided values. The memory is allocated withArena.ofAuto()
.- Parameters:
data
- value for the fielddata
len
- value for the fieldlen
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readData
Read the value of the fielddata
.- Returns:
- The value of the field
data
-
writeData
Write a value in the fielddata
.- Parameters:
data
- The new value for the fielddata
-
readLen
public int readLen()Read the value of the fieldlen
.- Returns:
- The value of the field
len
-
writeLen
public void writeLen(int len) Write a value in the fieldlen
.- Parameters:
len
- The new value for the fieldlen
-
append
Adds the given bytes to the end of theGByteArray
. The array will grow in size automatically if necessary.- Parameters:
array
- aGByteArray
data
- the byte data to be addedlen
- the number of bytes to add- Returns:
- the
GByteArray
-
free
Frees the memory allocated by theGByteArray
. IffreeSegment
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
- aGByteArray
freeSegment
- iftrue
the actual byte data is freed as well- Returns:
- the element data if
freeSegment
isfalse
, otherwisenull
. The element data should be freed using g_free().
-
freeToBytes
Transfers the data from theGByteArray
into a new immutableGBytes
.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 g_bytes_new_take() and g_byte_array_free() together.
- Parameters:
array
- aGByteArray
- Returns:
- a new immutable
GBytes
representing same byte data that was in the array
-
new_
public static byte[] new_()Creates a newGByteArray
with a reference count of 1.- Returns:
- the new
GByteArray
-
newTake
public static byte[] newTake(byte[] data) Creates a byte array containing thedata
. 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 with g_free().Do not use it if
len
is greater thanG_MAXUINT
.GByteArray
stores the length of its data inguint
, which may be shorter thangsize
.- Parameters:
data
- byte data for the array- Returns:
- a new
GByteArray
-
prepend
Adds the given data to the start of theGByteArray
. The array will grow in size automatically if necessary.- Parameters:
array
- aGByteArray
data
- the byte data to be addedlen
- the number of bytes to add- Returns:
- the
GByteArray
-
ref
public static byte[] ref(byte[] array) Atomically increments the reference count ofarray
by one. This function is thread-safe and may be called from any thread.- Parameters:
array
- AGByteArray
- Returns:
- The passed in
GByteArray
-
removeIndex
public static byte[] removeIndex(byte[] array, int index) Removes the byte at the given index from aGByteArray
. The following bytes are moved down one place.- Parameters:
array
- aGByteArray
index
- the index of the byte to remove- Returns:
- the
GByteArray
-
removeIndexFast
public static byte[] removeIndexFast(byte[] array, int index) Removes the byte at the given index from aGByteArray
. 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 than g_byte_array_remove_index().- Parameters:
array
- aGByteArray
index
- the index of the byte to remove- Returns:
- the
GByteArray
-
removeRange
public static byte[] removeRange(byte[] array, int index, int length) Removes the given number of bytes starting at the given index from aGByteArray
. The following elements are moved to close the gap.- Parameters:
array
- aGByteArray
index
- the index of the first byte to removelength
- the number of bytes to remove- Returns:
- the
GByteArray
-
setSize
public static byte[] setSize(byte[] array, int length) Sets the size of theGByteArray
, expanding it if necessary.- Parameters:
array
- aGByteArray
length
- the new size of theGByteArray
- Returns:
- the
GByteArray
-
sizedNew
public static byte[] sizedNew(int reservedSize) Creates a newGByteArray
withreservedSize
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:
reservedSize
- number of bytes preallocated- Returns:
- the new
GByteArray
-
sort
Sorts a byte array, usingcompareFunc
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
- aGByteArray
compareFunc
- comparison function
-
sortWithData
Like g_byte_array_sort(), but the comparison function takes an extra user data argument.- Parameters:
array
- aGByteArray
compareFunc
- comparison function
-
steal
Frees the data in the array and resets the size to zero, while the underlying array is preserved for use elsewhere and returned to the caller.- Parameters:
array
- aGByteArray
.len
- pointer to retrieve the number of elements of the original array- Returns:
- the element data, which should be freed using g_free().
-
unref
public static void unref(byte[] array) Atomically decrements the reference count ofarray
by one. If the reference count drops to 0, all memory allocated by the array is released. This function is thread-safe and may be called from any thread.- Parameters:
array
- AGByteArray
-