重要位置
// Invariants: mark <= position <= limit <= capacity
private int mark = -1;
private int position = 0;
private int limit;
private int capacity;
a. capacity:容量位。用于标记当前缓冲区的容量/大小
b. limit:限制位。用于限定position所能达到的最大下标。默认和容量位是重合的
c. position:操作位。类似于数组中的下标,用于指向要操作的位置。默认是第0位
d.mark:标记位。用于进行标记,通常是用于避免数据大批量产生错误。注意,标记位
默认是不启用的
重要操作
a. flip:翻转缓冲区。将limit挪到position上,然后将position归零,标记位置为-1
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
b. clear:清空缓冲区。position归零,limit挪到capacity上,mark置为-1
public final Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
return this;
}
c. reset:重置缓冲区。将position挪到mark上
public final Buffer reset() {
int m = mark;
if (m < 0)
throw new InvalidMarkException();
position = m;
return this;
}
d. rewind:重绕缓冲区。将position归零,将mark置为-1
public final Buffer rewind() {
position = 0;
mark = -1;
return this;
}