When studying Apache Mina source code, IoBuffer is used as the data Buffer object in IoFilter, and the implementation of IoBuffer comes from java. nio. Buffer. The concepts of Buffer, such as flip (), clear (), reset (), and mark (), make me confused. After carefully reading the jdk documentation, I can understand the concept of Buffer objects.
The Buffer class has the following four important attributes:
Mark: mark bit. It is used to restore the position to the original position when reset () is used. Call the mark () method to enable the value of position to associate with mark
Position: the location of the first data in the Buffer that can be read or written. When you call the put () method, place the written data to the position, then the position value is added with 1.
Limit: indicates the location of the first data in the buffer that cannot be read or written, that is, the stop bit. The data operation ends here.
Capacity: The size of the space allocated to the buffer by calling allocate (int size) during initialization, Which is unchangeable.
The Demo code is as follows:
Import Java. NIO. bytebuffer; <br/> public class bytebuffertest {</P> <p> Public static void main (string [] ARGs) {<br/> bytebuffer BB = bytebuffer. allocate (10); <br/> for (INT I = 1; I <9; I ++) {<br/> BB. put (byte) I); <br/>}</P> <p> system. out. println ("POS:" + BB. position (); <br/> system. out. println ("Limit:" + BB. limit (); <br/> system. out. println ("Cap:" + BB. capacity (); </P> <p> BB. flip (); <br/> system. out. println ("/nafter flip"); <br/> system. out. println ("POS:" + BB. position (); <br/> system. out. println ("Limit:" + BB. limit (); <br/> system. out. println ("Cap:" + BB. capacity (); </P> <p> BB. mark (); <br/> system. out. println ("/nafter mark"); <br/> system. out. println ("POS:" + BB. position (); <br/> system. out. println ("Limit:" + BB. limit (); <br/> system. out. println ("Cap:" + BB. capacity (); </P> <p> BB. reset (); <br/> system. out. println ("/nafter RESET"); <br/> system. out. println ("POS:" + BB. position (); <br/> system. out. println ("Limit:" + BB. limit (); <br/> system. out. println ("Cap:" + BB. capacity (); </P> <p> BB. clear (); <br/> system. out. println ("/nafter clear"); <br/> system. out. println ("POS:" + BB. position (); <br/> system. out. println ("Limit:" + BB. limit (); <br/> system. out. println ("Cap:" + BB. capacity (); </P> <p> BB. limit (1); <br/> BB. put (byte) 9); <br/> BB. put (byte) 10); // beyond the limit range, Java is thrown. NIO. bufferoverflowexception exception <br/> BB. put (byte) 11); <br/>}< br/>
Output result:
Pos: 8 <br/> limit: 10 <br/> cap: 10 <br/> after flip <br/> pos: 0 <br/> limit: 8 <br/> cap: 10 <br/> after mark <br/> pos: 0 <br/> limit: 8 <br/> cap: 10 <br/> after reset <br/> pos: 0 <br/> limit: 8 <br/> cap: 10 <br/> after clear <br/> pos: 0 <br/> limit: 10 <br/> cap: 10 <br/> Exception in thread "main" java. nio. bufferOverflowException <br/> at java. nio. buffer. nextPutIndex (Buffer. java: 419) <br/> at java. nio. heapByteBuffer. put (HeapByteBuffer. java: 145) <br/> at com. gftech. mytool. mina. byteBufferTest. main (ByteBufferTest. java: 45)