Introduction to Java NIO (iv) Internal principles of Buffer

Source: Internet
Author: User

Overview
In this section, we will focus on two important components in NIO Buffer: State Variables and access methods.
Status variables are very important for the "Internal counting system" mentioned above. After each read/write, the Buffer status changes accordingly. By recording and tracking these changes, the Buffer can manage the resources in the Buffer.

When you read data from the Channel, the data is first put in the Buffer. In some cases, you can directly write the Buffer into another Channel, but in general, you may want to see the data content. This idea can be achieved through the get () method. Similarly, when you want to put the raw data in the Buffer, you can use the put () method.

In this section, we will learn about the state variables and access methods in NIO. Each component is involved and has the opportunity to view the specific usage. However, NIO's internal counting system may seem complicated at first, and you will soon see what the internal counting system actually does for you.

Status variable
A total of three values can be used to indicate the Buffer State at any given moment. They are:
Position
Limit
Capacity

These three variables track the Buffer status and the data contained in the Buffer.
Next we will examine each detail one by one, and also see why such a design is suitable for typical read/write (input/output) processing. For example, in this example, we assume that data is copied from one Channel to another.

Position
Recall that Buffer is actually an array. When you read data from a Channel, you put the data read from the Channel into the underlying array. The position variable is used to track the data that has been written so far. More precisely, it indicates the location of the array where data should be written in the Buffer next time. Therefore, if three bytes have been read from the Channel, the position of the Buffer is set to 3, pointing to the fourth position in the array.
Similarly, if you are writing data to the Channel, you need to obtain the data to be written from the Buffer. At this time, the position keeps track of how much data you have read from the Buffer. More precisely, position indicates which element of the array will be read in the next read from the Buffer. Therefore, if you have written five bytes to the Channel, the Buffer position is set to 5, pointing to the sixth element in the array.

Limit
When writing data from the Buffer to the Channel, the limit variable indicates how much data is available to read. When reading data from the Channel to the Buffer, the limit variable indicates how much space is left to store data.
Position is less than or equal to limit under normal conditions.

Capacity
The Capacity of the Buffer indicates the maximum amount of data that the Buffer can store. In fact, it indicates the capacity of the underlying array, or at least the amount of space allowed by the underlying array.
Limit will never be greater than capacity.

Observe the three variables by instance
We start with a new Buffer. For example, we assume that the Buffer has an 8-byte Capacity. The Buffer status is as follows:
 

Recall that limit is not greater than capacity. In this example, both limit and capacity are set to 8. We use arrows to indicate at the end of the array.
 

Set position to 0. If we read some data from the Channel into the Buffer, the next byte will be stored in a location of 0. If we write data from the Buffer into the Channel, the next read byte in the Buffer will be obtained from position 0. Shows how to set position:

 

Because once capacity is set, it will not change, and we will ignore capacity for the time being.

First read operation
Now we are ready to start read/write operations on the newly created Buffer. We start to read some data from the Channel into the Buffer, and read three bytes for the first time. The position is 0 before reading, and increases from 0 to 3 after reading, as shown below:
 

Second read operation
In the second read operation, we will read two more bytes from the Channel to the Buffer. The storage positions of these two bytes start from the previous position, that is, 3. After reading the data, the position increases by 2 to 5. For example:

 

Flip operations
At the current position, we have finished reading data from the Channel, and now we are writing data to the output Channel. Before writing, we must call the flip () method. This method has two important tasks:
1. Set limit to the current position.
2. Set position to 0.
The above figure shows the Buffer before the execution of flip. The following figure shows the Buffer after the execution of flip:

 

Now we are ready to write data from the Buffer to the Channel. Position has been set to 0, which means that the next byte we get is the byte with the position 0, and the limit has been set to the previous Position, this indicates that the Buffer contains all the bytes previously read into the Buffer.

First write operation
In our first write operation, we took 4 bytes from the Buffer and then written them to the output channel. This operation increases position from 0 to 4, while limit does not change, as shown below:

 

Second write operation
So far, only one byte can be written. When we call flip (), the limit is set to 5, and the position cannot exceed the limit. Therefore, the final write operation extracts a byte from the Buffer and writes it to the output Channel. This write operation will increase the position to 5, while the limit will remain unchanged. As follows:

 

Clear operation
The last step is to call the Clear method. This method resets the Buffer to prepare to receive new data. Clear has done two important tasks:
1. Set limit to 0 to match capacity.
2. Set position to 0.
The following figure shows the Buffer status after calling flip:

 

A working Buffer
The following code summarizes how to use Buffer to copy data from one Channel to another.
Java code
While (trie ){
Buffer. clear ();
Int r = fcin. read (buffer );
 
If (r =-1 ){
Break;
}
 
Buffer. flip ();
Fcout. write (buffer );
}

The read () and write () methods greatly simplify the program because the Buffer processes all the details. The clear () and flip () methods are used to switch the read and write operations of the Buffer.

Author: "guibin"
 

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.