diff --git a/java/src/main/native/include/jni_utils.hpp b/java/src/main/native/include/jni_utils.hpp index a45716a89b3..e8346e5ef1e 100644 --- a/java/src/main/native/include/jni_utils.hpp +++ b/java/src/main/native/include/jni_utils.hpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -524,7 +525,7 @@ class native_jstring { void init_cstr() const { if (orig != NULL && cstr == NULL) { cstr_length = env->GetStringUTFLength(orig); - cstr = env->GetStringUTFChars(orig, 0); + cstr = env->GetStringUTFChars(orig, 0); // not guarantee to have null terminated. check_java_exception(env); } } @@ -555,6 +556,7 @@ class native_jstring { bool is_null() const noexcept { return orig == NULL; } + // Note that the char* return by this function is not guaranteed to be null-terminated. const char *get() const { init_cstr(); return cstr; @@ -565,6 +567,12 @@ class native_jstring { return cstr_length; } + // Note that the char* return by `get()` is not guaranteed to be null-terminated. + // Thus, constructing an std::string should be performed with a string size supplied. + std::string get_cpp_str() const { return std::string(get(), size_bytes()); } + + jstring get_jstring() const { return orig; } + bool is_empty() const { if (cstr != NULL) { return cstr_length <= 0; @@ -576,8 +584,6 @@ class native_jstring { return true; } - const jstring get_jstring() const { return orig; } - ~native_jstring() { if (orig != NULL && cstr != NULL) { env->ReleaseStringUTFChars(orig, cstr);