nio之Buffer allocate allocateDirect

2017-02-08 21:18:00
admin
原创 3345
摘要:nio之Buffer allocate allocateDirect

一、nio之Buffer

1、ByteBuffer.allocate分配的内存在JVM里面,返回ByteBuffer的子类HeapByteBuffer,初始值0;

2、ByteBuffer.allocateDirect分配的内存在系统里面,返回MappedByteBuffer的子类DirectByteBuffer,初始值0;

3系统内存分配速度慢,但使用效率更高,并且不参与垃圾回收;

4buffer的四个属性,0 <= mark <= position <= limit <= capacity,position决定了read和write的起始位置;

5mark用于reset使用,没有mark调用reset会抛异常,初始状态无标记,只有position小于mark时标记被丢弃;

6buffer.get()和buffer.put(byte)都是相对方法,都会导致position++;

7buffer.get(int)和buffer.put(int, byte)都是绝对方法,不会导致position变化;

8clear只是将position设置为0,limit设置为capacity,并不清除数据,做好put准备;

9flip将limit设置为position,position设置为0,做好get准备;

10rewind将position设置为0,做好重新get准备;

11remaining表示position到limit之间的元素数量;

12compact将position到limit之间的元素移动到数组开始;

13多线程访问是不安全的,需要使用同步手段进行控制;

14ByteOrder.nativeOrder()获取本机字节序列;

15视图和allocateDirect情况下hasArray返回false;

16array()方法返回底层数组;


属性测试:

ByteBuffer buffer = ByteBuffer.allocate(12);
System.out.println(buffer);
System.out.println(buffer.position(2).mark());
System.out.println(buffer.position(4));
System.out.println(buffer.reset());
System.out.println(buffer.position(0));
buffer.reset(); //抛出InvalidMarkException异常


输出:

java.nio.HeapByteBuffer[pos=0 lim=12 cap=12]
java.nio.HeapByteBuffer[pos=2 lim=12 cap=12]
java.nio.HeapByteBuffer[pos=4 lim=12 cap=12]
java.nio.HeapByteBuffer[pos=2 lim=12 cap=12]
java.nio.HeapByteBuffer[pos=0 lim=12 cap=12]


数据操作,默认大端模式:

ByteBuffer buffer = ByteBuffer.allocate(12);
byte by = 1;
int in = 32;
buffer.put(by);
buffer.putInt(in);
System.out.println(buffer);
System.out.println(buffer.get(4));


输出:

java.nio.HeapByteBuffer[pos=5 lim=12 cap=12]
32


duplicate复制原buffer所有状态,slice将position和limit之间的内容映射成新buffer,两者都使用原来内存:

1、duplicate复制mark值,slice清除mark值;

2、asReadOnlyBuffer与duplicate完成相同,除了缓冲区只读以外;

ByteBuffer buffer = ByteBuffer.allocate(12);
byte by = 1;
int in = 32;
buffer.put(by);
buffer.putInt(in);
System.out.println(buffer);
System.out.println(buffer.array());
System.out.println(buffer.duplicate());
System.out.println(buffer.duplicate().array());
buffer.position(4);
System.out.println(buffer.slice());
System.out.println(buffer.slice().array());
System.out.println(buffer.slice().arrayOffset());


输出:

java.nio.HeapByteBuffer[pos=5 lim=12 cap=12]
[B@1c0ec97
java.nio.HeapByteBuffer[pos=5 lim=12 cap=12]
[B@1c0ec97
java.nio.HeapByteBuffer[pos=0 lim=8 cap=8]
[B@1c0ec97

4


数组转换到ByteBuffer

byte[] bytes = new byte[12];
ByteBuffer buf = ByteBuffer.wrap(bytes, 1, 1);
System.out.println(buf);
System.out.println(bytes);
System.out.println(buf.array());
System.out.println(buf.arrayOffset());


输出:

java.nio.HeapByteBuffer[pos=1 lim=2 cap=12]
[B@ecb281
[B@ecb281
0


大小端

ByteBuffer buf = ByteBuffer.allocate(12);
System.out.println(buf.order());
System.out.println(buf.order(ByteOrder.LITTLE_ENDIAN).order());


输出:

BIG_ENDIAN
LITTLE_ENDIAN


IntBuffer测试

ByteBuffer buffer = ByteBuffer.allocate(16);
buffer.position(4);
IntBuffer intBuf = buffer.asIntBuffer();
System.out.println(intBuf);
System.out.println(intBuf.order());
System.out.println(intBuf.hasArray());
intBuf = IntBuffer.allocate(4);
System.out.println(intBuf);
System.out.println(intBuf.order());
System.out.println(intBuf.hasArray());


输出:

java.nio.ByteBufferAsIntBufferB[pos=0 lim=3 cap=3]
BIG_ENDIAN
false
java.nio.HeapIntBuffer[pos=0 lim=4 cap=4]
LITTLE_ENDIAN
true


compareTo对所有元素进行比较

ByteBuffer buf1 = ByteBuffer.allocate(4);
ByteBuffer buf2 = ByteBuffer.allocateDirect(4);
IntBuffer buf3 = IntBuffer.allocate(1);
System.out.println(buf1.compareTo(buf2));
System.out.println(buf1.asIntBuffer().compareTo(buf3));


输出:

0
0

发表评论
评论通过审核之后才会显示。