Too*_*dos 5 c++ unicode java-native-interface android-ndk
简单的问题。如何使用 JNI 和 C++ 从 unicode const char* 中获取 jstring?
\n\n这是我的问题,以及我已经尝试过的:
\n\nconst char* value = (some value from server);\n(*env)->NewStringUTF(value);\nRun Code Online (Sandbox Code Playgroud)\n\n这里的问题是 NewStringUTF 返回一个 UTF 字符串,并且它不喜欢某些非 UTF8 字符(有点明显,但值得简单尝试一下)。
\n\n尝试 2,使用 NewString:
\n\nconst char* value = (some value from server);\n(*env)->NewString(value, strlen(value));\nRun Code Online (Sandbox Code Playgroud)\n\n虽然 NewString 接受并返回 unicode 字符串,但 strlen(value) 方法不起作用,因为它需要 jsize 参数,而不仅仅是一个好的 ol' size_t 或长度。
\n\n我们如何获得 jsize?根据(非常少量的)文档和在线示例,您可以从 jIntArray 中获取 jsize。\n我找不到有关如何将 const char* 转换为某种类型的 jarray 的信息,这可能是无论如何,这是一个坏主意。
\n\n另一种选择是从 size_t 中的 int 中获取 jsize,我也还没有成功。
\n\n有没有人遇到过这个问题,或者对如何解决这个问题有建议?\n看来 jsize 是我在 unicode 转换中缺少的关键。\n另外,我正在使用 JNI 和 Android NDK,以防万一帮助任何人。
\n\n谢谢。
\n\n编辑\n我刚刚意识到 NewString 也需要 jchar*,所以它的签名是 (jchar*, jsize)。这意味着即使使用 jsize,const char* 也无法编译。
\n\n编辑 2 \n这是使用 NewStringUTF 方法时在运行时引发的异常。这与@fadden正在谈论的内容有关:
\n\nJNI WARNING: NewStringUTF input is not valid Modified UTF-8: illegal start byte 0xb7 03string: ' : Method(d6, us-dev1-api, 0), , \xe8\xa8\xa9\xef\xbf\xbdx\xef\xbf\xbdm\xef\xbf\xbdP)\nRun Code Online (Sandbox Code Playgroud)\n
正如错误消息所示,您的 char* 不是有效的 Modifed-utf8,因此 JVM 中止。
您有两种方法可以避免它们。
android ART check_jni.cc 中的检查逻辑如下 https://android.googlesource.com/platform/art/+/35e827a/runtime/check_jni.cc#1273
jstring toJString(JNIEnv* env, const char* bytes) {
const char* error = nullptr;
auto utf8 = CheckUtfBytes(bytes, &error);
if (error) {
std::ostringstream msg;
msg << error << " 0x" << std::hex << static_cast<int>(utf8);
throw std::system_error(-1, std::generic_category(), msg.str());
} else {
return env->NewStringUTF(bytes);
}
Run Code Online (Sandbox Code Playgroud)
这样,您始终会获得有效的jstring.
jbyteArray.jstring toJString(JNIEnv *env, const char *pat) {
int len = strlen(pat);
jbyteArray bytes = env->NewByteArray(len);
env->SetByteArrayRegion(bytes, 0, len, (jbyte *) pat);
jstring encoding = env->NewStringUTF("utf-8");
jstring jstr = (jstring) env->NewObject(java_lang_String_class,
java_lang_String_init, bytes, encoding);
env->DeleteLocalRef(encoding);
env->DeleteLocalRef(bytes);
return jstr;
}
Run Code Online (Sandbox Code Playgroud)
这样,你只是避免了崩溃,但字符串可能仍然无效,并且你复制了两次内存,这表现得很糟糕。
加上代码:
inline bool checkUtfBytes(const char* bytes) {
while (*bytes != '\0') {
const uint8_t* utf8 = reinterpret_cast<const uint8_t*>(bytes++);
// Switch on the high four bits.
switch (*utf8 >> 4) {
case 0x00:
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
case 0x06:
case 0x07:
// Bit pattern 0xxx. No need for any extra bytes.
break;
case 0x08:
case 0x09:
case 0x0a:
case 0x0b:
// Bit patterns 10xx, which are illegal start bytes.
return false;
case 0x0f:
// Bit pattern 1111, which might be the start of a 4 byte sequence.
if ((*utf8 & 0x08) == 0) {
// Bit pattern 1111 0xxx, which is the start of a 4 byte sequence.
// We consume one continuation byte here, and fall through to consume two more.
utf8 = reinterpret_cast<const uint8_t*>(bytes++);
if ((*utf8 & 0xc0) != 0x80) {
return false;
}
} else {
return false;
}
// Fall through to the cases below to consume two more continuation bytes.
case 0x0e:
// Bit pattern 1110, so there are two additional bytes.
utf8 = reinterpret_cast<const uint8_t*>(bytes++);
if ((*utf8 & 0xc0) != 0x80) {
return false;
}
// Fall through to consume one more continuation byte.
case 0x0c:
case 0x0d:
// Bit pattern 110x, so there is one additional byte.
utf8 = reinterpret_cast<const uint8_t*>(bytes++);
if ((*utf8 & 0xc0) != 0x80) {
return false;
}
break;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5540 次 |
| 最近记录: |