在NIO中,有两种分配字节的方法:
在堆中初始缓冲区
public static ByteBuffer allocate(int capacity) {
if (capacity < 0)
throw createCapacityException(capacity);
return new HeapByteBuffer(capacity, capacity);
}
以及
在堆外创建缓冲区
public static ByteBuffer allocateDirect(int capacity) {
return new DirectByteBuffer(capacity);
}
allocate()方法本质是调用HeapByteBuffer()
// Cached array base offset
//缓存数组基本偏移量
//UNSAFE.arrayBaseOffset(Class class)是一个本地方法,能够获取数组的第一个元素的偏移地址
private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
// Cached array base offset
private static final