Contents Prev Next

# 3 Using Java Strings from C

This section describes a set of utility functions useful when accessing Java String objects from C.

javaStringPrint

    void javaStringPrint(Hjava_lang_String *s)

Print the String object, just like the regular java "print()" function does.

javaStringLength

    int javaStringLength(Hjava_lang_String *s)

Return the length of the String object.

makejavaString

    Hjava_lang_String *makejavaString(char *str, int len)

Create and return a new Java String object, initialized from the C string.

makeCString
allocCString

    char *makeCString(Hjava_lang_String *s)
    char *allocCString(Hjava_lang_String *s)

Create a new C string initialized from the specified Java string, and return a pointer to the C string.

For makeCString, temporary storage is allocated and released automatically when all references to the returned value are eliminated. WARNING: You must keep this pointer in a variable to prevent the storage from getting garbage collected.

For allocCString, a "malloc" is used to get the storage; the caller is responsible for "free"ing the pointer that is returned.

javaString2unicode

    unicode *javaString2unicode(Hjava_lang_String *s, unicode *buf, int len)

Get the characters of the String object into a unicode string buffer. No allocation occurs. Assumes that len is less than or equal to the length of the string, and that the buf is at least len+1 unicodes in size. The unicode buffer's address is returned.

javaString2CString

    char *javaString2CString(Hjava_lang_String *s, char *buf, int len)

Get the characters of the String object into a C string buffer. No allocation occurs. Assumes that len is less than or equal to the length of the string, and that the buf is at least len+1 bytes in size. The C string's address is returned.

len does not indicate the maximum number of characters to copy (a la strncpy)! It indicates the exact number of characters to copy, which most commonly will be javaStringLength(s)).

Contents Prev Next

Implementing Native Methods

Generated with CERN WebMaker