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