NDK: Does GetByteArrayElements copy data from Java to C ++?

I read this link about GetByteArrayElements: Frequently Asked Questions: How can I share raw data using my own code? http://developer.android.com/training/articles/perf-jni.html

  • He said GetByteArrayElements will return the actual pointers to the raw data in the Dalvik heap. So I can manipulate the source code in C ++ and speed up the process, am I correct?

  • So, does ReleaseByteArrayElements also not copy the data? Or, since GetByteArrayElements returns a pointer, and I don’t even need to release it after data manipulation, as with GetDirectBufferAddress for FloatBuffer?

  • If he does not need to copy data from Java to C ++, is it possible to pass and process a float array through GetByteArrayElements? Plz answer to NDK: passing a Jfloat array from Java to C ++ via GetByteArrayElements?

+6
source share
1 answer
  • Get<Primitive>ArrayElements may or may not copy data at its discretion. The isCopy parameter displays information about whether it was copied. If the data is not copied, you received a pointer to the data directly in the Dalvik heap. More details here .

  • You always need to call the appropriate Release<Primitive>ArrayElements , regardless of whether a copy was made. Copying the data back to the VM array is not the only cleanup that may need to be performed, although (according to the JNI documentation already linked), it is possible that the changes can be seen on the Java side before Release... was called Release... (if the data were not copied).

  • I do not believe that a virtual machine will allow you to make the transformations necessary in order to do what you think. As I can see, in any case, you will need to convert the byte array to float or float to the byte array in Java, which you cannot do with type casting. The data will be copied at some point.

Edit:

What you want to do is possible with ByteBuffer.allocateDirect , ByteBuffer.asFloatBuffer and GetDirectBufferAddress . In Java, you can use a FloatBuffer to interpret the data as a float, and the array will be available directly in native code using GetDirectBufferAddress . I sent an answer to your other question as an additional explanation.

+9
source

All Articles