make和rest用法
位置(position):下一个要读取或写入的数据的索引。缓冲区的位置不能为负,并且不能大于其限制(limit)。
标记(mark)与重置(reset):标记是一个索引,通过Buffer中的mark()方法指定Buffer中一个特定的position,之后可以通过调用reset()方法恢复到这个position。
make与rest用法
标记(mark)与重置(reset):标记是一个索引,通过Buffer中的mark()方法指定Buffer中一个特定的position,之后可以通过调用reset()方法恢复到这个position。
还原到最初打标机的位置
重置(reset)
package com.toov5.Nio;
import java.nio.ByteBuffer;
public class BUfferTest01 {
public static void main(String[] args) {
ByteBuffer byteBuffer = ByteBuffer.allocate(10); //10个字节
String str="abcd";
byteBuffer.put(str.getBytes());
//开启读模式
byteBuffer.flip();
byte[] bytes = new byte[byteBuffer.limit()];
//缓冲拿到数组
byteBuffer.get(bytes,0,2);
byteBuffer.mark(); //做个标记
System.out.println(new String(bytes,0,2)); //从0到2的位置
System.out.println(byteBuffer.position());
System.out.println("--------------------");
byteBuffer.reset() ; //还原到标记位置
byteBuffer.get(bytes,0,2);
System.out.println(new String(bytes,0,2));
System.out.println(byteBuffer.position());
}
}