Commit | Line | Data |
---|---|---|
805e021f CE |
1 | |
2 | #include <jni.h> | |
3 | #include "GetNativeString.h" | |
4 | ||
5 | char* GetNativeString(JNIEnv *env, jstring jstr){ | |
6 | jbyteArray bytes = 0; | |
7 | jthrowable exc; | |
8 | char *result = 0; | |
9 | ||
10 | if ((*env)->EnsureLocalCapacity(env, 2) < 0) { | |
11 | return 0; /* out of memory error */ | |
12 | } | |
13 | ||
14 | jclass stringClass=(*env)->FindClass(env,"java/lang/String"); | |
15 | ||
16 | if(!stringClass){ | |
17 | return 0; | |
18 | } | |
19 | ||
20 | jmethodID MID_String_getBytes = (*env)->GetMethodID(env,stringClass,"getBytes","()[B"); | |
21 | if(!MID_String_getBytes){ | |
22 | return 0; | |
23 | } | |
24 | ||
25 | bytes = (*env)->CallObjectMethod(env, jstr, | |
26 | MID_String_getBytes); | |
27 | exc = (*env)->ExceptionOccurred(env); | |
28 | if (!exc) { | |
29 | jint len = (*env)->GetArrayLength(env, bytes); | |
30 | result = malloc(len + 1); | |
31 | if (result == 0) { | |
32 | ||
33 | /*JNU_ThrowByName(env, "java/lang/OutOfMemoryError", | |
34 | 0);*/ | |
35 | ||
36 | (*env)->DeleteLocalRef(env, bytes); | |
37 | return 0; | |
38 | } | |
39 | (*env)->GetByteArrayRegion(env, bytes, 0, len, | |
40 | (jbyte *)result); | |
41 | result[len] = 0; /* NULL-terminate */ | |
42 | } else { | |
43 | (*env)->DeleteLocalRef(env, exc); | |
44 | } | |
45 | (*env)->DeleteLocalRef(env, bytes); | |
46 | return result; | |
47 | } | |
48 | ||
49 | jstring GetJavaString(JNIEnv *env, char*); |