Here's a"dumb question : can two jstrings be compared in JNI ? if so how ?
+1
A:
You have 2 options (I'm assuming you want to compare for equality...)
- Convert both jstrings to wstrings and compare
- 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
Glen
2010-04-08 12:03:52