Class Interop
java.lang.Object
org.javagi.interop.Interop
The Interop class contains functionality for interoperability with native
code.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic MemorySegmentallocate(boolean @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Convert a boolean[] array into an int[] array, and callsallocate(int[], boolean, SegmentAllocator).static MemorySegmentallocate(byte @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of bytes.static MemorySegmentallocate(char @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of chars.static MemorySegmentallocate(double @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of doubles.static MemorySegmentallocate(float @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of floats.static MemorySegmentallocate(int @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of floats.static MemorySegmentallocate(long @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of longs.static MemorySegmentallocate(short @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of shorts.static MemorySegmentallocate(@Nullable String @Nullable [] @Nullable [] strvs, boolean zeroTerminated, SegmentAllocator alloc, SegmentAllocator elementAlloc) Allocate and initialize an (optionallyNULL-terminated) array of arrays of strings (a Strv-array).static MemorySegmentallocate(@Nullable String @Nullable [] strings, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of strings (NULL-terminated utf8char*).static MemorySegmentallocate(@Nullable String string, SegmentAllocator alloc) Allocate a native string usingSegmentAllocator.allocateFrom(String), but returnMemorySegment.NULLfor anullargument.static MemorySegmentallocate(@Nullable MemorySegment @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of memory addresses.static <T> MemorySegmentallocate(@Nullable Alias<T> @Nullable [] aliases, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array with the values taken fromAliasvalues, i.e. typedefs of primitive values, Strings or memory segments.static MemorySegmentallocate(@Nullable Filename @Nullable [] filenames, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of filenames (NULL-terminatedchar*).static MemorySegmentallocate(@Nullable Proxy @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of pointers (from Proxy instances).static MemorySegmentallocate(@Nullable Proxy @Nullable [] array, MemoryLayout layout, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of structs (from Proxy instances).static MemorySegmentallocateUnowned(@Nullable String string) Allocate a native string usingg_malloc0().static ArenaattachArena(Arena arena, Object instance) Register a Cleaner action that will close the arena when the instance is garbage-collected, coupling the lifetime of the arena to the lifetime of the instance.static voidcheckNull(@Nullable MemorySegment pointer) Whenpointerisnull, orpointerequalsMemorySegment.NULLor reading an address frompointerreturnsMemorySegment.NULL, raise aNullPointerException.static voidcopy(MemorySegment src, MemorySegment dst, long size) Reinterpret both memory segments to the specified size and copysizebytes fromsrcintodst.static MemorySegmentdereference(@Nullable MemorySegment pointer) Dereference a pointerstatic MethodHandledowncallHandle(MemorySegment symbol, FunctionDescriptor fdesc, Linker.Option... options) Create a method handle that is used to call the native function at the provided memory address.static MethodHandledowncallHandle(String name, FunctionDescriptor fdesc) Convenience method that callsdowncallHandle(String, FunctionDescriptor, boolean)with variadic=false.static MethodHandledowncallHandle(String name, FunctionDescriptor fdesc, boolean variadic) Create a method handle that is used to call the native function with the provided name and function descriptor.static <T extends Enum<T>>
intenumSetToInt(@Nullable Set<T> set) Create a bitfield from the provided Set of enumsstatic voidfreeGBytes(MemorySegment address) Free a GBytes withg_bytes_unref()static voidfreeGString(MemorySegment address) Free a GString (including the character data).static byte @Nullable []fromGBytes(MemorySegment address) Create a Java byte array from a GBytesstatic @Nullable StringfromGString(MemorySegment address, TransferOwnership transfer) Marshal a GString to a Java String.static MemorySegmentgetAddress(@Nullable Object o, SegmentAllocator alloc) Allocate memory for this object.static MemorySegment @Nullable []getAddressArray(MemorySegment address, int length, TransferOwnership transfer) Read an array of pointers with the requested length from native memory.static MemorySegment @Nullable []getAddressArray(MemorySegment address, TransferOwnership transfer) Read an array of pointers from aNULL-terminated array in native memory.static <T> T @Nullable []getArrayFromIntPointer(MemorySegment address, int length, Class<T> cls, Function<Integer, T> make) Read an array of integers from native memory, create a Java instance for each integer value with the provided constructor, and return an array of these instances.static booleangetBoolean(MemorySegment address) Copy a boolean value from native memory.static booleangetBoolean(MemorySegment address, TransferOwnership transfer) Copy a boolean value from native memory.static boolean @Nullable []getBooleanArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of booleans with the requested length from native memory.static bytegetByte(MemorySegment address) Copy a byte value from native memory.static bytegetByte(MemorySegment address, TransferOwnership transfer) Copy a byte value from native memory.static byte @Nullable []getByteArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of bytes with the requested length from native memory.static byte @Nullable []getByteArray(MemorySegment address, TransferOwnership transfer) Read aNULL-terminated array of bytes from native memory.static chargetCharacter(MemorySegment address) Copy a char value from native memory.static chargetCharacter(MemorySegment address, TransferOwnership transfer) Copy a char value from native memory.static char @Nullable []getCharacterArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of chars with the requested length from native memory.static doublegetDouble(MemorySegment address) Copy a double value from native memory.static doublegetDouble(MemorySegment address, TransferOwnership transfer) Copy a double value from native memory.static double @Nullable []getDoubleArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of doubles with the requested length from native memory.static Filename @Nullable []getFilenameArray(MemorySegment address, int length, TransferOwnership transfer) Read an array of Filenames with the requested length from native memory.static Filename @Nullable []getFilenameArray(MemorySegment address, TransferOwnership transfer) Read an array of Filenames from aNULL-terminated array in native memory.static floatgetFloat(MemorySegment address) Copy a float value from native memory.static floatgetFloat(MemorySegment address, TransferOwnership transfer) Copy a float value from native memory.static float @Nullable []getFloatArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of floats with the requested length from native memory.static float @Nullable []getFloatArray(MemorySegment address, TransferOwnership transfer) Read aNULL-terminated array of float from native memory.static intgetInteger(MemorySegment address) Copy an integer value from native memory.static intgetInteger(MemorySegment address, TransferOwnership transfer) Copy an integer value from native memory.static int @Nullable []getIntegerArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of integers with the requested length from native memory.static int @Nullable []getIntegerArray(MemorySegment address, TransferOwnership transfer) Read aNULL-terminated array of integers from native memory.static longgetLong(MemorySegment address) Copy a long value from native memory.static longgetLong(MemorySegment address, TransferOwnership transfer) Copy a long value from native memory.static long @Nullable []getLongArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of longs with the requested length from native memory.static long @Nullable []getLongArray(MemorySegment address, TransferOwnership transfer) Read aNULL-terminated array of longs from native memory.static <T extends Proxy>
T @Nullable []getProxyArray(MemorySegment address, int length, Class<T> cls, Function<MemorySegment, T> make) Read an array of memory addresses from native memory, create a Proxy instance for each address, and return an array of Proxy instances.static <T extends Proxy>
T @Nullable []getProxyArray(MemorySegment address, Class<T> cls, Function<MemorySegment, T> make) Read aNULL-terminated array of memory addresses from native memory, create a Proxy instance for each address, and return an array of Proxy instances.static shortgetShort(MemorySegment address) Copy a short value from native memory.static shortgetShort(MemorySegment address, TransferOwnership transfer) Copy a short value from native memory.static short @Nullable []getShortArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of shorts with the requested length from native memory.static short @Nullable []getShortArray(MemorySegment address, TransferOwnership transfer) Read aNULL-terminated array of shorts from native memory.static @Nullable StringgetString(MemorySegment address) Copy a Java string from native memory usingMemorySegment.getString().static @Nullable StringgetString(MemorySegment address, TransferOwnership transfer) Copy a Java string from native memory usingMemorySegment.getString().static @Nullable String @Nullable []getStringArray(MemorySegment address, int length, TransferOwnership transfer) Read an array of Strings with the requested length from native memory.static @Nullable String @Nullable []getStringArray(MemorySegment address, TransferOwnership transfer) Read an array of Strings from aNULL-terminated array in native memory.static <T extends Proxy>
T @Nullable []getStructArray(MemorySegment address, int length, Class<T> cls, Function<MemorySegment, T> make, MemoryLayout layout) Read an array of structs from native memory, create a Proxy instance for each struct, and return an array of Proxy instances.static <T extends Proxy>
T @Nullable []getStructArray(MemorySegment address, Class<T> cls, Function<MemorySegment, T> make, MemoryLayout layout) Read aNULL-terminated array of structs from native memory, create a Proxy instance for each struct, and return an array of Proxy instances.static @Nullable String @Nullable [] @Nullable []getStrvArray(MemorySegment address, int length, TransferOwnership transfer) Readlengtharrays of Strings from native memory.static @Nullable String @Nullable [] @Nullable []getStrvArray(MemorySegment address, TransferOwnership transfer) ReadNULL-terminated arrays of Strings from aNULL-terminated array in native memory.static @Nullable TypeGet a GType by executing the provided get-type function.static int @Nullable []getValues(Enumeration @Nullable [] array) Convert an array of enums into an array of integers.intToEnumSet(Class<T> cls, Function<Integer, T> make, int bitfield) Create an EnumSet of class `cls` from the provided bitfield.static voidloadLibrary(String name) Load the specified library usingSymbolLookup.libraryLookup(String, Arena)RESTRICTED.static booleanstatic SegmentAllocatorCreate a SegmentAllocator that usesmalloc()to allocate memory.static MemorySegmentnewGArray(int elementSize) Create a new empty GArray.static MemorySegmentnewGArray(MemorySegment data, long length, long elementSize) Create a new GArray with the provided data.static MemorySegmentCreate a new empty GByteArray.static MemorySegmentCreate a new empty GPtrArray.static MemorySegmentnewGPtrArray(MemorySegment data, long length) Create a new empty GPtrArray.static MemorySegmentreinterpret(MemorySegment address, long newSize) ReinterpretaddresstonewSizeiffnewSizeis larger than the current size ofaddress.static booleanNull-safe retrieve the value of a Boolean Outstatic byteNull-safe retrieve the value of a Byte Outstatic chartoCharacter(@Nullable Out<@Nullable Character> val) Null-safe retrieve the value of a Character Outstatic doubleNull-safe retrieve the value of a Double Outstatic floatNull-safe retrieve the value of a Float Outstatic MemorySegmenttoGBytes(byte @Nullable [] data) Create a GBytes from a Java byte arraystatic MemorySegmentMarshal a Java String to a GString.static intNull-safe retrieve the value of an Integer Outstatic longNull-safe retrieve the value of a Long Outstatic shortNull-safe retrieve the value of a Short Outstatic MethodHandleupcallHandle(MethodHandles.Lookup lookup, Class<?> cls, FunctionDescriptor descriptor) Create a method handle for theupcallmethod in the provided class.static MethodHandleupcallHandle(MethodHandles.Lookup lookup, Class<?> cls, String name, FunctionDescriptor descriptor) Create a method handle for a method in the provided class.
-
Constructor Details
-
Interop
public Interop()
-
-
Method Details
-
longAsInt
public static boolean longAsInt() -
loadLibrary
Load the specified library usingSymbolLookup.libraryLookup(String, Arena)RESTRICTED.- Parameters:
name- the name of the library
-
downcallHandle
Convenience method that callsdowncallHandle(String, FunctionDescriptor, boolean)with variadic=false.- Parameters:
name- name of the native functionfdesc- function descriptor of the native function- Returns:
- the MethodHandle
-
downcallHandle
Create a method handle that is used to call the native function with the provided name and function descriptor.- Parameters:
name- name of the native functionfdesc- function descriptor of the native functionvariadic- whether the function has varargs- Returns:
- the newly created MethodHandle
-
downcallHandle
public static MethodHandle downcallHandle(MemorySegment symbol, FunctionDescriptor fdesc, Linker.Option... options) Create a method handle that is used to call the native function at the provided memory address.- Parameters:
symbol- memory address of the native functionfdesc- function descriptor of the native functionoptions- linker options- Returns:
- the newly created MethodHandle
- Throws:
NullPointerException- whensymbolis null
-
upcallHandle
public static MethodHandle upcallHandle(MethodHandles.Lookup lookup, Class<?> cls, FunctionDescriptor descriptor) Create a method handle for theupcallmethod in the provided class.- Parameters:
cls- the callback classdescriptor- the function descriptor for the native function- Returns:
- a method handle to use when creating an upcall stub
-
upcallHandle
public static MethodHandle upcallHandle(MethodHandles.Lookup lookup, Class<?> cls, String name, FunctionDescriptor descriptor) Create a method handle for a method in the provided class.- Parameters:
cls- the callback classname- the name of the callback methoddescriptor- the function descriptor for the native function- Returns:
- a method handle to use when creating an upcall stub
-
mallocAllocator
Create a SegmentAllocator that usesmalloc()to allocate memory.The returned allocator currently calls
GLib.tryMalloc0(long), so the allocated memory is zero-initialized, but they may change in the future.It is up to the user of this allocator to release the allocated memory, for example with
GLib.free(MemorySegment).- Returns:
- the newly created SegmentAllocator
-
attachArena
Register a Cleaner action that will close the arena when the instance is garbage-collected, coupling the lifetime of the arena to the lifetime of the instance.- Parameters:
arena- a memory arena that can be closed (normallyArena.ofConfined()orArena.ofShared().instance- an object- Returns:
- the arena (for method chaining)
-
reinterpret
ReinterpretaddresstonewSizeiffnewSizeis larger than the current size ofaddress.- Parameters:
address- a MemorySegmentnewSize- new size for the MemorySegment- Returns:
- the same MemorySegment reinterpreted to at least
newSize
-
dereference
Dereference a pointer- Parameters:
pointer- the pointer to dereference- Returns:
- the value of the pointer
- Throws:
NullPointerException- when the pointer is null or references null
-
copy
Reinterpret both memory segments to the specified size and copysizebytes fromsrcintodst.- Parameters:
src- source memory segmentdst- destination memory segmentsize- the number of bytes to copy
-
checkNull
Whenpointerisnull, orpointerequalsMemorySegment.NULLor reading an address frompointerreturnsMemorySegment.NULL, raise aNullPointerException.- Parameters:
pointer- the pointer to check- Throws:
NullPointerException- when the check failed
-
getType
-
allocate
Allocate a native string usingSegmentAllocator.allocateFrom(String), but returnMemorySegment.NULLfor anullargument.- Parameters:
string- the string to allocate as a native string (utf8 char*) (can benull)alloc- the segment allocator to use- Returns:
- the allocated MemorySegment with the native utf8 string, or
MemorySegment.NULL
-
allocateUnowned
- Parameters:
string- the string to allocate as a native string (utf8 char*) (can benull)- Returns:
- the allocated MemorySegment with the native utf8 string, that
must be freed with
g_free(), orMemorySegment.NULL
-
getString
Copy a Java string from native memory usingMemorySegment.getString(). If an error occurs or when the native address is NULL, null is returned.The native memory is not freed.
- Parameters:
address- the memory address of the native String (aNULL-terminatedchar*)- Returns:
- a String or null
-
getString
Copy a Java string from native memory usingMemorySegment.getString(). If an error occurs or when the native address is NULL, null is returned.- Parameters:
address- the memory address of the native String (aNULL-terminatedchar*)transfer- ownership transfer- Returns:
- a String or null
-
getBoolean
Copy a boolean value from native memory. If the native address is NULL or contains the value 0, false is returned; else, true is returned.- Parameters:
address- the memory address of the native boolean (0 is false, any other value is true)- Returns:
- the resulting boolean
-
getBoolean
Copy a boolean value from native memory. If the native address is NULL or contains the value 0, false is returned; else, true is returned.- Parameters:
address- the memory address of the native boolean (0 is false, any other value is true)transfer- ownership transfer- Returns:
- the resulting boolean
-
getByte
Copy a byte value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native byte- Returns:
- the resulting byte
-
getByte
Copy a byte value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native bytetransfer- ownership transfer- Returns:
- the resulting byte
-
getCharacter
Copy a char value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native char- Returns:
- the resulting char
-
getCharacter
Copy a char value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native chartransfer- ownership transfer- Returns:
- the resulting char
-
getDouble
Copy a double value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native double- Returns:
- the resulting double
-
getDouble
Copy a double value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native doubletransfer- ownership transfer- Returns:
- the resulting double
-
getFloat
Copy a float value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native float- Returns:
- the resulting float
-
getFloat
Copy a float value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native floattransfer- ownership transfer- Returns:
- the resulting float
-
getInteger
Copy an integer value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native integer- Returns:
- the resulting integer
-
getInteger
Copy an integer value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native integertransfer- ownership transfer- Returns:
- the resulting integer
-
getLong
Copy a long value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native long- Returns:
- the resulting long
-
getLong
Copy a long value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native longtransfer- ownership transfer- Returns:
- the resulting long
-
getShort
Copy a short value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native short- Returns:
- the resulting short
-
getShort
Copy a short value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address- the memory address of the native shorttransfer- ownership transfer- Returns:
- the resulting short
-
getAddress
Allocate memory for this object. Java-GI must be able to marshal the object, which means it must implement theProxyinterface, a primitive type, a String, anEnumeration, aSet<Enumeration>, or an existing MemorySegment (returned as-is).For most primitive types, the value is written in a newly allocated memory segment. Integers however are returned as an address; for the reason why, read the GLib documentation for the
GINT_TO_POINTERandGPOINTER_TO_INTmacros. The same is done for GTypes (for compatibility withGTYPE_TO_POINTERand vice versa).- Parameters:
o- the object to allocate memory foralloc- the memory allocator- Returns:
- the allocated memory holding the object
-
getStringArray
public static @Nullable String @Nullable [] getStringArray(MemorySegment address, int length, TransferOwnership transfer) Read an array of Strings with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of Strings
-
getStringArray
public static @Nullable String @Nullable [] getStringArray(MemorySegment address, TransferOwnership transfer) Read an array of Strings from aNULL-terminated array in native memory.- Parameters:
address- address of the memory segmenttransfer- ownership transfer- Returns:
- array of Strings
-
getFilenameArray
public static Filename @Nullable [] getFilenameArray(MemorySegment address, int length, TransferOwnership transfer) Read an array of Filenames with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of Filenames
-
getFilenameArray
public static Filename @Nullable [] getFilenameArray(MemorySegment address, TransferOwnership transfer) Read an array of Filenames from aNULL-terminated array in native memory.- Parameters:
address- address of the memory segmenttransfer- ownership transfer- Returns:
- array of Filenames
-
getStrvArray
public static @Nullable String @Nullable [] @Nullable [] getStrvArray(MemorySegment address, TransferOwnership transfer) ReadNULL-terminated arrays of Strings from aNULL-terminated array in native memory.- Parameters:
address- address of the memory segmenttransfer- ownership transfer- Returns:
- two-dimensional array of Strings
-
getStrvArray
public static @Nullable String @Nullable [] @Nullable [] getStrvArray(MemorySegment address, int length, TransferOwnership transfer) Readlengtharrays of Strings from native memory.- Parameters:
address- address of the memory segmentlength- the length of the arraytransfer- ownership transfer- Returns:
- two-dimensional array of Strings
-
getAddressArray
public static MemorySegment @Nullable [] getAddressArray(MemorySegment address, int length, TransferOwnership transfer) Read an array of pointers with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of pointers
-
getAddressArray
public static MemorySegment @Nullable [] getAddressArray(MemorySegment address, TransferOwnership transfer) Read an array of pointers from aNULL-terminated array in native memory.- Parameters:
address- address of the memory segmenttransfer- ownership transfer- Returns:
- array of pointers
-
getBooleanArray
public static boolean @Nullable [] getBooleanArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of booleans with the requested length from native memory. The array is read from native memory as an array of integers with value 1 or 0, and converted to booleans with 1 = true and 0 = false.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of booleans
-
getByteArray
public static byte @Nullable [] getByteArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of bytes with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of bytes
-
getByteArray
Read aNULL-terminated array of bytes from native memory.- Parameters:
address- address of the memory segmenttransfer- ownership transfer- Returns:
- array of bytes
-
getCharacterArray
public static char @Nullable [] getCharacterArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of chars with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of chars
-
getDoubleArray
public static double @Nullable [] getDoubleArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of doubles with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of doubles
-
getFloatArray
public static float @Nullable [] getFloatArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of floats with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of floats
-
getFloatArray
Read aNULL-terminated array of float from native memory.- Parameters:
address- address of the memory segmenttransfer- ownership transfer- Returns:
- array of floats
-
getIntegerArray
public static int @Nullable [] getIntegerArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of integers with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of integers
-
getIntegerArray
Read aNULL-terminated array of integers from native memory.- Parameters:
address- address of the memory segmenttransfer- ownership transfer- Returns:
- array of integers
-
getLongArray
public static long @Nullable [] getLongArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of longs with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of longs
-
getLongArray
Read aNULL-terminated array of longs from native memory.- Parameters:
address- address of the memory segmenttransfer- ownership transfer- Returns:
- array of longs
-
getShortArray
public static short @Nullable [] getShortArray(MemorySegment address, long length, TransferOwnership transfer) Read an array of shorts with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arraytransfer- ownership transfer- Returns:
- array of shorts
-
getShortArray
Read aNULL-terminated array of shorts from native memory.- Parameters:
address- address of the memory segmenttransfer- ownership transfer- Returns:
- array of shorts
-
getProxyArray
public static <T extends Proxy> T @Nullable [] getProxyArray(MemorySegment address, Class<T> cls, Function<MemorySegment, T> make) Read aNULL-terminated array of memory addresses from native memory, create a Proxy instance for each address, and return an array of Proxy instances.- Type Parameters:
T- the type of the Proxy instances- Parameters:
address- address of the memory segmentcls- class of the Proxy typemake- constructor of the Proxy type- Returns:
- array of Proxy instances
-
getProxyArray
public static <T extends Proxy> T @Nullable [] getProxyArray(MemorySegment address, int length, Class<T> cls, Function<MemorySegment, T> make) Read an array of memory addresses from native memory, create a Proxy instance for each address, and return an array of Proxy instances.- Type Parameters:
T- the type of the Proxy instances- Parameters:
address- address of the memory segmentlength- length of the arraycls- class of the Proxy typemake- constructor of the Proxy type- Returns:
- array of Proxy instances
-
getStructArray
public static <T extends Proxy> T @Nullable [] getStructArray(MemorySegment address, Class<T> cls, Function<MemorySegment, T> make, MemoryLayout layout) Read aNULL-terminated array of structs from native memory, create a Proxy instance for each struct, and return an array of Proxy instances. The array must be terminated by a completely null-filled struct.- Type Parameters:
T- the type of the Proxy instances- Parameters:
address- address of the memory segmentcls- class of the Proxy typemake- constructor of the Proxy type- Returns:
- array of Proxy instances
-
getStructArray
public static <T extends Proxy> T @Nullable [] getStructArray(MemorySegment address, int length, Class<T> cls, Function<MemorySegment, T> make, MemoryLayout layout) Read an array of structs from native memory, create a Proxy instance for each struct, and return an array of Proxy instances.- Type Parameters:
T- the type of the Proxy instances- Parameters:
address- address of the memory segmentlength- length of the arraycls- class of the Proxy typemake- constructor of the Proxy type- Returns:
- array of Proxy instances
-
getArrayFromIntPointer
public static <T> T @Nullable [] getArrayFromIntPointer(MemorySegment address, int length, Class<T> cls, Function<Integer, T> make) Read an array of integers from native memory, create a Java instance for each integer value with the provided constructor, and return an array of these instances. This is used to create an array of Enumeration or Bitfield objects from a native integer array.- Type Parameters:
T- the type to construct- Parameters:
address- address of the memory segmentlength- length of the arraycls- class that will be returned in the arraymake- constructor to create the instances- Returns:
- array of constructed instances
-
allocate
public static <T> MemorySegment allocate(@Nullable Alias<T> @Nullable [] aliases, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array with the values taken fromAliasvalues, i.e. typedefs of primitive values, Strings or memory segments.- Parameters:
aliases- array of aliased valueszeroTerminated- whether to add aNULLto the arrayalloc- the segment allocator for the array- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(@Nullable String @Nullable [] strings, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of strings (NULL-terminated utf8char*).- Parameters:
strings- array of StringszeroTerminated- whether to add aNULLto the arrayalloc- the segment allocator for the array- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(@Nullable Filename @Nullable [] filenames, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of filenames (NULL-terminatedchar*).- Parameters:
filenames- array of FilenameszeroTerminated- whether to add aNULLto the arrayalloc- the segment allocator for the array- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(@Nullable String @Nullable [] @Nullable [] strvs, boolean zeroTerminated, SegmentAllocator alloc, SegmentAllocator elementAlloc) Allocate and initialize an (optionallyNULL-terminated) array of arrays of strings (a Strv-array).- Parameters:
strvs- the array of String arrayszeroTerminated- whether to add aNULLto the array. The embedded arrays are alwaysNULL-terminated.alloc- the segment allocator for the arrayelementAlloc- the segment allocator for the array elements- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(boolean @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Convert a boolean[] array into an int[] array, and callsallocate(int[], boolean, SegmentAllocator). Each boolean value "true" is converted 1, boolean value "false" to 0.- Parameters:
array- array of booleanszeroTerminated- when true, an (int) 0 is appended to the arrayalloc- the segment allocator for memory allocation- Returns:
- The memory segment of the native array
-
allocate
public static MemorySegment allocate(byte @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of bytes.- Parameters:
array- array of byteszeroTerminated- when true, a (byte) 0 is appended to the arrayalloc- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(char @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of chars.- Parameters:
array- array of charszeroTerminated- when true, a (char) 0 is appended to the arrayalloc- the segment allocator for memory allocation- Returns:
- whe memory segment of the native array
-
allocate
public static MemorySegment allocate(double @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of doubles.- Parameters:
array- array of doubleszeroTerminated- when true, a (double) 0 is appended to the arrayalloc- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(float @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of floats.- Parameters:
array- array of floatszeroTerminated- when true, a (float) 0 is appended to the arrayalloc- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(int @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of floats.- Parameters:
array- array of floatszeroTerminated- when true, a (int) 0 is appended to the arrayalloc- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
newGArray
Create a new empty GArray.- Parameters:
elementSize- element size- Returns:
- the newly created GArray
-
newGArray
Create a new GArray with the provided data.- Parameters:
data- memory segment containing the array datalength- number of array elementselementSize- element size- Returns:
- the newly created GArray
-
newGByteArray
Create a new empty GByteArray.- Returns:
- the newly create GByteArray
-
newGPtrArray
Create a new empty GPtrArray.- Returns:
- the newly create GPtrArray
-
newGPtrArray
Create a new empty GPtrArray.- Returns:
- the newly create GPtrArray
-
allocate
public static MemorySegment allocate(long @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of longs.- Parameters:
array- array of longszeroTerminated- when true, a (long) 0 is appended to the arrayalloc- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(short @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of shorts.- Parameters:
array- array of shortszeroTerminated- when true, a (short) 0 is appended to the arrayalloc- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(@Nullable Proxy @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of pointers (from Proxy instances).- Parameters:
array- array of Proxy instanceszeroTerminated- whether to add aNULLto the arrayalloc- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(@Nullable Proxy @Nullable [] array, MemoryLayout layout, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of structs (from Proxy instances). The actual struct contents (not the pointers) are copied into the array.- Parameters:
array- array of Proxy instanceslayout- the memory layout of the structzeroTerminated- whether to terminate the array by a struct with all members beingNULLalloc- the allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocate
public static MemorySegment allocate(@Nullable MemorySegment @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of memory addresses.- Parameters:
array- array of MemorySegmentszeroTerminated- whether to add aNULLto the arrayalloc- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
intToEnumSet
public static <T extends Enum<T>> EnumSet<T> intToEnumSet(Class<T> cls, Function<Integer, T> make, int bitfield) Create an EnumSet of class `cls` from the provided bitfield. Undefined flags are logged (with levelLogLevelFlags.LEVEL_WARNING) and ignored.- Type Parameters:
T- an enum implementing the Java-GI Enumeration interface- Parameters:
cls- the class of the enummake- function that will construct an enum from one flag valuebitfield- the integer containing the bitfield- Returns:
- an EnumSet containing the enum values as set in the bitfield
-
enumSetToInt
-
getValues
Convert an array of enums into an array of integers.- Parameters:
array- an array of enums- Returns:
- an array containing the integer values of the provided Enumeration instances
-
toGBytes
Create a GBytes from a Java byte array- Parameters:
data- the Java byte array- Returns:
- the GBytes
-
fromGBytes
Create a Java byte array from a GBytes- Parameters:
address- the memory address of the GBytes- Returns:
- the Java byte array
-
freeGBytes
Free a GBytes withg_bytes_unref()- Parameters:
address- the address of the GBytes to free
-
fromGString
Marshal a GString to a Java String.- Parameters:
address- address of a GStringtransfer- if not NONE, the GString is freed- Returns:
- the Java String
-
toGString
Marshal a Java String to a GString.- Parameters:
string- a Java String- Returns:
- the address of the GString
-
freeGString
Free a GString (including the character data).- Parameters:
address- address of a GString
-
toBoolean
-
toByte
-
toCharacter
-
toDouble
-
toFloat
-
toInteger
-
toLong
-
toShort
-