Android's approach to using Bytebuffer in JNI _android

Source: Internet
Author: User

The examples in this article describe the way Android uses Bytebuffer in JNI. Share to everyone for your reference. Specifically as follows:

I. Definition of Bytebuffer

In NiO, the read-write operation of the data is always associated with the buffer (read-time channel (Socketchannel) reads the data into the buffer, and writes the data into the buffer in order first)
The buffer is fixed-length, basically it's just a list, all of its elements are basic data types. Bytebuffer is the most commonly used buffer that provides a way to read and write to other data types, and the read-write method of the channel only receives Bytebuffer.

Bytebuffer has the following common properties:

Mark: The initial value is-1, marking the index location;
Position: Initial value is 0, index subscript;
Limit: It is best to define the length of bytebuffer, that is, to allow the length of readable space;
Capacity: The maximum number of data elements that the buffer can hold, which cannot be changed after creation;

Second, the use of Bytebuffer

1. Create Bytebuffer

① use Allocate () to create:

Bytebuffer buf = bytebuffer.allocate (length);
Length represents BUF Lengths

② using array creation:

Bytebuffer buf = Bytebuffer.wrap (ByteArray);
ByteArray represents an array

2. Wrap over buffer

Buf.flip ();

This method is used to prepare the buffer for the outgoing state of the data, and after the above method is executed, the output channel starts at the beginning of the data rather than the end. Wraps the data in the buffer and is only ready to write instead of read.

3. Clear Buffer

Buf.clear ();

This method does not actually change the data of the buffer, but simply resets the primary index value of the buffer. You do not have to create a new buffer for each read or write, which can degrade performance. Instead, reuse the current buffer and clear the buffer before reading again.

4. Bytebuffer and byte[] Interaction

byte[] ByteArray = new BYTE[10];
Bytebuffer buf = Bytebuffer.wrap (ByteArray);
Writes an array to buf
ByteArray = new byte[buf.remaining ()];
Buf.get (Bytearray,0,bytearray.length ());
Read the data to the array
ByteArray = new byte[buf.capacity ()];

Third, Bytebuffer and JNI interaction

JNI introduced in the Java1.4 version has three functions that can be used for the direct buffer of NIO. A direct byte buffer is a container for byte data, and Java will do its best to perform native I/O operations on it. JNI defines three functions for NIO operations.

The function allocates and returns a new Java.nio.ByteBuffer based on the pointer to the memory address and the memory length (capacity). Returns null if the function is not implemented for the current Java virtual machine, or throws an exception. If no storage is available, a outofmemoryexception will be thrown.

Jobject Newdirectbytebuffer (void* address, jlong capacity);

The Getdirectbufferaddress function returns an address pointer to the Java.nio.ByteBuffer object being passed in. If the function has not been implemented for the current virtual machine, or if BUF is not an object of java.nio.ByteBuffer, or if the memory area has not been defined, NULL will be returned.

void* getdirectbufferaddress (Jobject buf);

The Getdirectbuffercapacity function returns the capacity (in bytes) of the incoming Java.nio.ByteBuffer object. If the function is not implemented for the current environment, or if BUF is not a Java.nio.ByteBuffer type object, returns-1.

Jlong getdirectbuffercapacity (Jobject buf);

1. JNI Call

Java layer:

 Public final int ProcessData (bytebuffer data);

Native Interface:

 Private native long native_process (bytebuffer data);

JNI Layer:

Static Jlong native_process (jnienv *env,jobject obj,jobject data);

Note the signature of the Bytebuffer in the JNI layer: ljava/nio/bytebuffer;

2. Example (c + +):

Jclass cls = Env->getobjectclass (obj);
Jfieldid FID = Env->getfieldid (CLS, "Data", "ljava/nio/bytebuffer;");
Jobject bar = Env->getobjectfield (obj, FID);
Pimagedata->data= (mbyte*) env->getdirectbufferaddress (bar);
Data is the byte[in the pimagedata of structural body;

I hope this article will help you with your Android program.

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.