The difference between the clear (), rewind (), and Flip () methods of the NIO buffer

Source: Internet
Author: User
Tags rewind

There are several common approaches to buffer in the Java NIO, such as Clear,rewind and flip. The following gives you this three ways of source code, convenient for everyone to remember.
The clear () method is used for write mode, which acts as the content in buffer, so-called emptying refers to the same write upper limit as the real capacity of buffer, that is, limit==capacity, while the current write position is placed at the forefront Subscript 0. The code is as follows:

Public final Buffer Clear () {          position = 0;//set current subscript to 0         limit = capacity;//Set write cross-border to same as Buffer capacity         mark = 1;//Cancel Mark         

Rewind () is available in read/write mode, it simply places the current position at 0 and cancels the mark tag, which means that the limit in write mode remains the same as the buffer capacity, only to write it again, while the limit in read mode is still the same as before the rewind () call. That is, flip () calls the last position of the position in the previous write mode, and after the flip () call this position becomes the limit position of the read mode, i.e. the cross-border position, the code is as follows:

Public final Buffer Rewind () {         position = 0;         Mark =-1;         
The flip () function is to convert the write mode to read mode, the final position of the content in the buffer in the write mode becomes the limit position in the read mode, as the read out of bounds position, at the same time the current read position is set to 0, indicating that after the conversion to start reading, while eliminating the mark mark in write mode, The code is as follows
Public final Buffer Flip () {         limit = position;         Position = 0;         Mark =-1;         return this;  }  
In these three modes, the capacity of the buffer is constant, and the value is always the same.








The difference between the clear (), rewind (), and Flip () methods of the NIO buffer

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.