开发者

Comparation in JNI

Here's a"dumb question : can two jstrings be compared 开发者_JAVA百科in JNI ? if so how ?


You have 2 options (I'm assuming you want to compare for equality...)

  1. Convert both jstrings to wstrings and compare
  2. Call the equals method of the jstring (however this requires a couple of calls to the JVM so will be slower than option 1.)

For option 1 you want to try something like the following. I don't have source code for option 2.

WARNING I haven't compiled this, or even tested it, but you probably want something like this. I think there's a bug in there if the java string holds anything other than ASCI characters. I also haven't done any error checking.

jbyteArray bytes = 0;
jclass localClass = env->FindClass("java/lang/String");
jmethodID methodID = env->GetMethodID(localClass, "getBytes", "()[B");
bytes = (jbyteArray)env->CallObjectMethod( jstr, methodID);
jint len = env->GetArrayLength( bytes);
char* data = new char[len+1];
env->GetByteArrayRegion( bytes, 0, len,(jbyte *)data);
data[len] = 0;

std::wstring result(data);

do the same for the second jstring, then compare the 2 std::wstring's

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜