Turn: Android Development Practice: Array Transfer of Java layer and JNI layer

Source: Internet
Author: User

Android development, often in the Java code and the JNI layer passed between the array (byte[]), a typical application is the Java layer to send the data sent to the client stream to the JNI layer, by the JNI layer socket code sent out, of course, The JNI layer also needs to return the data stream received from the socket to the Java layer. I summarized briefly, from the Java layer to the JNI layer, from the JNI layer to the Java layer, there are 3 kinds of delivery methods, the following is a brief introduction to the code example.

The main files of the sample code are two, one is Native.java, the Java layer class, the other is native.c, is the JNI layer of the file, the key place I have to add comments to the code, the complete code see post-blog attachment.

One, passing arrays from Java to the JNI layer

The JNI layer receives the byte[] array passed by the Java layer, which generally has 2 functions to get its value, one getbytearrayregion, and the other getbytearrayelements, which is a copy of the value, Copy the data from the Java-side array to a local array, which is a pointer to the local array pointer directly to the Java side of the array address, in essence, the JVM on the heap allocated on this array object to add a reference count, to ensure that garbage collection when not released, so that the local pointer to use, After use, the pointer must remember to release through the releasebytearrayelements, otherwise it will create a memory leak.

First look at the definition of Native.java:

Then look at the implementation code for the corresponding NATIVE.C:

Second, passing arrays from the JNI layer to the Java layer

To pass the array defined by the JNI layer to the Java layer, there are generally two methods, one is passed by the return value of the native function, and the other is passed through the function of the JNI layer callback Java layer, which is used in the JNI thread. Either way, it is inseparable from the Setbytearrayregion function, which copies the local array data to the Java-side array. The following is only the previous way, that is, by passing the native function return the value of the array of JNI layer, the callback method is in fact similar to the use, it is not described in detail.

First look at the definition of Native.java:

and see how native.c is implemented:

As can be seen from the above code example, the array object is first allocated on the heap by Newbytearray, and then the local array data is copied to the allocated array on the heap by Setbytearrayregion, and then the allocated array object is returned to the Java layer by the return value. For a callback, these steps are the same, except that the callback method returns the array object to the Java layer in the same way as the return value, and returns it to the Java layer as a callback function parameter in the callback function.

Third, Direct Buffer mode transfer

An important way to pass an array of Java and JNI layers is through direct buffer, which is similar to creating an entire block of memory on the heap that creates a shared Java and JNI layer, which can be accessed by either the Java layer or the JNI layer. And the Java side and the JNI side of the change, because it is the use of shared memory, it is more efficient than the normal array transfer, but because the construction/destruction/maintenance of the shared memory cost is relatively large, so the small data of the array is recommended to use the same method, Direct The buffer mode is more suitable for long-term sharing of large chunks of memory with frequent access. The specific use method is described as follows:

First look at the definition of Native.java:

and see how native.c is implemented:

As can be seen from the above code, it is very simple to use, the JNI layer only need to pass the Getdirectbufferaddress function to obtain the address of this shared memory, the management of Direct buffer is the responsibility of the operating system.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.