Java NIO usage and Principle Analysis (2) from the online data collation

Source: Internet
Author: User

In the buffer, the most important attributes are the following three, which work together to track changes in the internal state of the buffer:

Position: Specifies the next element index to be written or read, and its value is updated automatically by the Get ()/put () method, and position is initialized to 0 when a new buffer object is created.

Limit: Specifies how much data needs to be fetched (when the channel is written from the buffer), or how much space can be placed in the data (when the buffer is read from the channel).

Capacity: Specifies the maximum amount of data that can be stored in the buffer, in fact, it specifies the size of the underlying array, or at least specifies the capacity of the underlying array that we are allowed to use.

There are some relative size relationships between the above four attribute values: 0 <= position <= limit <= capacity. If we create a new Bytebuffer object with a capacity size of 10, at initialization time, position is set to 0,limit and capacity is set to 10, in the process of using Bytebuffer object later, The value of the capacity will no longer change, while the other two will change with use. The four attribute values are:

Now we can read some data from the channel into the buffer, noting that reading the data from the channel is equivalent to writing the data into the buffer. If you read 4 of your own data, then the value of position is 4, that is, the next byte index to be written is 4, and the limit is still 10, as shown in:

The next step is to write the read data to the output channel, which is equivalent to reading the data from the buffer, before which the flip () method must be called, and the method will do two things:

1. Set limit to the current position value
2. Set the position to 0

Since position is set to 0, it is guaranteed that the first byte in the buffer is read at the next output, and that the limit is set to the current position, which guarantees that the data read is exactly the data that was written to the buffer before, as shown in:

Now call the Get () method to read the data from the buffer to the output channel, which causes the position to increase while the limit remains the same, but the position does not exceed the limit value, so after reading us to 4 of ourselves in the buffer, The values for both position and limit are 4, as shown in:

After reading the data from the buffer, the value of limit remains at the value we call the Flip () method, and the clear () method is able to set all state changes to the value at the time of initialization, as shown in:

Finally, we use a piece of code to verify the process, as follows:

    ImportJava.io.*; ImportJava.nio.*; Importjava.nio.channels.*;  Public classProgram { Public Static voidMain (String args[])throwsException {fileinputstream fin=NewFileInputStream ("D:\\test.txt"); FileChannel FC=Fin.getchannel (); Bytebuffer Buffer= Bytebuffer.allocate (10); Output ("Initialize", buffer);              Fc.read (buffer); Output ("Call Read ()", buffer);              Buffer.flip (); Output ("Call Flip ()", buffer);  while(buffer.remaining () > 0) {                  byteb =Buffer.get (); //System.out.print (((char) b)); } output ("Call Get ()", buffer);              Buffer.clear (); Output ("Call Clear ()", buffer);          Fin.close (); }                 Public Static voidoutput (String step, buffer buffer) {System.out.println (step+ " : "); System.out.print ("Capacity:" + buffer.capacity () + ","); System.out.print ("Position:" + buffer.position () + ","); System.out.println ("Limit:" +buffer.limit ());          System.out.println (); }      }  

The finished output is:

Java NIO usage and Principle Analysis (2) from the online data collation

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.