Java NiO depth (buffer)-How far can we go series (18)

Source: Internet
Author: User
Tags rewind
How far can we go series (18)

Nonsense:I want to change jobs recently. The current supervisor is not happy. When I did not mention it, the relationship seemed to be good. When I mention it, I am determined that he insisted that I stay for a month, to be honest, I just want to get it for three weeks. Yes, in international practice for a month, I also want to cut it for a week and take a rest. He actually threatened me, he expressed his bad attitude, indicating that he could make me feel embarrassed when I did not leave. Now I started to pick out my work problems, and my attitude is no longer enough. I am a little overwhelmed, haha. I think, if you do this to me, I will not do my best to hand over the road. If you do your job, you will leave a hole and you will fill it out slowly.

In the past month, I have experienced some things and taught me how to invest in myself and make myself stronger so that I can survive the storm. I was lucky enough to escape the storm under someone else's tree. Today may have passed through the Customs. It's not good if I can pass the bag tomorrow. In my opinion, it is a bit high to not be subject to human requirements. It is good to be able to be unable to be totally subject to human beings.

Subject:

The basic knowledge about NiO has been recorded in (Series 17). Continue to understand the buffer.

Buffer API:

 Int Capacity () //  Returns the buffer capacity. Buffer clear () //  Clear this buffer. Buffer flip () // This buffer zone is reversed.  Boolean Hasremaining () //  Determines whether there are any elements between the current position and the limit.  Abstract    Boolean Isreadonly () //  Determines whether the buffer is a read-only buffer.  Int Limit () //  Returns the buffer limit. Buffer limit ( Int Newlimit) //  Set the buffer limit. Buffer mark ()//  Set its flag at the location of this buffer zone.  Int Position () //  Returns the location of the buffer. Buffer position ( Int Newposition) //  Set the location of the buffer.  Int Remaining () //  Returns the number of elements between the current position and the limit. Buffer reset () //  Reset the location of the buffer to the previously marked location. Buffer rewind () // Repeat this buffer zone. 

Let's look at the source code. The flip and clear methods are detailed in series 17.

 Public   Abstract   Class  Buffer {  //  Invariants: Mark <= position <= Limit <= capacity      Private   Int Mark =-1; //  A memo location      Private   Int Position = 0; // Location, the next index of the element to be read or written, based on which to determine the read location      Private   Int Limit; //  Upper limit, the first element in the buffer zone that cannot be read or written.      Private   Int Capacity; //  Buffer capacity. The buffer is set when it is created and cannot be changed.  //  Initialization Buffer ( Int Mark, Int POs, Int Lim,Int CAP ){ //  Package-Private      If (Cap <0 )  Throw   New  Illegalargumentexception ();  This . Capacity = CAP; limit (lim); position (POS );  If (Mark> = 0 ){  If (Mark> Pos) Throw   New  Illegalargumentexception ();  This . Mark = Mark ;}}  Public   Final   Int  Capacity (){  Return  Capacity ;}  Public   Final   Int  Position (){ Return  Position ;}  Public   Final Buffer position ( Int  Newposition ){  If (Newposition> limit) | (newposition <0 ))  Throw   New  Illegalargumentexception (); Position = Newposition;  If (Mark> position) mark =-1;//  Because the position is reset, the mark may exceed it, and the mark is restored simply.  //  Return to the new user after setting  //  This method can be used for Cascade calls, such as buffer. Mark (). Position (5). Reset ();      Return   This  ;}  Public   Final   Int  Limit (){  Return  Limit ;} Public   Final Buffer limit ( Int  Newlimit ){  If (Newlimit> capacity) | (newlimit <0 ))  Throw   New  Illegalargumentexception (); Limit = Newlimit;  //  The limit settings involve the following: Position and Mark      If (Position> limit) Position =Limit;  If (Mark> limit) mark =-1 ;  Return   This  ;}  Public   Final  Buffer mark () {mark = Position;  Return   This  ;}  Public   Final Buffer reset (){  Int M = Mark;  If (M <0 )  Throw   New  Invalidmarkexception (); Position = M; //  Set the position to the position of the mark. The next time you read the data, you can start reading from the mark position.      Return   This  ;}  Public  Final  Buffer clear () {position = 0; //  Position resetting Limit = capacity; //  Limit Initialization Mark =-1; //  Mark Initialization  //  Three steps to operate this buffer again. You can continue to use it. The next write is overwriting.  //  Note that the clear method returns itself, because the clear method does not actually clear its own content. If the buffer returned by our operation at this time, this buffer has content.      Return  This  ;}  Public   Final  Buffer flip () {limit = Position; Position = 0 ; Mark =-1 ;  Return   This  ;}  Public   Final  Buffer rewind () {position = 0; // Unlike the clear () operation, the limit Initialization is missing, so that you can re-read the buffer. Mark =-1 ;  Return   This  ;}  //  Returns the number of elements between the current position and the limit.      Public   Final   Int  Remaining (){  Return Limit- Position ;}  //  Determines whether there are any elements between the current position and the limit.     Public   Final   Boolean  Hasremaining (){  Return Position < Limit ;}  //  All buffers are readable, but not all are writable.      Public   Abstract   Boolean  Isreadonly ();  //  There are nextgetindex (), nextgetindex (INT Nb) and other methods later, because it is not an external method, we will not discuss it further. }

Bytebuffer API:

1. heapbytebuffer implements the methods implemented in the abstract class bytebuffer.

2. The Get Set Method in bytebuffer is an array operation.

Four Methods of get:

Get ();
Get (INT index );
Get (byte [] DST );
Get (byte [] DST, int offset, int length );

The first two methods are easy to understand and return the obtained content. The next two methods are to transmit the bytes of the buffer to the given target array.

Let's look at the source code:

 Public Bytebuffer get ( Byte  [] DST ){  Return Get (DST, 0 , DST. Length );}  Public Bytebuffer get ( Byte [] DST, Int Offset,Int  Length) {checkbounds (offset, length, DST. Length );  If (Length> Remaining ())  Throw   New  Bufferunderflowexception ();  Int End = offset + Length;  //  Finally, take one by one      For ( Int I = offset; I <end; I ++) DST [I] = Get ();  Return   This  ;} 

Put method API:

Abstract bytebuffer put (byte B)
Bytebuffer put (byte [] SRC)
Bytebuffer put (byte [] SRC, int offset, int length)
Bytebuffer put (bytebuffer SRC)
Abstract bytebuffer put (INT index, byte B)

 

Compression Method:

 
Public AbstractBytebuffer compact ();

See, before use:

After use:

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.