nio-Buffer

来源:互联网 发布:json文件dw打开乱码 编辑:程序博客网 时间:2024/05/16 08:54

Buffer属性

capacity: buffer的容量 
A buffer's capacity is the number of elements it contains.  The capacity of a buffer is never negative and never changes. limit: 写时limit=capacity,读时limit=buffer内数据的长度
A buffer's limit is the index of the first element that should not be read or written.  A buffer's limit is never negative and is never greater than its capacity.   position:下一个读写的位置
A buffer's position is the index of the next element to be read or written.  A buffer's position is never negative and is never greater than its limit.  

Buffer方法

flip:
把limit设为当前position,把position设为0,一般在从Buffer读出数据前调用。
public final Buffer flip() {    limit = position;    position = 0;    mark = -1;    return this;}
clear:
把position设为0,把limit设为capacity,一般在把数据写入Buffer前调用。
public final Buffer clear() {    position = 0;    limit = capacity;    mark = -1;    return this;}
rewind:
把position设为0,limit不变,一般在把数据重写入Buffer前调用。
public final Buffer rewind() {    position = 0;    mark = -1;    return this;}

0 0
原创粉丝点击