- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
简单的问题。如何使用 JNI 和 C++ 从 unicode const char* 中获取 jstring?
这是我的问题,以及我已经尝试过的:
const char* value = (some value from server);
(*env)->NewStringUTF(value);
这里的问题是 NewStringUTF 返回一个 UTF 字符串,并且它不喜欢某些非 UTF8 字符(有点明显,但值得简单尝试一下)。
尝试 2,使用 NewString:
const char* value = (some value from server);
(*env)->NewString(value, strlen(value));
虽然 NewString 接受并返回 unicode 字符串,但 strlen(value) 方法不起作用,因为它需要 jsize 参数,而不仅仅是一个好的 ol' size_t 或长度。
我们如何获得 jsize?根据(非常非常少量的)文档和在线示例,您可以从 jIntArray 中获取 jsize。我找不到有关如何将 const char* 转换为某种 jarray 的信息,无论如何,这可能是一个坏主意。
另一个选择是从 size_t 中的 int 中获取 jsize,我也没有成功。
有人遇到过这个问题,或者有关于如何解决这个问题的建议吗?看来 jsize 是我在 unicode 转换中缺少的关键。另外,我正在使用 JNI 和 Android NDK,以防它对任何人有帮助。
谢谢。
编辑我刚刚意识到 NewString 也需要 jchar*,所以它的签名是 (jchar*, jsize)。这意味着即使使用 jsize,const char* 也无法编译。
编辑2这是使用 NewStringUTF 方法时在运行时引发的异常。这与 @fadden 正在谈论的内容相关:
JNI WARNING: NewStringUTF input is not valid Modified UTF-8: illegal start byte 0xb7 03string: ' : Method(d6, us-dev1-api, 0), , 訩�x�m�P)
最佳答案
如错误消息所示,您的 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);
}
这样,您始终会获得有效的 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;
}
这样,你只是避免了崩溃,但字符串可能仍然无效,并且你复制了两次内存,这表现得很糟糕。
加上代码:
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;
}
关于c++ - 使用 JNI 和 C++ 将 Unicode const char* 转换为 JString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28990038/
我刚开始使用 JNI,但遇到以下问题。 我有一个包含简单类的 C++ 库。我从 Java Android 项目中调用了三个 JNI 方法,分别实例化所述类、调用实例化类的方法并销毁它。我保留了对该对象
背景 我有一个 android 项目,它使用 JNI(使用 NDK)以 Java 和 C/C++ 进行编码。 我在java端创建了一个Jni java包装器,它将自己完成所有Jni操作,而除了这个包装
我想传递一个java对象的地址,JNI方法应该填充所传递对象的字段。1. java代码传递给定的对象引用。 JNI 方法应该能够缓存这个对象。这是一次性通话。2. 然后,java 对象使用不同的参数重
我本质上是在尝试遵循描述的 SO 解决方案 here , 但我遇到了问题。 这基本上就是我在 JNI 库中所做的事情: handle = dlopen("/data/data/lib/my.packa
我试图实现某种异常处理 一开始我打电话 jni::ExceptionDescribe() 之后我使用了的实现 How to obtain a description of a Java excepti
是否可以从不同 jni 库的另一个方法调用 1 个库的 jni 方法?例如:我有 2 个库 lib_1.so 和 lib_2.so。 我想从 lib_2.so 调用 lib_1.so 的方法 get_
我想在native方法中使用动态注册,所以我需要设置JNI_onLoad功能。我只是写了一个函数来获取两个数字的总和。但是,它无法正确构建。我该如何更正错误? 这是我的 *.cpp 文件,我将此文件命
我已经为 C 头文件制作了一个 make 文件,它工作正常,但是说 JNICALL 和 JNIEnv 存在语法错误,但我已经弄明白了这是因为头文件中的类型。 Image of the failure
我需要实现一个本地方法,比方说“public native void someFunc();”。我有两个库,libabc.so 和 libdef.so。 Java 使用 System.loadLibr
背景 我正在 eclipse 中为 android 开发一个应用程序,现在我遇到了一个问题,我需要你的帮助。所以我必须从 JAVA 应用程序调用用 C 编写的函数。但是在我编写代码的过程中,我有一些问
我正在使用 Android 上的 Java native 接口(interface)将当前 Activity 传递给 native 方法。但我没有使用类似 JNI 的函数名称来执行此操作。我正在手动注
我正在使用 JNI,我想知道是否可以通过 delegate 进行通信。 例如: Kotlin typealias MessageReceived = (msg: String) -> Unit ext
看来我对 JNI 的运气并不好。我正在等待我买的书到货,但现在是试错法。 我正在使用 JNI 来实现 Lua 求值器。 evaluatorNew() 只是创建一个新的 Evaluator() 对象,创
我有下面的代码,我想调用在同一个源文件中实现的函数,在本例中使用 C 语言: JNIEXPORT jstring JNICALL MyClass_get_1Uname__C (JNIEnv *env,
硬件手机和平板电脑内存太少,但 HAXM 工作正常? 我的基本问题是我的应用程序(很可能是我的 Java 应用程序加载的 JNI 动态库)太大。如果未使用硬件电话和模板调用 JNI 指令,Java 应
我正面临崩溃 JNI WARNING : 0x44f81e80 is not a valid JNI reference, in Ldalvik/system/NativeStart;. run()v
我移植了很多数学知识。我正在使用 over to c++ from java 并看到这样做有很大的性能提升,但我无法弄清楚要使用什么 jni 函数来摆脱我不再需要的变量。例如,我知道当您的 jni 方
我正在使用 JNI 调用一个静态 java 方法,该方法又创建一个 Swing JFrame 并显示它。代码相当简单,Java 代码独立运行(即 java StartAWT 做它应该做的事),而当使用
我正在尝试创建一个新线程,因此我将 VM 从我的方法初始化(从 Java 调用)传递到我的新线程。在线程中,我调用 AttachCurrentThread 并获取 JNIEnv* env。 稍后,我尝
我想知道是否有可能从java调用C++方法。 我非常希望能够从 java 读取内存进程。 我懂c++,但我想使用像java这样的更高级别,但仍然能够侵入进程内存。 有什么线索吗? []的 最佳答案 这
我是一名优秀的程序员,十分优秀!