Top Description Inners Constructors Methods
org.graalvm.nativeimage.c.type

public final Class CTypeConversion

extends Object
Class Inheritance
Imports
java.nio.ByteBuffer, .ByteOrder, java.nio.charset.Charset, org.graalvm.nativeimage.ImageSingletons, org.graalvm.nativeimage.impl.CTypeConversionSupport, org.graalvm.word.PointerBase, .UnsignedWord

Utility methods to convert between Java types and C types.
Since
19.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
CTypeConversion.CCharPointerHolder

An auto-closable that holds a Java CharSequence as a null-terminated C char[] array.

public abstract static class
CTypeConversion.CCharPointerPointerHolder

An auto-closable that holds a Java CharSequence[] array as a null-terminated array of null-terminated C char[]s.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ByteBuffer
asByteBuffer(PointerBase address, int size)

Creates a ByteBuffer that refers to the native memory at the specified address.

public static boolean
toBoolean(int value)

Converts a C int containing boolean values into a Java boolean.

public static boolean
toBoolean(PointerBase pointer)

Converts a C pointer into a Java boolean.

public static byte

Returns:

the C boolean value
toCBoolean
(boolean
the Java boolean value
value
)

Converts a Java boolean into a C int containing boolean values.

public static CTypeConversion.CCharPointerHolder
toCBytes(byte[] bytes)

Provides access to a C pointer for the provided Java byte array.

public static CTypeConversion.CCharPointerHolder
toCString(CharSequence javaString)

Provides access to a C pointer for the provided Java String, encoded with the default charset.

public static UnsignedWord

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.
toCString
(CharSequence
managed Java string
javaString
,
CCharPointer
to store the bytes of 0 terminated javaString encoded with the default charset or a nullptr
buffer
,
UnsignedWord
size of the buffer or 0 when the buffer is a nullptr
bufferSize
)

Copies the javaString into the buffer as a 0 terminated C string encoded with the default character set.

public static UnsignedWord

Returns:

the byte-sized length of the javaString when represented as C string encoded with the charset character set. This length does not include the 0 terminator.
toCString
(CharSequence
managed Java string
javaString
,
Charset
desired character set for the returned string
charset
,
CCharPointer
to store the bytes of 0 terminated javaString encoded with charset or a nullptr
buffer
,
UnsignedWord
size of the buffer or 0 when the buffer is a nullptr
bufferSize
)

Copies the javaString into the buffer as a 0 terminated C string encoded with the charset character set.

public static CTypeConversion.CCharPointerPointerHolder
toCStrings(CharSequence[] javaStrings)

Provides access to C pointers for the provided Java Strings, encoded with the default charset.

public static String

Returns:

a Java string
toJavaString
(CCharPointer
the pointer to a 0 terminated C string
cString
)

Decodes a 0 terminated C char* to a Java string using the platform's default charset.

public static String

Returns:

a Java string
toJavaString
(CCharPointer
the pointer to a 0 terminated C string
cString
,
UnsignedWord length)

Decodes a C char* of length length to a Java string using the platform's default charset.

public static String

Returns:

a Java string
toJavaString
(CCharPointer
the pointer to a 0 terminated C string
cString
,
UnsignedWord length, Charset charset)

Decodes a C char* of length length to a Java string using charset.

public static String

Returns:

a Java string
utf8ToJavaString
(CCharPointer
the pointer to a UTF-8 encoded, 0 terminated C string
utf8String
)

Decodes a UTF-8 encoded, 0 terminated C char* to a Java string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CTypeConversionback to summary
private CTypeConversion()

Method Detail

asByteBufferback to summary
public static ByteBuffer asByteBuffer(PointerBase address, int size)

Creates a ByteBuffer 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
toBooleanback to summary
public static boolean toBoolean(int value)

Converts a C int containing boolean values into a Java boolean.

Since
19.0
toBooleanback to summary
public static boolean toBoolean(PointerBase pointer)

Converts a C pointer into a Java boolean.

Since
19.0
toCBooleanback to summary
public static byte toCBoolean(boolean value)

Converts a Java boolean into a C int containing boolean values.

Parameters
value:boolean

the Java boolean value

Returns:byte

the C boolean value

Since
19.0
toCBytesback to summary
public static CTypeConversion.CCharPointerHolder toCBytes(byte[] bytes)

Provides access to a C pointer for the provided Java byte array.

Since
22.2
toCStringback to summary
public static CTypeConversion.CCharPointerHolder toCString(CharSequence javaString)

Provides access to a C pointer for the provided Java String, encoded with the default charset.

Since
19.0
toCStringback to summary
public static UnsignedWord toCString(CharSequence javaString, CCharPointer buffer, UnsignedWord bufferSize)

Copies the javaString 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 an IllegalArgumentException is thrown.

Parameters
javaString:CharSequence

managed Java string

buffer:CCharPointer

to store the bytes of 0 terminated javaString encoded with the default charset or a nullptr

bufferSize:UnsignedWord

size of the buffer or 0 when the buffer is a nullptr

Returns:UnsignedWord

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
toCStringback to summary
public static UnsignedWord toCString(CharSequence javaString, Charset charset, CCharPointer buffer, UnsignedWord bufferSize)

Copies the javaString into the buffer as a 0 terminated C string encoded with the charset 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 an IllegalArgumentException is thrown.

Parameters
javaString:CharSequence

managed Java string

charset:Charset

desired character set for the returned string

buffer:CCharPointer

to store the bytes of 0 terminated javaString encoded with charset or a nullptr

bufferSize:UnsignedWord

size of the buffer or 0 when the buffer is a nullptr

Returns:UnsignedWord

the byte-sized length of the javaString when represented as C string encoded with the charset character set. This length does not include the 0 terminator.

Since
19.0
toCStringsback to summary
public static CTypeConversion.CCharPointerPointerHolder toCStrings(CharSequence[] javaStrings)

Provides access to C pointers for the provided Java Strings, encoded with the default charset.

Since
19.0
toJavaStringback to summary
public static String toJavaString(CCharPointer cString)

Decodes a 0 terminated C char* to a Java string using the platform's default charset.

Parameters
cString:CCharPointer

the pointer to a 0 terminated C string

Returns:String

a Java string

Since
19.0
toJavaStringback to summary
public static String toJavaString(CCharPointer cString, UnsignedWord length)

Decodes a C char* of length length to a Java string using the platform's default charset.

Parameters
cString:CCharPointer

the pointer to a 0 terminated C string

Returns:String

a Java string

Since
19.0
toJavaStringback to summary
public static String toJavaString(CCharPointer cString, UnsignedWord length, Charset charset)

Decodes a C char* of length length to a Java string using charset.

Parameters
cString:CCharPointer

the pointer to a 0 terminated C string

Returns:String

a Java string

Since
19.0
utf8ToJavaStringback to summary
public static String utf8ToJavaString(CCharPointer utf8String)

Decodes a UTF-8 encoded, 0 terminated C char* to a Java string.

Parameters
utf8String:CCharPointer

the pointer to a UTF-8 encoded, 0 terminated C string

Returns:String

a Java string

Since
22.3
org.graalvm.nativeimage.c.type back to summary

public Interface CTypeConversion.CCharPointerHolder

extends AutoCloseable

An auto-closable that holds a Java CharSequence as a null-terminated C char[] array. The C pointer is only valid as long as the auto-closeable has not been closed.
Since
19.0

Method Summary

Modifier and TypeMethod and Description
public void
close()

Redeclares java.lang.AutoCloseable.close.

Discards the C pointer.

public CCharPointer
get()

Returns the C pointer to the null-terminated C char[] array.

Method Detail

closeback to summary
public void close()

Redeclares java.lang.AutoCloseable.close.

Discards the C pointer.

Annotations
@Override
Since
19.0
getback to summary
public CCharPointer get()

Returns the C pointer to the null-terminated C char[] array.

Since
19.0
org.graalvm.nativeimage.c.type back to summary

public abstract Class CTypeConversion.CCharPointerPointerHolder

extends Object
implements AutoCloseable
Class Inheritance
  • java.lang.Object
  • org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerPointerHolder
All Implemented Interfaces
java.lang.AutoCloseable

An auto-closable that holds a Java CharSequence[] array as a null-terminated array of null-terminated C char[]s. The C pointers are only valid as long as the auto-closeable has not been closed.
Since
19.0

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract void
close()

Redeclares java.lang.AutoCloseable.close.

Discards the C pointer.

public abstract CCharPointerPointer
get()

Returns the C pointer to pointers of null-terminated C char[] arrays.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CCharPointerPointerHolderback to summary
public CCharPointerPointerHolder()
Since
19.0

Method Detail

closeback to summary
public abstract void close()

Redeclares java.lang.AutoCloseable.close.

Discards the C pointer.

Annotations
@Override
Since
19.0
getback to summary
public abstract CCharPointerPointer get()

Returns the C pointer to pointers of null-terminated C char[] arrays.

Since
19.0