Package org.graalvm.nativeimage.c.type
Class CTypeConversion
java.lang.Object
org.graalvm.nativeimage.c.type.CTypeConversion
Utility methods to convert between Java types and C types.
- Since:
- 19.0
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
An auto-closable that holds a JavaCharSequence
as a null-terminated C char[] array.static class
An auto-closable that holds a JavaCharSequence
[] array as a null-terminated array of null-terminated C char[]s. -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBuffer
asByteBuffer
(PointerBase address, int size) Creates aByteBuffer
that refers to the native memory at the specified address.static boolean
toBoolean
(int value) Converts a C int containing boolean values into a Java boolean.static boolean
toBoolean
(PointerBase pointer) Converts a C pointer into a Java boolean.static byte
toCBoolean
(boolean value) Converts a Java boolean into a C int containing boolean values.toCBytes
(byte[] bytes) Provides access to a C pointer for the provided Java byte array.toCString
(CharSequence javaString) Provides access to a C pointer for the provided Java String, encoded with the default charset.static UnsignedWord
toCString
(CharSequence javaString, Charset charset, CCharPointer buffer, UnsignedWord bufferSize) Copies thejavaString
into the buffer as a 0 terminated C string encoded with thecharset
character set.static UnsignedWord
toCString
(CharSequence javaString, CCharPointer buffer, UnsignedWord bufferSize) Copies thejavaString
into the buffer as a 0 terminated C string encoded with the default character set.toCStrings
(CharSequence[] javaStrings) Provides access to C pointers for the provided Java Strings, encoded with the default charset.static String
toJavaString
(CCharPointer cString) Decodes a 0 terminated Cchar*
to a Java string using the platform's default charset.static String
toJavaString
(CCharPointer cString, UnsignedWord length) Decodes a Cchar*
of lengthlength
to a Java string using the platform's default charset.static String
toJavaString
(CCharPointer cString, UnsignedWord length, Charset charset) Decodes a Cchar*
of lengthlength
to a Java string usingcharset
.static String
utf8ToJavaString
(CCharPointer utf8String) Decodes a UTF-8 encoded, 0 terminated Cchar*
to a Java string.
-
Method Details
-
toCString
Provides access to a C pointer for the provided Java String, encoded with the default charset.- Since:
- 19.0
-
toCString
public static UnsignedWord toCString(CharSequence javaString, CCharPointer buffer, UnsignedWord bufferSize) Copies thejavaString
into the buffer as a 0 terminated C string encoded with the default character set.When a nullptr is passed as the buffer and the buffer size is zero, then no copying is performed but the byte-sized length of the
javaString
is still returned.In case the string is larger than the
buffer
, then anIllegalArgumentException
is thrown.- Parameters:
javaString
- managed Java stringbuffer
- to store the bytes of 0 terminated javaString encoded with the default charset or a nullptrbufferSize
- size of the buffer or 0 when the buffer is a nullptr- Returns:
- the byte-sized length of the
javaString
when represented as a C string encoded with the default character set. This length does not include the 0 terminator. - Since:
- 19.0
-
toCString
public static UnsignedWord toCString(CharSequence javaString, Charset charset, CCharPointer buffer, UnsignedWord bufferSize) Copies thejavaString
into the buffer as a 0 terminated C string encoded with thecharset
character set.When a nullptr is passed as the buffer and the buffer size is zero, then no copying is performed but the byte-sized length of the
javaString
is still returned.In case the string is larger than the
buffer
, then anIllegalArgumentException
is thrown.- Parameters:
javaString
- managed Java stringcharset
- desired character set for the returned stringbuffer
- to store the bytes of 0 terminated javaString encoded with charset or a nullptrbufferSize
- size of the buffer or 0 when the buffer is a nullptr- Returns:
- the byte-sized length of the
javaString
when represented as C string encoded with thecharset
character set. This length does not include the 0 terminator. - Since:
- 19.0
-
toJavaString
Decodes a 0 terminated Cchar*
to a Java string using the platform's default charset.- Parameters:
cString
- the pointer to a 0 terminated C string- Returns:
- a Java string
- Since:
- 19.0
-
toJavaString
Decodes a Cchar*
of lengthlength
to a Java string using the platform's default charset.- Parameters:
cString
- the pointer to a 0 terminated C string- Returns:
- a Java string
- Since:
- 19.0
-
toJavaString
Decodes a Cchar*
of lengthlength
to a Java string usingcharset
.- Parameters:
cString
- the pointer to a 0 terminated C string- Returns:
- a Java string
- Since:
- 19.0
-
utf8ToJavaString
Decodes a UTF-8 encoded, 0 terminated Cchar*
to a Java string.- Parameters:
utf8String
- the pointer to a UTF-8 encoded, 0 terminated C string- Returns:
- a Java string
- Since:
- 22.3
-
toCBoolean
public static byte toCBoolean(boolean value) Converts a Java boolean into a C int containing boolean values.- Parameters:
value
- the Java boolean value- Returns:
- the C boolean value
- Since:
- 19.0
-
toBoolean
public static boolean toBoolean(int value) Converts a C int containing boolean values into a Java boolean.- Since:
- 19.0
-
toBoolean
Converts a C pointer into a Java boolean.- Since:
- 19.0
-
toCStrings
Provides access to C pointers for the provided Java Strings, encoded with the default charset.- Since:
- 19.0
-
asByteBuffer
Creates aByteBuffer
that refers to the native memory at the specified address. The passed size becomes the capacity of the byte buffer, and the buffer's byte order is set to native byte order. The caller is responsible for ensuring that the memory can be safely accessed while the ByteBuffer is used, and for freeing the memory afterwards.- Since:
- 19.0
-
toCBytes
Provides access to a C pointer for the provided Java byte array.- Since:
- 22.2
-