Java NIO usage and Principle Analysis (ii) (EXT)

Source: Internet
Author: User

In the first article, we introduced the two core objects in NiO: Buffers and channels, when it comes to buffers, we say that the buffer object is essentially an array, but it is actually a special array, and the buffer object has built-in mechanisms to track and record the state of the buffer, if we use Get () Method gets the data from the buffer or writes the data to the buffer using the put () method, causing a change in the state of the buffer. This article is the second in the analysis of the use and principle of NIO, which will analyze the buffer object in NiO.

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:

[Java]View PlainCopyprint?
  1. Import java.io.*;
  2. Import java.nio.*;
  3. Import java.nio.channels.*;
  4. Public class Program {
  5. public static void Main (String args[]) throws Exception {
  6. FileInputStream fin = new FileInputStream ("D:\\test.txt");
  7. FileChannel FC = Fin.getchannel ();
  8. Bytebuffer buffer = bytebuffer.allocate (10);
  9. Output ("initialize", buffer);
  10. Fc.read (buffer);
  11. Output ("call Read ()", buffer);
  12. Buffer.flip ();
  13. Output ("Call Flip ()", buffer);
  14. While (buffer.remaining () > 0) {
  15. byte B = buffer.get ();
  16. //System.out.print (((char) b));
  17. }
  18. Output ("Call Get ()", buffer);
  19. Buffer.clear ();
  20. Output ("Call Clear ()", buffer);
  21. Fin.close ();
  22. }
  23. public static void output (String step, buffer buffer) {
  24. System.out.println (step + ":");
  25. System.out.print ("Capacity:" + buffer.capacity () + ",");
  26. System.out.print ("Position:" + buffer.position () + ",");
  27. System.out.println ("limit:" + buffer.limit ());
  28. System.out.println ();
  29. }
  30. }

The finished output is:

This is consistent with the process we demonstrated above. In a later article, we continue to introduce some of the more advanced uses of the buffer in NiO.

Original address: http://blog.csdn.net/wuxianglong/article/details/6612246

Java NIO usage and Principle Analysis (ii) (EXT)

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.