forked from yxw027/Nuro_Navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjni_util.c
100 lines (87 loc) · 3.08 KB
/
jni_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/////////////// For Jni //////////////////
// Created by Dengxu @ 2012 03 19
// ///////////////////////////////////////
#include "jni_util.h"
#include <stdlib.h>
#include <string.h>
char *jstringTostring(JNIEnv *env, jstring jstr)
{
#ifndef __cplusplus
#define env (*env)
#define FindClass(a) FindClass(&env, a)
#define NewStringUTF(a) NewStringUTF(&env, a)
#define GetMethodID(a, b, c) GetMethodID(&env, a, b, c)
#define CallObjectMethod(a, b, c) CallObjectMethod(&env, a, b, c)
#define GetArrayLength(a) GetArrayLength(&env, a)
#define GetByteArrayElements(a, b) GetByteArrayElements(&env, a, b)
#define ReleaseByteArrayElements(a, b, c) ReleaseByteArrayElements(&env, a, b, c)
#define NewByteArray(a) NewByteArray(&env, a)
#define SetByteArrayRegion(a, b, c, d) SetByteArrayRegion(&env, a, b, c, d)
#define NewObject(a, b, c, d) NewObject(&env, a, b, c, d)
#endif
char *rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("utf-8");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
jsize alen = env->GetArrayLength(barr);
jbyte *ba = env->GetByteArrayElements(barr, JNI_FALSE);
if (alen > 0)
{
rtn = (char *)malloc(alen + 1);
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
//Release memory allocated.
env->ReleaseByteArrayElements(barr, ba, 0);
return rtn;
#ifndef __cplusplus
#undef env
#undef FindClass
#undef NewStringUTF
#undef GetMethodID
#undef CallObjectMethod
#undef GetArrayLength
#undef GetByteArrayElements
#undef ReleaseByteArrayElements
#undef NewByteArray
#undef SetByteArrayRegion
#undef NewObject
#endif
}
jstring stoJstring(JNIEnv *env, const char *pat)
{
#ifndef __cplusplus
#define env (*env)
#define FindClass(a) FindClass(&env, a)
#define NewStringUTF(a) NewStringUTF(&env, a)
#define GetMethodID(a, b, c) GetMethodID(&env, a, b, c)
#define CallObjectMethod(a, b, c) CallObjectMethod(&env, a, b, c)
#define GetArrayLength(a) GetArrayLength(&env, a)
#define GetByteArrayElements(a, b) GetByteArrayElements(&env, a, b)
#define ReleaseByteArrayElements(a, b, c) ReleaseByteArrayElements(&env, a, b, c)
#define NewByteArray(a) NewByteArray(&env, a)
#define SetByteArrayRegion(a, b, c, d) SetByteArrayRegion(&env, a, b, c, d)
#define NewObject(a, b, c, d) NewObject(&env, a, b, c, d)
#endif
jclass strClass = env->FindClass("java/lang/String");
jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(pat));
env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
jstring encoding = env->NewStringUTF("utf-8");
//Memory to JVM Proxy
return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
#ifndef __cplusplus
#undef env
#undef FindClass
#undef NewStringUTF
#undef GetMethodID
#undef CallObjectMethod
#undef GetArrayLength
#undef GetByteArrayElements
#undef ReleaseByteArrayElements
#undef NewByteArray
#undef SetByteArrayRegion
#undef NewObject
#endif
}