Package org.javagi.interop
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 MemorySegmentallocateNativeArray(boolean @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Convert a boolean[] array into an int[] array, and callsallocateNativeArray(int[], boolean, SegmentAllocator).static MemorySegmentallocateNativeArray(byte @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of bytes.static MemorySegmentallocateNativeArray(char @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of chars.static MemorySegmentallocateNativeArray(double @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of doubles.static MemorySegmentallocateNativeArray(float @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of floats.static MemorySegmentallocateNativeArray(int @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of floats.static MemorySegmentallocateNativeArray(long @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of longs.static MemorySegmentallocateNativeArray(short @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of shorts.static MemorySegmentallocateNativeArray(@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 MemorySegmentallocateNativeArray(@Nullable String @Nullable [] strings, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of strings (NULL-terminated utf8char*).static MemorySegmentallocateNativeArray(@Nullable MemorySegment @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of memory addresses.static MemorySegmentallocateNativeArray(@Nullable Proxy @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of pointers (from Proxy instances).static MemorySegmentallocateNativeArray(@Nullable Proxy @Nullable [] array, MemoryLayout layout, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL-terminated) array of structs (from Proxy instances).static MemorySegmentallocateNativeString(@Nullable String string, SegmentAllocator alloc) Allocate a native string usingSegmentAllocator.allocateFrom(String), but returnMemorySegment.NULLfor anullargument.static MemorySegmentallocateUnownedString(@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 intenumSetToInt(@Nullable Set<? extends Enumeration> 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 ObjectfunctionNotFound(Object[] args) A MethodHandle to this function is returned when a downcall handle was requested for a foreign function that was not found by the linker.static MemorySegmentgetAddress(@Nullable Object o, SegmentAllocator alloc) Allocate memory for this object.static MemorySegment @Nullable []getAddressArrayFrom(MemorySegment address, int length, TransferOwnership transfer) Read an array of pointers with the requested length from native memory.static MemorySegment @Nullable []getAddressArrayFrom(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 boolean @Nullable []getBooleanArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of booleans with the requested length from native memory.static booleangetBooleanFrom(MemorySegment address) Copy a boolean value from native memory.static booleangetBooleanFrom(MemorySegment address, TransferOwnership transfer) Copy a boolean value from native memory.static byte @Nullable []getByteArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of bytes with the requested length from native memory.static byte @Nullable []getByteArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of bytes from native memory.static bytegetByteFrom(MemorySegment address) Copy a byte value from native memory.static bytegetByteFrom(MemorySegment address, TransferOwnership transfer) Copy a byte value from native memory.static char @Nullable []getCharacterArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of chars with the requested length from native memory.static chargetCharacterFrom(MemorySegment address) Copy a char value from native memory.static chargetCharacterFrom(MemorySegment address, TransferOwnership transfer) Copy a char value from native memory.static double @Nullable []getDoubleArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of doubles with the requested length from native memory.static doublegetDoubleFrom(MemorySegment address) Copy a double value from native memory.static doublegetDoubleFrom(MemorySegment address, TransferOwnership transfer) Copy a double value from native memory.static float @Nullable []getFloatArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of floats with the requested length from native memory.static float @Nullable []getFloatArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of float from native memory.static floatgetFloatFrom(MemorySegment address) Copy a float value from native memory.static floatgetFloatFrom(MemorySegment address, TransferOwnership transfer) Copy a float value from native memory.static int @Nullable []getIntegerArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of integers with the requested length from native memory.static int @Nullable []getIntegerArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of integers from native memory.static intgetIntegerFrom(MemorySegment address) Copy an integer value from native memory.static intgetIntegerFrom(MemorySegment address, TransferOwnership transfer) Copy an integer value from native memory.static long @Nullable []getLongArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of longs with the requested length from native memory.static long @Nullable []getLongArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of longs from native memory.static longgetLongFrom(MemorySegment address) Copy a long value from native memory.static longgetLongFrom(MemorySegment address, TransferOwnership transfer) Copy a long value from native memory.static <T extends Proxy>
T @Nullable []getProxyArrayFrom(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 []getProxyArrayFrom(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 short @Nullable []getShortArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of shorts with the requested length from native memory.static short @Nullable []getShortArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of shorts from native memory.static shortgetShortFrom(MemorySegment address) Copy a short value from native memory.static shortgetShortFrom(MemorySegment address, TransferOwnership transfer) Copy a short value from native memory.static @Nullable String @Nullable []getStringArrayFrom(MemorySegment address, int length, TransferOwnership transfer) Read an array of Strings with the requested length from native memory.static @Nullable String @Nullable []getStringArrayFrom(MemorySegment address, TransferOwnership transfer) Read an array of Strings from aNULL-terminated array in native memory.static @Nullable StringgetStringFrom(MemorySegment address) Copy a Java string from native memory usingMemorySegment.getString().static @Nullable StringgetStringFrom(MemorySegment address, TransferOwnership transfer) Copy a Java string from native memory usingMemorySegment.getString().static <T extends Proxy>
T @Nullable []getStructArrayFrom(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 []getStructArrayFrom(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 []getStrvArrayFrom(MemorySegment address, int length, TransferOwnership transfer) Readlengtharrays of Strings from native memory.static @Nullable String @Nullable [] @Nullable []getStrvArrayFrom(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.static <T extends Enum<T> & Enumeration>
EnumSet<T> 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
-
functionNotFound
A MethodHandle to this function is returned when a downcall handle was requested for a foreign function that was not found by the linker.- Parameters:
args- ignored- Returns:
- ignored
- Throws:
InteropException- always thrown
-
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(java.lang.foreign.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
-
allocateNativeString
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
-
allocateUnownedString
- 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
-
getStringFrom
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
-
getStringFrom
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
-
getBooleanFrom
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
-
getBooleanFrom
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
-
getByteFrom
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
-
getByteFrom
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
-
getCharacterFrom
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
-
getCharacterFrom
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
-
getDoubleFrom
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
-
getDoubleFrom
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
-
getFloatFrom
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
-
getFloatFrom
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
-
getIntegerFrom
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
-
getIntegerFrom
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
-
getLongFrom
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
-
getLongFrom
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
-
getShortFrom
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
-
getShortFrom
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
-
getStringArrayFrom
public static @Nullable String @Nullable [] getStringArrayFrom(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
-
getStringArrayFrom
public static @Nullable String @Nullable [] getStringArrayFrom(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
-
getStrvArrayFrom
public static @Nullable String @Nullable [] @Nullable [] getStrvArrayFrom(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
-
getStrvArrayFrom
public static @Nullable String @Nullable [] @Nullable [] getStrvArrayFrom(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
-
getAddressArrayFrom
public static MemorySegment @Nullable [] getAddressArrayFrom(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
-
getAddressArrayFrom
public static MemorySegment @Nullable [] getAddressArrayFrom(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
-
getBooleanArrayFrom
public static boolean @Nullable [] getBooleanArrayFrom(MemorySegment address, long length, Arena arena, 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 arrayarena- the memory scopetransfer- ownership transfer- Returns:
- array of booleans
-
getByteArrayFrom
public static byte @Nullable [] getByteArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of bytes with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arrayarena- the memory scopetransfer- ownership transfer- Returns:
- array of bytes
-
getByteArrayFrom
public static byte @Nullable [] getByteArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of bytes from native memory.- Parameters:
address- address of the memory segmentarena- the memory scopetransfer- ownership transfer- Returns:
- array of bytes
-
getCharacterArrayFrom
public static char @Nullable [] getCharacterArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of chars with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arrayarena- the memory scopetransfer- ownership transfer- Returns:
- array of chars
-
getDoubleArrayFrom
public static double @Nullable [] getDoubleArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of doubles with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arrayarena- the memory scopetransfer- ownership transfer- Returns:
- array of doubles
-
getFloatArrayFrom
public static float @Nullable [] getFloatArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of floats with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arrayarena- the memory scopetransfer- ownership transfer- Returns:
- array of floats
-
getFloatArrayFrom
public static float @Nullable [] getFloatArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of float from native memory.- Parameters:
address- address of the memory segmentarena- the memory scopetransfer- ownership transfer- Returns:
- array of floats
-
getIntegerArrayFrom
public static int @Nullable [] getIntegerArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of integers with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arrayarena- the memory scopetransfer- ownership transfer- Returns:
- array of integers
-
getIntegerArrayFrom
public static int @Nullable [] getIntegerArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of integers from native memory.- Parameters:
address- address of the memory segmentarena- the memory scopetransfer- ownership transfer- Returns:
- array of integers
-
getLongArrayFrom
public static long @Nullable [] getLongArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of longs with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arrayarena- the memory scopetransfer- ownership transfer- Returns:
- array of longs
-
getLongArrayFrom
public static long @Nullable [] getLongArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of longs from native memory.- Parameters:
address- address of the memory segmentarena- the memory scopetransfer- ownership transfer- Returns:
- array of longs
-
getShortArrayFrom
public static short @Nullable [] getShortArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of shorts with the requested length from native memory.- Parameters:
address- address of the memory segmentlength- length of the arrayarena- the memory scopetransfer- ownership transfer- Returns:
- array of shorts
-
getShortArrayFrom
public static short @Nullable [] getShortArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL-terminated array of shorts from native memory.- Parameters:
address- address of the memory segmentarena- the memory scopetransfer- ownership transfer- Returns:
- array of shorts
-
getProxyArrayFrom
public static <T extends Proxy> T @Nullable [] getProxyArrayFrom(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
-
getProxyArrayFrom
public static <T extends Proxy> T @Nullable [] getProxyArrayFrom(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
-
getStructArrayFrom
public static <T extends Proxy> T @Nullable [] getStructArrayFrom(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
-
getStructArrayFrom
public static <T extends Proxy> T @Nullable [] getStructArrayFrom(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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(@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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(@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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(boolean @Nullable [] array, boolean zeroTerminated, SegmentAllocator alloc) Convert a boolean[] array into an int[] array, and callsallocateNativeArray(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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(@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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(@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
-
allocateNativeArray
public static MemorySegment allocateNativeArray(@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> & Enumeration> 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
Create a bitfield from the provided Set of enums- Parameters:
set- the set of enums- Returns:
- the resulting bitfield
-
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
-