-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcompv_jni.cxx
executable file
·61 lines (53 loc) · 1.83 KB
/
compv_jni.cxx
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
/* Copyright (C) 2011-2020 Doubango Telecom <https://www.doubango.org>
* File author: Mamadou DIOP (Doubango Telecom, France).
* License: GPLv3. For commercial license please contact us.
* Source code: https://github.com/DoubangoTelecom/compv
* WebSite: http://compv.org
*/
#include "compv/base/compv_jni.h"
#if defined(HAVE_JNI_H) || COMPV_OS_ANDROID
#include "compv/base/compv_debug.h"
COMPV_NAMESPACE_BEGIN()
std::string CompVJNI::toString(JNIEnv* jEnv, jstring jstr)
{
if (!jEnv || !jstr) {
COMPV_DEBUG_ERROR("Invalid parameter");
return "";
}
const char* cstr = jEnv->GetStringUTFChars(jstr, NULL);
std::string str(cstr ? cstr : "");
jEnv->ReleaseStringUTFChars(jstr, cstr);
return str;
}
// You *must* clear the exception before calling this function
std::string CompVJNI::toString(JNIEnv* jEnv, jthrowable exc)
{
if (!jEnv || !exc) {
COMPV_DEBUG_ERROR("Invalid parameter");
return "";
}
std::string error = "";
jclass klass = NULL;
jmethodID toString = NULL;
jstring jstr = NULL;
const char* utf;
klass = jEnv->FindClass("java/lang/Object");
COMPV_CHECK_EXP_BAIL(!klass, COMPV_ERROR_CODE_E_JNI);
toString = jEnv->GetMethodID(klass, "toString", "()Ljava/lang/String;");
COMPV_CHECK_EXP_BAIL(!toString, COMPV_ERROR_CODE_E_JNI);
jstr = reinterpret_cast<jstring>(jEnv->CallObjectMethod(exc, toString));
COMPV_CHECK_EXP_BAIL(!jstr, COMPV_ERROR_CODE_E_JNI);
utf = jEnv->GetStringUTFChars(jstr, NULL);
COMPV_CHECK_EXP_BAIL(!jstr, COMPV_ERROR_CODE_E_JNI);
error = std::string(utf);
bail:
COMPV_jni_checkException1(jEnv);
if (jstr) {
jEnv->ReleaseStringUTFChars(jstr, utf);
}
COMPV_jni_DeleteLocalRef(jEnv, klass);
COMPV_jni_DeleteLocalRef(jEnv, jstr);
return error;
}
COMPV_NAMESPACE_END()
#endif /* defined(HAVE_JNI_H) || COMPV_OS_ANDROID */