Just wondering whether it is possible to update a ByteArray in C Code, which is created in Java, without returning it from C.
I have situation, where I need to update a single bytearray for multiple times through JNI and returning bytearray from C takes lot of JNI calls. Please let me know if anybody knows how to do this?
Code should be something like this
Java Code
byte[] storeData;
updateFromNative(storeData); //update the byteArray in native code;
//use the storeData in Java with updated value.
Updating data in the array is one thing, allocating is another. If you know the size, and it’s not supposed to change, allocate the array beforehand, pass it into JNI, and use JNI calls SetByteArrayElement() and SetByteArrayRegion() to set elements. Like this:
However, if you want to (re)alocate the array within JNI, you’re stuck with returning it. There’s no out parameters in Java. One way around it is passing a class where the array is a member variable, and updating that member variable, but that complicates the JNI part somewhat.