51 static constexpr auto signature =
"Ljava/lang/String;"_asl;
53 static std::string fromJni(jobject val) {
56 static jmethodID getBytes =
jni::env()->GetMethodID(stringClass.asClass(),
"getBytes",
"(Ljava/lang/String;)[B");
57 jbyteArray b = jbyteArray(
jni::env()->CallObjectMethod(val, getBytes,
jni::env()->NewStringUTF(
"UTF-8")));
58 size_t length =
jni::env()->GetArrayLength(b);
59 jbyte* elems =
jni::env()->GetByteArrayElements(b,
nullptr);
60 std::string str(
reinterpret_cast<char*
>(elems), length);
61 jni::env()->ReleaseByteArrayElements(b, elems, JNI_ABORT);
68 static jstring toJni(std::string_view value) {
69 auto bytes =
env()->NewByteArray(value.length());
74 env()->SetByteArrayRegion(bytes, 0, value.length(),
reinterpret_cast<const jbyte*
>(value.data()));
76 static jmethodID stringConstructor =
env()->GetMethodID(stringClass(),
"<init>",
"([BLjava/lang/String;)V");
77 auto str =
env()->NewObject(stringClass(), stringConstructor, bytes,
env()->NewStringUTF(
"UTF-8"));
78 env()->DeleteLocalRef(bytes);
79 return static_cast<jstring
>(str);
83 static jclass stringClass() {