Skip to content

Commit e9cf593

Browse files
committed
refactor jstring2string
1 parent 8f0c03c commit e9cf593

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

cpp-ch/local-engine/local_engine_jni.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,13 @@ static std::string jstring2string(JNIEnv * env, jstring jStr)
7777
if (!jStr)
7878
return "";
7979

80-
jclass string_class = env->GetObjectClass(jStr);
81-
jmethodID get_bytes = env->GetMethodID(string_class, "getBytes", "(Ljava/lang/String;)[B");
82-
jbyteArray string_jbytes
80+
const jclass string_class = env->GetObjectClass(jStr);
81+
const jmethodID get_bytes = env->GetMethodID(string_class, "getBytes", "(Ljava/lang/String;)[B");
82+
const auto string_jbytes
8383
= static_cast<jbyteArray>(local_engine::safeCallObjectMethod(env, jStr, get_bytes, env->NewStringUTF("UTF-8")));
8484

85-
size_t length = static_cast<size_t>(env->GetArrayLength(string_jbytes));
86-
jbyte * p_bytes = env->GetByteArrayElements(string_jbytes, nullptr);
87-
88-
std::string ret = std::string(reinterpret_cast<char *>(p_bytes), length);
89-
env->ReleaseByteArrayElements(string_jbytes, p_bytes, JNI_ABORT);
85+
const auto string_jbytes_a = local_engine::getByteArrayElementsSafe(env, string_jbytes);
86+
std::string ret{reinterpret_cast<char *>(string_jbytes_a.elems()), static_cast<size_t>(string_jbytes_a.length())};
9087

9188
env->DeleteLocalRef(string_jbytes);
9289
env->DeleteLocalRef(string_class);

0 commit comments

Comments
 (0)