I am using the JNI between Java and C code to pass a byte[] array from Java to C. In C, the parameter is a jbyteArray, like follows:
jint Java_com_my_example_Class_getResult(jbyteArray ary, JNIEnv* env, jobject thiz);
Now, once I receive ‘ary’ as a jbyteArray, I would like to cast it to a structure, like this one:
struct st {
int first;
int second;
};
What is the best and safest way to do this? Should I try to convert the jbyteArray to a char array and then cast as normal, or is there a safer JNI related method for doing this?
call
GetByteArrayElementsto get a pointer-to-byte, and case that to your struct. Then don’t forget to make the corresponding release call.