public abstract class Buffer {
private int mark = -1;
private int position = 0;
private int limit; // 缓冲区中第一个不能读或写的元素
private int capacity;// 缓冲区中数据元素的最大容量
long address;
// 包似有的构造函数,用mark,pos,lim,cap初始化Buffer
Buffer(int mark, int pos, int lim, int cap) {
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 int limit() {
return limit;
}
// 做标记,把当前的pos位置记在mark中
// 以后如果需要,可以通过调用reset()返回到当前位置
public final Buffer mark() {
mark = position;
return this;
}
// 如果mark已被设置,把当前位置设为mark值
// 当mark值没有被设置时,初始为1,抛出异常
public final Buffer reset() {
int m = mark;
if (m < 0)
throw new InvalidMarkException();
position = m;
return this;
}
// 设置缓冲区的下一个待读或写元素的新位置
public final Buffer position(int newPosition) {
if ((newPosition > limit) || (newPosition < 0))
throw new IllegalArgumentException();
position = newPosition;
if (mark > position)
mark = -1;
return this;
}
// 设置limit
public final Buffer limit(int newLimit) {
if ((newLimit > capacity) || (newLimit < 0))
throw new IllegalArgumentException();
limit = newLimit;
if (position > limit)
position = limit;
if (mark > limit)
mark = -1;
return this;
}
//
public final Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
return this;
}
// 在向缓冲区填数据时,可能填到一半,你希望不再写数据进去,而是希望
// 读取该缓冲区里的数据,flip方法就是在你希望对缓冲区由写状态变成
// 读状态时使用
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
// 与flip类似
public final Buffer rewind() {
position = 0;
mark = -1;
return this;
}
// 返回当前位置和上界的差值
public final int remaining() {
return limit - position;
}
// 判断是否已经到达上界
public final boolean hasRemaining() {
return position < limit;
}
public abstract boolean isReadOnly();
public abstract boolean hasArray();
public abstract Object array();
public abstract int arrayOffset();
public abstract boolean isDirect();
// -- Package-private methods for bounds checking, etc. --
// 检查是否到达limit位置,如果没有,就pos++
// 返回当前的位置
final int nextGetIndex() { // package-private
if (position >= limit)
throw new BufferUnderflowException();
return position++;
}
// 检查当前pos加上nb后是否会超过limit,超过抛出异常
// 当前pos + = nb;
// 返回当前位置
final int nextGetIndex(int nb) { // package-private
if (limit - position < nb)
throw new BufferUnderflowException();
int p = position;
position += nb;
return p;
}
final int nextPutIndex() { // package-private
if (position >= limit)
throw new BufferOverflowException();
return position++;
}
final int nextPutIndex(int nb) { // package-private
if (limit - position < nb)
throw new BufferOverflowException();
int p = position;
position += nb;
return p;
}
final int checkIndex(int i) { // package-private
if ((i < 0) || (i >= limit))
throw new IndexOutOfBoundsException();
return i;
}
final int checkIndex(int i, int nb) { // package-private
if ((i < 0) || (nb > limit - i))
throw new IndexOutOfBoundsException();
return i;
}
final int markValue() { // package-private
return mark;
}
//.....
static void checkBounds(int off, int len, int size) { // package-private
if ((off | len | (off + len) | (size - (off + len))) < 0)
throw new IndexOutOfBoundsException();
}
}
Buffer源码解读
最新推荐文章于 2021-09-30 11:00:24 发布