| 42 | } |
| 43 | |
| 44 | std::string SafeConvertToStdString(JNIEnv* env, jstring jText) { |
| 45 | if (jText == nullptr) { |
| 46 | return ""; |
| 47 | } |
| 48 | std::string result; |
| 49 | static Global<jclass> StringClass = env->FindClass("java/lang/String"); |
| 50 | static jmethodID GetBytesID = |
| 51 | env->GetMethodID(StringClass.get(), "getBytes", "(Ljava/lang/String;)[B"); |
| 52 | auto encoding = env->NewStringUTF("utf-8"); |
| 53 | auto jBytes = (jbyteArray)env->CallObjectMethod(jText, GetBytesID, encoding); |
| 54 | env->DeleteLocalRef(encoding); |
| 55 | if (env->ExceptionCheck()) { |
| 56 | env->ExceptionClear(); |
| 57 | return ""; |
| 58 | } |
| 59 | if (jBytes == nullptr) { |
| 60 | return ""; |
| 61 | } |
| 62 | auto textLength = env->GetArrayLength(jBytes); |
| 63 | if (textLength > 0) { |
| 64 | char* bytes = new char[textLength]; |
| 65 | env->GetByteArrayRegion(jBytes, 0, textLength, (jbyte*)bytes); |
| 66 | result = std::string(bytes, (unsigned)textLength); |
| 67 | delete[] bytes; |
| 68 | } |
| 69 | env->DeleteLocalRef(jBytes); |
| 70 | return result; |
| 71 | } |
| 72 | } // namespace pag |
no test coverage detected