I have a C function as follows:
int getInfo(int index, int type, void *pValue, int valueLen);
pValue
is the address of the buffer to hold the information value. However, the buffer must have been allocated by the caller beforehand.
Since I am fairly certain that I am working with small data, I do the following in my Java managed code...
public native int callGetInfo(int index, int type, byte[] value);
...and then pass a fixed-size byte array like this:
byte[] buf = new byte[1024];
callGetInfo(idx, t, buf);
Reason is because I read that for small data, using a byte array is better than ByteBuffer
in terms of performance.
My auto-generated JNI header is as follows:
JNIEXPORT jint JNICALL Java_com_testing_jni_Tester_callGetInfo (JNIEnv *, jobject, jint, jint, jbyteArray);
And so far this is what I have inside my bridging function:
JNIEXPORT jint JNICALL Java_com_testing_jni_Tester_callGetInfo (JNIEnv *env, jobject obj, jint index, jint type, jbyteArray array)
{
jboolean isCopy;
jbyte* bufferPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
// here I need to call the getInfo(index, type, pValue, valueLen) function, and then convert the pValue into a jbyteArray and return it using array
if(isCopy) {
(*env)->ReleaseByteArrayElements(env, array, bufferPtr, 0);
}
return 0;
}
Inside my bridging function, I would like to convert the value of pValue
(whatever type it may be) that I get back from the getInfo
function, into a jbyteArray
so that I can return it to my Java caller. How can I achieve this?