public class SNI extends Object
Note: the platform default encoding is used. For converting using a different encoding, see
String(byte[], String)
& String.getBytes(String)
.
Example:
package example; public class Foo { private static final int MAX_STRING_SIZE = 42; // including the '\0' character public void pushString(String str) { pushString(SNI.toCString(str)); } public String pullString() { byte[] buffer = new byte[MAX_STRING_SIZE]; pullString(buffer); return SNI.toJavaString(buffer); } private static native void pushString(byte[] str); private static native void pullString(byte[] buffer); }
#include <sni.h> #include <string.h> #define MAX_STRING_SIZE 42 static char gStr[MAX_STRING_SIZE]; void Java_example_Foo_pushString(jbyte *str) { strncpy(gStr, (char*) str, MAX_STRING_SIZE); } void Java_example_Foo_pullString(jbyte *buffer) { strncpy((char*) buffer, gStr, MAX_STRING_SIZE); }
Modifier and Type | Method and Description |
---|---|
static byte[] |
toCString(String javaString)
Transforms a Java String into a C String.
The platform default encoding is used to transform Java characters into C characters. The returned C String is a NULL terminated String (ends with '\0'). |
static void |
toCString(String javaString,
byte[] cString)
Transforms a Java String into a C String.
The platform default encoding is used to transform Java characters into C characters. The created C String is a NULL terminated String (ends with '\0'). |
static String |
toJavaString(byte[] cString)
Transforms a C String into a Java String, using platform default encoding.
|
public static byte[] toCString(String javaString)
javaString
- the Java String to convert.IllegalArgumentException
- if javaString is null.public static void toCString(String javaString, byte[] cString)
cString
array length must be
at least javaString.length()+1
.javaString
- the Java String to convert.cString
- byte array which contains the C String.IllegalArgumentException
- if javaString or cString is null.ArrayIndexOutOfBoundsException
- if cString is too small to contain the string.public static String toJavaString(byte[] cString)
cString
- byte array which contains the C String.IllegalArgumentException
- if cString is null or its length is < 1.IllegalArgumentException
- if cString is not NULL terminated.