Class CTypeConversion

java.lang.Object
org.graalvm.nativeimage.c.type.CTypeConversion

public final class CTypeConversion extends Object
Utility methods to convert between Java types and C types.
Since:
19.0
  • Method Details

    • toCString

      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
    • toCString

      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 - managed Java string
      buffer - to store the bytes of 0 terminated javaString encoded with the default charset or a nullptr
      bufferSize - 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 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 - managed Java string
      charset - desired character set for the returned string
      buffer - to store the bytes of 0 terminated javaString encoded with charset or a nullptr
      bufferSize - 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 the charset character set. This length does not include the 0 terminator.
      Since:
      19.0
    • toJavaString

      public static String toJavaString(CCharPointer cString)
      Decodes a 0 terminated C char* 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

      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 - the pointer to a 0 terminated C string
      Returns:
      a Java string
      Since:
      19.0
    • toJavaString

      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 - the pointer to a 0 terminated C string
      Returns:
      a Java string
      Since:
      19.0
    • utf8ToJavaString

      public static String utf8ToJavaString(CCharPointer utf8String)
      Decodes a UTF-8 encoded, 0 terminated C char* 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

      public static boolean toBoolean(PointerBase pointer)
      Converts a C pointer into a Java boolean.
      Since:
      19.0
    • toCStrings

      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
    • asByteBuffer

      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
    • toCBytes

      public static CTypeConversion.CCharPointerHolder toCBytes(byte[] bytes)
      Provides access to a C pointer for the provided Java byte array.
      Since:
      22.2