一、nio之Buffer
1 ByteBuffer.allocate分配的内存在JVM里面,返回ByteBuffer的子类HeapByteBuffer,初始值0;
2 ByteBuffer.allocateDirect分配的内存在系统里面,返回MappedByteBuffer的子类DirectByteBuffer,初始值0;
3 系统内存分配速度慢,但使用效率更高,并且不参与垃圾回收;
4 buffer的四个属性&#xff0c;0 <&#61; mark <&#61; position <&#61; limit <&#61; capacity&#xff0c;position决定了read和write的起始位置&#xff1b;
5 mark用于reset使用&#xff0c;没有mark调用reset会抛异常&#xff0c;初始状态无标记&#xff0c;只有position小于mark时标记被丢弃&#xff1b;
6 buffer.get()和buffer.put(byte)都是相对方法&#xff0c;都会导致position&#43;&#43;&#xff1b;
7 buffer.get(int)和buffer.put(int, byte)都是绝对方法&#xff0c;不会导致position变化&#xff1b;
8 clear只是将position设置为0&#xff0c;limit设置为capacity&#xff0c;并不清除数据&#xff0c;做好put准备&#xff1b;
9 flip将limit设置为position&#xff0c;position设置为0&#xff0c;做好get准备&#xff1b;
10 rewind将position设置为0&#xff0c;做好重新get准备&#xff1b;
11 remaining表示position到limit之间的元素数量&#xff1b;
12 compact将position到limit之间的元素移动到数组开始&#xff1b;
13 多线程访问是不安全的&#xff0c;需要使用同步手段进行控制&#xff1b;
14 ByteOrder.nativeOrder()获取本机字节序列&#xff1b;
15 视图和allocateDirect情况下hasArray返回false&#xff1b;
16 array()方法返回底层数组&#xff1b;
1 属性测试&#xff1a;
ByteBuffer buffer &#61; 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异常
输出&#xff1a;
java.nio.HeapByteBuffer[pos&#61;0 lim&#61;12 cap&#61;12]
java.nio.HeapByteBuffer[pos&#61;2 lim&#61;12 cap&#61;12]
java.nio.HeapByteBuffer[pos&#61;4 lim&#61;12 cap&#61;12]
java.nio.HeapByteBuffer[pos&#61;2 lim&#61;12 cap&#61;12]
java.nio.HeapByteBuffer[pos&#61;0 lim&#61;12 cap&#61;12]
2 数据操作&#xff0c;默认大端模式&#xff1a;
ByteBuffer buffer &#61; ByteBuffer.allocate(12);
byte by &#61; 1;
int in &#61; 32;
buffer.put(by);
buffer.putInt(in);
System.out.println(buffer);
System.out.println(buffer.get(4));
输出&#xff1a;
java.nio.HeapByteBuffer[pos&#61;5 lim&#61;12 cap&#61;12]
32
3duplicate复制原buffer所有状态&#xff0c;slice将position和limit之间的内容映射成新buffer&#xff0c;两者都使用原来内存&#xff1a;
1、duplicate复制mark值&#xff0c;slice清除mark值&#xff1b;
2、asReadOnlyBuffer与duplicate完成相同&#xff0c;除了缓冲区只读以外&#xff1b;
ByteBuffer buffer &#61; ByteBuffer.allocate(12);
byte by &#61; 1;
int in &#61; 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());
输出&#xff1a;
java.nio.HeapByteBuffer[pos&#61;5 lim&#61;12 cap&#61;12]
[B&#64;1c0ec97
java.nio.HeapByteBuffer[pos&#61;5 lim&#61;12 cap&#61;12]
[B&#64;1c0ec97
java.nio.HeapByteBuffer[pos&#61;0 lim&#61;8 cap&#61;8]
[B&#64;1c0ec97
4
4 数组转换到ByteBuffer
byte[] bytes &#61; new byte[12];
ByteBuffer buf &#61; ByteBuffer.wrap(bytes, 1, 1);
System.out.println(buf);
System.out.println(bytes);
System.out.println(buf.array());
System.out.println(buf.arrayOffset());
输出&#xff1a;
java.nio.HeapByteBuffer[pos&#61;1 lim&#61;2 cap&#61;12]
[B&#64;ecb281
[B&#64;ecb281
0
5 大小端
ByteBuffer buf &#61; ByteBuffer.allocate(12);
System.out.println(buf.order());
System.out.println(buf.order(ByteOrder.LITTLE_ENDIAN).order());
输出&#xff1a;
BIG_ENDIAN
LITTLE_ENDIAN
6 IntBuffer测试
ByteBuffer buffer &#61; ByteBuffer.allocate(16);
buffer.position(4);
IntBuffer intBuf &#61; buffer.asIntBuffer();
System.out.println(intBuf);
System.out.println(intBuf.order());
System.out.println(intBuf.hasArray());
intBuf &#61; IntBuffer.allocate(4);
System.out.println(intBuf);
System.out.println(intBuf.order());
System.out.println(intBuf.hasArray());
输出&#xff1a;
java.nio.ByteBufferAsIntBufferB[pos&#61;0 lim&#61;3 cap&#61;3]
BIG_ENDIAN
false
java.nio.HeapIntBuffer[pos&#61;0 lim&#61;4 cap&#61;4]
LITTLE_ENDIAN
true
7 compareTo对所有元素进行比较
ByteBuffer buf1 &#61; ByteBuffer.allocate(4);
ByteBuffer buf2 &#61; ByteBuffer.allocateDirect(4);
IntBuffer buf3 &#61; IntBuffer.allocate(1);
System.out.println(buf1.compareTo(buf2));
System.out.println(buf1.asIntBuffer().compareTo(buf3));
输出&#xff1a;
0
0