Java Bytebuffer Flip () and limit () of understanding, reproduced, from the inside understand the role of flip (), imagine the old typewriter

Source: Internet
Author: User
Tags closing tag compact

First column Point code snippet:
// ...
//
This code function copies all data from T.txt to Out_j.txt:
//
...
1 FileChannel fcin = new FileInputStream ("D:/t.txt"). Getchannel ();
2 FileChannel fcout = new FileOutputStream (New File ("D:/out_j.txt")). Getchannel ();
3 Bytebuffer buff = bytebuffer.allocate (1024);
4 Long T1 = System.currenttimemillis ();
5
6 while (Fcin.read (buff)! =-1)
7 {
8 Buff.flip ();
9 Fcout.write (Buff);
Ten buff.clear ();
11}
12
Long t2 = System.currenttimemillis ();
A Long size = Fcin.size ();
Javax.swing.JOptionPane.showMessageDialog (NULL, size + "bytes, time consuming" + (T2-T1 + 1) + "Ms.");
...
----------------------------------------------------------------------------------------------------
The instructions for Bytebuffer in the SDK documentation are:
Public abstract class Bytebuffer
Extends Buffer
Implements comparable <ByteBuffer>
This shows that Bytebuffer is an abstract class that inherits from Buffer and implements two interfaces.
Line 3 allocates a 1024-byte buffer through allocate () and returns a Bytebuffer object. (abstract class cannot be directly new)
Line 6 Fcin.read () reads the data into the buff. Read () Here is a virtual function of the FileChannel class.
Line 8 Buff.flip () This call is a part of the beginning that has not been understood.
----------------------------------------------------------------------------------------------------
The instructions for flip () in the SDK documentation are:
Public final Buffer Flip ()
Reverses this buffer. Set the limit for the current position first, and then set the location to zero. If a tag is already defined, the token is discarded.
This method is often used in conjunction with the Compact method when transferring data from one place to another.
My final understanding is: The document translation is too bad, the content should not be translated into Chinese, so it is not easy to understand.
The key is in the following 2 places:
Current position: This can be intuitively understood as the current data pointer in the buffer, orSQLThe cursor in the curpointer.
Limit: This can be understood as the end tag of the buffer section of the actual operation, recorded as Endpointer.
Reversal: This is completely not responsible for the flip word translation, if you refer to the flip of DirectX () and translated as flipping/paging, it is much better understood, like writing/reading a letter, write/read a page, turn to the next page, the eyes/pen from the bottom of the page back to the top.
The operation behind this flip is actually "positioning the Endpointer to Curpointer and setting the Curpointer to 0".
About the mark, which is not involved here. The next sentence, which is often used in conjunction with the Compact method, is conceivable because the compact method
Compressed, the true length of the valid data changes, and you definitely need to reposition the closing tag with flip.
When data operations such as padding, compression, and so on, Curpointer estimates are automatically updated and always point to the last valid data, so each tune
With Flip (), the endpointer points to the end of the valid data, and Curpointer points to 0 (the start of the buffer).
For example, a legend:
(c and E represent Curpointer and endpointer two pointers respectively)
* First an empty Bytebuffer (10 bytes in size)
-------------------
-------------------
C
E
* Then populate 5 bytes of data
-------------------
0 1 2) 3 4
-------------------
E C
At this point, Endpointer is still at 0, and Curpointer moves to the end of the data.
After testing, if the data is taken at this time, it will get 5 bytes, the content is usually 0 (also may be unknown), because actually take from C to buffer
The 5 uninitialized bytes at the actual end of the zone.
* Once Flip () is called
-------------------
0 1 2) 3 4
-------------------
C E
At this point, the Endpointer is moved to Curpointer first, and then Curpointer moves to 0.
Through the test can be seen, bytebuffer fetch data, from Curpointer, to Endpointer, if Curpointer > Endpointer, then take to the end of the buffer.
Looking at the key fragment of the code above, the call to Flip () at line 8 has two functions, one is to move the Curpointer to 0, and the other to move the endpointer to the end of the valid data.
This line can be replaced by the following two lines:
Buff.limit (Buff.position ());
Buff.position (0);
It can be seen that the understanding of its working principle should be correct.
---------------------------- ------------------------------------------------------------------------
summarized as follows:
1. When you put the data, the existing data in the buffer is not automatically cleared.
2. After each get or put, the curpointer will move to the end of the buffer, the amount of movement = the amount of data being manipulated.
3. Get/put is from Curpointer, to Curpointer + operation data length.
4. In get/put operation, if the curpointer exceeds the Endpointer or the total buffer length, the java.nio.BufferUnderflowException exception will be thrown.
Note: Curpointer and Endpointer are only for the convenience of the description of the name, the actual corresponding to Bytebuffer.position () and Bytebuffer.limit () two methods.

Java Bytebuffer Flip () and limit () of understanding, reproduced, from the inside understand the role of flip (), imagine the old typewriter

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.