JNI allocate array of arrays of primitive

B

Bill Medland

How, in JNI, can I allocate an array of arrays of primitives?

The problem I am having is in finding a suitable class pointer to pass to
NewObjectArray.

I can use java/lang/Object but I was wondering if I could get slightly
closer to [java/lang/byte.

TIA
 
J

Jean-Francois Briere

/**
* Creates a Java byte[][] object from a C++ char[len1][len2] multidim
array.
*/
jobjectArray create_array_of_byte_array(JNIEnv * env, char *
c_2d_char_arr, int len1, int len2) {
jclass byteArrCls = env->FindClass("[B");
jobjectArray arrOfByteArrObj = env->NewObjectArray(len1,
byteArrCls, NULL);
for (int i = 0; i < len1; i++) {
jbyteArray byteArrObj = env->NewByteArray(len2);
env->SetByteArrayRegion(byteArrObj, 0, len2, (jbyte
*)&(c_2d_char_arr[i * len2]));
env->SetObjectArrayElement(arrOfByteArrObj, i, byteArrObj);
}
return arrOfByteArrObj;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top