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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAn auto-closable that holds a JavaCharSequenceas a null-terminated C char[] array.static classAn auto-closable that holds a JavaCharSequence[] array as a null-terminated array of null-terminated C char[]s. -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBufferasByteBuffer(PointerBase address, int size) Creates aByteBufferthat refers to the native memory at the specified address.static booleantoBoolean(int value) Converts a C int containing boolean values into a Java boolean.static booleantoBoolean(PointerBase pointer) Converts a C pointer into a Java boolean.static bytetoCBoolean(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 UnsignedWordtoCString(CharSequence javaString, Charset charset, CCharPointer buffer, UnsignedWord bufferSize) Copies thejavaStringinto the buffer as a 0 terminated C string encoded with thecharsetcharacter set.static UnsignedWordtoCString(CharSequence javaString, CCharPointer buffer, UnsignedWord bufferSize) Copies thejavaStringinto 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 StringtoJavaString(CCharPointer cString) Decodes a 0 terminated Cchar*to a Java string using the platform's default charset.static StringtoJavaString(CCharPointer cString, UnsignedWord length) Decodes a Cchar*of lengthlengthto a Java string using the platform's default charset.static StringtoJavaString(CCharPointer cString, UnsignedWord length, Charset charset) Decodes a Cchar*of lengthlengthto a Java string usingcharset.static Stringutf8ToJavaString(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 thejavaStringinto 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
javaStringis still returned.In case the string is larger than the
buffer, then anIllegalArgumentExceptionis 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
javaStringwhen 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 thejavaStringinto the buffer as a 0 terminated C string encoded with thecharsetcharacter 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
javaStringis still returned.In case the string is larger than the
buffer, then anIllegalArgumentExceptionis 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
javaStringwhen represented as C string encoded with thecharsetcharacter 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 lengthlengthto 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 lengthlengthto 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 aByteBufferthat 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
-