Android 从 JNI 中传入 UTF8 的字符到 Java
2010-03-26 21:14:00 来源:WEB开发网jbyte buffer[] = /* UTF8 encoding buffer */
jbyteArray bytes = env->NewByteArray(sizeof(buffer));
env->SetByteArrayRegion(bytes, 0, sizeof(buffer), buffer);
return bytes;
}
JNIEXPORT jbyteArray JNICALL Java_ReturnUTF8_method2
(JNIEnv *env, jclass cls)
{
jbyte buffer[] = /* UTF8 encoding buffer */
jbyteArray bytes = env->NewByteArray(sizeof(buffer));
env->SetByteArrayRegion(bytes, 0, sizeof(buffer), buffer);
jchar encoding_name[] = { 'U', 'T', 'F', '-', '8', '' };
jstring encoding = env->NewString(encoding_name, 5);
jclass theStringClass = env->FindClass("Ljava/lang/String;");
jmethodID mid = env->GetMethodID(theStringClass, "
jobject result = env->NewObject(theStringClass, mid, bytes, encoding);
return result;
}
#include
"ReturnUTF8.h"
JNIEXPORT jbyteArray JNICALL Java_ReturnUTF8_method1
(JNIEnv *env, jclass cls)
{
jbyte buffer[] = /* UTF8 encoding buffer */
jbyteArray bytes = env->NewByteArray(sizeof(buffer));
env->SetByteArrayRegion(bytes, 0, sizeof(buffer), buffer);
return bytes;
}
JNIEXPORT jbyteArray JNICALL Java_ReturnUTF8_method2
(JNIEnv *env, jclass cls)
{
jbyte buffer[] = /* UTF8 encoding buffer */
jbyteArray bytes = env->NewByteArray(sizeof(buffer));
env->SetByteArrayRegion(bytes, 0, sizeof(buffer), buffer);
jchar encoding_name[] = { 'U', 'T', 'F', '-', '8', '' };
jstring encoding = env->NewString(encoding_name, 5);
jclass theStringClass = env->FindClass("Ljava/lang/String;");
jmethodID mid = env->GetMethodID(theStringClass, "
"([BLjava/lang/String;)V");
jobject result = env->NewObject(theStringClass, mid, bytes,
encoding);
return result;
}
# g++ -shared -I /usr/lib/jvm/java-1.5.0-sun/include -I /usr/lib/jvm/java-1.5.0-sun/include/linux ReturnUTF8.cpp -o libReturnUTF8Jni.so
# export LD_LIBRARY=.
# java ReturnUTF8
更多精彩
赞助商链接