缓冲区(Buffer)
一、定义
缓冲区(Buffer):缓冲区本质上是一个可以读写数据的内存块,可以理解成一个容器对象(数组),该对象提供了一组方法,可以轻松的使用内存块,缓冲区对象内置了一些机制,能够跟踪和记录缓冲区的状态变化情况。
二、Buffer类及其子类
子类 | 存储类型 |
---|
byteBuffer | 存储字节数据类型 |
ShortBuffer | 存储短整数数字类型 |
CharBuffer | 存储字符数据类型 |
IntBuffer | 存储整数数据类型 |
LongBuffer | 存储长整数数据类型 |
DoubleBuffer | 存储双精度小数类型 |
FloatBuffer | 存储单精度小数类型 |
三、Buffer类的属性
属性 | 描述 |
---|
Capacity | 容量,既可以容纳的最大数据量,在缓冲区创建时被设定,并且不能改变 |
Limit | 表示缓冲区的当前终点,不能队超过极限的位置进行读写操作。极限是可以修改的 |
Position | 位置,下一个要被读或写的元素的索引,每次读写缓冲区数据时都会改变该值 |
Mark | 标记 |
四、Buffer主要操作API
方法 | 描述 |
---|
public static ByteBuffer allocateDirect(int capacity) | 创建直接缓冲区,并初始化容量 |
public static ByteBuffer allocate(int capacity) | 创建非直接缓冲区,并初始化容量 |
public static ByteBuffer wrap(byte[] array) | 把一个数组放到缓冲区中使用 |
public static ByteBuffer wrap(byte[] array,int offset,int length) | 从offset开始截取length长度的数组,放到缓冲区中使用 |
public abstract byte get() | 获取索引为position上的值,position加1 |
public abstract byte get(int index) | 获取指定位置上的值 |
public abstract ByteBuffer put(byte b) | 为索引为position的位置赋值 |
public abstract ByteBuffer put(int index,byte b) | 指定位置上赋值 |
五、Buffer常用API
方法 | 描述 |
---|
JDK1.4版本 | |
public final int capacity() | 返回此缓冲区的容量 |
public final int position() | 返回此缓冲区的位置 |
public final Buffer position(int newPosition) | 设置此缓冲区的位置 |
public final int limit() | 返回此缓冲区的限制 |
public final Buffer limit(int newLimit) | 设置此缓冲区的限制 |
public final Buffer mark() | 在此缓冲区的位置设置标记 |
public final Buffer reset() | 将此缓冲区的位置重置为以前标记的位置 |
public final Buffer clear() | 清除此缓冲区,将各个标记恢复到初始状态,但是数据并没有真正擦除 |
public final Buffer filp() | 反转此缓冲区 |
public final Buffer rewind() | 重绕此缓冲区 |
public final int remaining() | 返回当前位置与限制之间的元素数 |
public final boolean hsaRemaining() | 告知当前位置和限制之间是否有元素 |
public abstract boolean isReadOnly() | 告知此缓冲区是否为只读缓冲区 |
JDK1.6版本 | |
public abstract boolean hasArray() | 告知此缓冲区是否具有可访问的底层实现数组 |
public abstract Object array() | 返回此缓冲区的底层实现数据 |
public abstract int arrayOffset() | 返回此缓冲区的底层实现数据中第一个缓冲区元素的偏移量 |
public abstract boolean isDirect() | 告知此缓冲区是否为直接缓冲区 |
六、Buffer练习
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class NioBuffer {
public static void main(String[] args) {
}
public void test03(){
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
System.out.println("是否为直接缓冲区:"+byteBuffer.isDirect());
}
public void test02(){
String str = "abcdef";
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
byteBuffer.put(str.getBytes(StandardCharsets.UTF_8));
byteBuffer.flip();
byte[] bst = new byte[byteBuffer.limit()];
byteBuffer.get(bst,0,2);
System.out.println("byte数组:"+ new String(bst,0,2));
System.out.println("缓存操作输位置:"+byteBuffer.position());
System.out.println("-----------------------------");
byteBuffer.mark();
byteBuffer.get(bst,2,2);
System.out.println("byte数组:"+ new String(bst,2,2));
System.out.println("缓存操作输位置:"+byteBuffer.position());
System.out.println("-----------------------------");
byteBuffer.reset();
System.out.println("缓存操作输位置:"+byteBuffer.position());
System.out.println("-----------------------------");
if (byteBuffer.hasRemaining()){
System.out.println("缓冲区可操作数量剩余:"+byteBuffer.remaining());
System.out.println("-----------------------------");
}
}
public void test01(){
String str = "abcde";
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
System.out.println("-----------allocate()-----------");
System.out.println("缓冲区容量:"+byteBuffer.capacity());
System.out.println("缓冲区界限:"+byteBuffer.limit());
System.out.println("缓冲区操作数位置:"+byteBuffer.position());
byteBuffer.put(str.getBytes(StandardCharsets.UTF_8));
System.out.println("-----------put()-----------");
System.out.println("缓冲区容量:"+byteBuffer.capacity());
System.out.println("缓冲区界限:"+byteBuffer.limit());
System.out.println("缓冲区操作数位置:"+byteBuffer.position());
byteBuffer.flip();
System.out.println("-----------flip()-----------");
System.out.println("缓冲区容量:"+byteBuffer.capacity());
System.out.println("缓冲区界限:"+byteBuffer.limit());
System.out.println("缓冲区操作数位置:"+byteBuffer.position());
byte[] dst = new byte[byteBuffer.limit()];
byteBuffer.get(dst);
System.out.println(new String(dst,0,dst.length));;
System.out.println("-----------get()-----------");
System.out.println("缓冲区容量:"+byteBuffer.capacity());
System.out.println("缓冲区界限:"+byteBuffer.limit());
System.out.println("缓冲区操作数位置:"+byteBuffer.position());
byteBuffer.rewind();
System.out.println("-----------get()-----------");
System.out.println("缓冲区容量:"+byteBuffer.capacity());
System.out.println("缓冲区界限:"+byteBuffer.limit());
System.out.println("缓冲区操作数位置:"+byteBuffer.position());
byteBuffer.clear();
System.out.println("-----------clear()-----------");
System.out.println("缓冲区容量:"+byteBuffer.capacity());
System.out.println("缓冲区界限:"+byteBuffer.limit());
System.out.println("缓冲区操作数位置:"+byteBuffer.position());
}
}