buffer基础
1.容量(Capacity):缓冲区能够容纳的数据元素的最大数量,这一容量在缓冲区创建时被设定,并且永远不能被改变。
2.上界(Limit):缓冲区的第一个不能被读或被写的元素,或者说,缓冲区现存元素的计数。
3.位置(Position):下一个要读或要写的元素的索引。
4.标记(Mark):一个备忘位置,调用mark mark = position,调用reset position = mark。
//大小排序
0 mark postion limit capacity
可读
//isReadOnly 是否只可读
ByteBuffer byteBuffer=ByteBuffer.allocate(100)
System.out.println(byteBuffer.isReadOnly())
存取
put:将元素放入缓存中,position后移
get:获取当前position的元素,position后移
put
public ByteBuffer put(byte x) {
hb[ix(nextPutIndex())] = x;
return this;
}
public ByteBuffer put(int i, byte x) {
hb[ix(checkIndex(i))] = x;
return this;
}
final int nextPutIndex() {
if (position >= limit)
throw new BufferOverflowException();
return position++;
}
final int checkIndex(int i) {
if ((i < 0) || (i >= limit))
throw new IndexOutOfBoundsException();
return i;
}
get
public byte get() {
return hb[ix(nextGetIndex())];
}
public byte get(int i) {
return hb[ix(checkIndex(i))];
}
test
System.out.println("postion : " + buffer.position())
buffer.put((byte) 'a')
System.out.println("postion : " + buffer.position())
buffer.put((byte) 23)
System.out.println("postion : " + buffer.position())
buffer.put((byte) 189)
System.out.println("postion : " + buffer.position())
System.out.println(buffer.get())
buffer.put(4, (byte)12)
System.out.println("postion : " + buffer.position())
System.out.println(buffer.get())
System.out.println("postion : " + buffer.position())
System.out.println(buffer.get(1))
//输出
postion : 0
postion : 1
postion : 2
postion : 3
0
postion : 4
12
postion : 5
23
翻转
flip:将一个能够继续添加数据元素的填充状态的缓冲区翻转成一个准备读出元素的释放状态
rewind:重读已经被翻转的缓冲区中的数据
flip
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
flip 等效于 buffer.limit(buffer.position()).position(0);
rewind
public final Buffer rewind() {
position = 0;
mark = -1;
return this;
}
rewind 等效于 buffer.position(0);
test
ByteBuffer buffer=ByteBuffer.allocate(100)
System.out.println("postion : " + buffer.position())
buffer.put((byte) 'h').put((byte) 'e').put((byte) 'l').put((byte) 'l').put((byte) 'o')
System.out.println("postion : " + buffer.position())
System.out.println(buffer.get())
buffer.flip()
System.out.print((char)buffer.get())
System.out.print((char)buffer.get())
System.out.print((char)buffer.get())
System.out.print((char)buffer.get())
System.out.println((char)buffer.get())
System.out.println("postion : " + buffer.position())
buffer.rewind()
System.out.println("postion : " + buffer.position())
//输出
postion : 0
postion : 5
0
hello
postion : 5
postion : 0
释放
hasRemaining
public final boolean hasRemaining() {
return position < limit;
}
remaining
public final int remaining() {
return limit - position;
}
test
ByteBuffer buffer=ByteBuffer.allocate(100)
buffer.put((byte) 'h').put((byte) 'e').put((byte) 'l').put((byte) 'l').put((byte) 'o')
System.out.println("remaining :" +buffer.remaining())
buffer.flip()
System.out.println("remaining :" +buffer.remaining())
while (buffer.hasRemaining()) {
System.out.print((char)buffer.get())
}
buffer.rewind()
System.out.println()
while (buffer.hasRemaining()) {
System.out.print((char)buffer.get())
}
//输出
remaining :95
remaining :5
hello
hello
压缩
compact:丢弃已经释放的数据,保留未释放的数据,并使缓冲区对重新填充容量准备就绪。
compact
public ByteBuffer compact() {
System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
position(remaining());
limit(capacity());
discardMark();
return this;
}
test
ByteBuffer buffer=ByteBuffer.allocate(100)
buffer.put((byte) 'h').put((byte) 'e').put((byte) 'l').put((byte) 'l').put((byte) 'o')
buffer.flip()
System.out.println((char)buffer.get())
System.out.println((char)buffer.get())
buffer.compact()
buffer.put((byte) '&')
buffer.flip()
while (buffer.hasRemaining()) {
System.out.print((char)buffer.get())
}
//输出
h
e
llo&