Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing byte array from native code to the Java side

My goal is passing a byte array of variable length from native code to Java side. The Java class instance method takes bytearray as its argument:

private void writeBuffer(final byte[] buffer)
{
}

I am able to find a method ID in the native code:

jclass cls = (*env)->FindClass(env,"class_path");
jmethodID writeBufferMethodID = (*env)->GetMethodID(env, cls, "writeBuffer", "([B)V");

But still can't figure out how to pass a byte array properly. I have tried:

jbyteArray retArray = (*env)->NewByteArray(env, data_size);
void *temp = (*env)->GetPrimitiveArrayCritical(env, (jarray)retArray, 0);
memcpy(temp, decoded_frame->data[0], data_size);
(*env)->CallVoidMethod(env, obj, writeBufferMethodID, retArray);
(*env)->ReleasePrimitiveArrayCritical(env, retArray, temp, 0);

and also:

retArray = (*env)->NewByteArray(env, data_size);
(*env)->SetByteArrayRegion(env, retArray, 0, data_size, (jbyte *)decoded_frame->data[0]);
(*env)->CallVoidMethod(env, obj, writeBufferMethodID, retArray);

The Java method gets called, but after a while the application crashes. Moreover, all values in Java buffers I receive equal to zero so it seems the content is not copied at all.

I verified the buffers content (decoded_frame->data[0]) on the native side by writing them into the binary file and there is no problem, the file contains exactly what I expect.

I call that method periodically; the size of array can vary in each call.

What is the correct and most effective way? Allocating a new array during each call is obviously a silly idea, but I do not know, how to avoid that if the array size varies.

EDIT:

I have rewritten my code this way and it seems to be OK now.

called in a while loop:

...do some decoding...

if(!retArray)
retArray = (*env)->NewByteArray(env, data_size);

if((*env)->GetArrayLength(env, retArray) != data_size)
{
(*env)->DeleteLocalRef(env, retArray);
retArray = (*env)->NewByteArray(env, data_size);
}

void *temp = (*env)->GetPrimitiveArrayCritical(env, (jarray)retArray, 0);
memcpy(temp, decoded_frame->data[0], data_size);
(*env)->CallVoidMethod(env, obj, writeBufferMethodID, retArray);
(*env)->ReleasePrimitiveArrayCritical(env, retArray, temp, 0);

called at loop exit:

(*env)->DeleteLocalRef(env, retArray);
like image 415
vitakot Avatar asked Mar 01 '12 15:03

vitakot


People also ask

Can we convert byte array to file in Java?

Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.

What is byte array in Java?

A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..

Why do we use byte array in Java?

We can use a byte array to store the collection of binary data.


1 Answers

Try calling ReleasePrimitiveArrayCritical before you call CallVoidMethod.

like image 190
Mārtiņš Možeiko Avatar answered Oct 30 '22 00:10

Mārtiņš Možeiko