ByteBuffer缓冲区的三种创建方式简介说明
下文笔者讲述ByteBuffer创建缓冲区的三种方法分享,如下所示:
1、allocate()创建堆缓冲区
ByteBuffer buffer1 = ByteBuffer.allocate(10);
allocate()方法的功能
分配一个新的非直接字节缓冲区
position为0,limit为capacity,mark不确定
它将具有一个底层实现数组,且数组偏移量为0。
底层源码:
public static ByteBuffer allocate(int capacity) {
if (capacity < 0)
throw new IllegalArgumentException();
return new HeapByteBuffer(capacity, capacity);
}
class HeapByteBuffer
extends ByteBuffer
{
HeapByteBuffer(int cap, int lim) {
super(-1, 0, lim, cap, new byte[cap], 0);
}
}
2.allocateDirect()创建直接缓冲区
ByteBuffer buffer2 = ByteBuffer.allocateDirect(10);
allocateDirect()的功能:
分配新的直接字节缓冲区。
position为0,limit为capacity,mark不确定。
在JVM层面没有实现数组
源码:
public static ByteBuffer allocateDirect(int capacity) {
return new DirectByteBuffer(capacity);
}
class DirectByteBuffer extends MappedByteBuffer implements DirectBuffer
3.wrap()创建堆缓冲区
ByteBuffer buffer3 = ByteBuffer.wrap(new byte[]{1,2,3,4},0,3);
wrap(byte[]array) 方法的功能:
将byte数组包装到缓冲区中。
新的缓冲区将由给定的byte数组支持,也就是说,缓冲区修改将导致数组的修改,反之亦然。
新缓冲区的capacity和limit将为array.length,其位置position将为0,其标记mark是不确定的。
其底层实现数组将为给定数组,并且其arrayOffset将为0
wrap(byte[]array,int offset,int length)方法的作用:
新缓冲区的capacity将为array.length,其position为offset(0<offset<=array.length),limit为offset+length(0<length<=array.length-offset)
其不具备subString()方法截取功能,它的参数offset只是设置缓冲区的position值,而length确定limit值。
(其他与上个方法wrap(byte[]array) 一致)
public static ByteBuffer wrap(byte[] array,
int offset, int length)
{
try {
return new HeapByteBuffer(array, offset, length);
} catch (IllegalArgumentException x) {
throw new IndexOutOfBoundsException();
}
}
HeapByteBuffer(byte[] buf, int off, int len) {
super(-1, off, off + len, buf.length, buf, 0);
}
直接缓冲区和非直接缓冲区的比较:
直接缓冲区:
直接缓冲区在内部使用sun.misc.Unsafe类进行值的处理。Unsafe类的作用是JVM与操作系统进行直接通信
直接缓冲区操作的数据不在JVM堆中,而是在内核空间中,这样提高运行效率
通过allacateDirect()返回的缓冲区进行内存的分配和释放所需的时间成本通常要高于非直接缓冲区,但运行效率远比非直接高
非直接缓冲区:
在内部直接对数组进行操作,并且是在JVM的堆中进行数组处理
直接缓冲区提高运行效率的原理是每次调用基于操作系统的I/O操作之前或之后,JVM都会尽量避免将缓冲区的内容复制到中间缓冲区,或从中间缓冲区中获取内容
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


