Java Reference
In-Depth Information
Here,
js
is the
jstring
received as a function argument that is to be converted.
The return value from
GetStringUTFChars()
is
cs
,which is a pointer to an
array of UTF-8 characters that can be treated within C/C
code as a C string.
We e xplain the
NULL
argument passed to
GetStringUTFChars()
later.
It is very important to call
ReleaseStringUTFChars()
when finished with
the returned array of characters so the JVM can clean up the storage allocated
for
cs
. Otherwise, a memory leak occurs. One cannot assume that the memory
allocated for
cs
is freed automatically when the native method goes out of scope
as would happen in a true Java method.
It is also important to check the return value from
GetStringUTFChars()
.
If sufficient memory could not be allocated to contain the C string, then the
function returns
NULL
. The function also throws an
OutOfMemoryError
,but
because of the way exceptions are handled in JNI, you must still check for an
error return and then return to the calling Java method before the exception is
seen on the Java side. We learn more about exceptions in JNI later.
If buffer space to contain the converted string is already pre-allocated, or if
it is known that only a substring of the original Java string is needed, then the
GetStringUTFRegion()
function may be used. This function is much like
GetStringUTFChars()
except that its arguments include a beginning index
into the string and the number of characters that should be converted. It also
requires a pointer to a sufficiently-sized pre-existing buffer in which to place
the converted UTF-8 characters. A short code snippet illustrating
GetString-
UTFRegion()
is
++
char buffer[21];
jenv-
>
GetStringUTFRegion (js, 5, 20, buffer);
This code extracts 20 UTF-8 characters beginning at index 5 (counting from the
first character at index 0). The buffer is sized at 21 in order to contain the standard
C terminating null character. If the input string does not contain enough char-
acters, then a
StringIndexOutOfBoundsException
is thrown because the
function attempts to extract more characters from the
jstring
than exist. If the
buffer is not sized large enough, on the other hand, then data is written to memory
locations past the end of the buffer, producing unpredictable (and probably bad)
results. It is your responsibility to write error-checking code to prevent and/or
handle any such abnormal situations. If you are certain that no index overflow
can occur, then
GetStringUTFRegion()
is easier to use than
GetString-
UTFChars()
because the former does not allocate memory, thereby removing
the necessity to call
ReleaseStringUTFChars()
and never raising unex-
pected out-of-memory exceptions. In practice,
GetStringUTFRegion()
is
preferred for small fixed-size strings since the required buffer can be allocated on
the C stack very cheaply. Another useful string handling function is
GetString-
UTFLength()
,which returns the length in bytes needed to contain the UTF-8
version of a
jstring
.
Search WWH ::
Custom Search