import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
public class test_02 {
public final static int MAX_SIZE = 2000;
public static void main (String[] args) throws Exception {
//直接缓冲区:开启和释放时间成本较高,适合保存易受操作系统本机I/O操作影响的大量,长时间保存的数据
//创建方法:
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(MAX_SIZE);
//非直接缓冲区,
//创建方法:
ByteBuffer byteBuffer1 = ByteBuffer.allocate(MAX_SIZE);
//性能比较:
//直接缓冲区性能稍微高一点
byteBuffer=ByteBuffer.allocateDirect(MAX_SIZE);
Long beginTime = System.currentTimeMillis();
for (int i = 0 ; i < MAX_SIZE ; i++){
byteBuffer.put((byte)123456789);
}
Long endTime = System.currentTimeMillis();
System.out.println("直接缓冲区用时:");
System.out.println(endTime - beginTime);
byteBuffer1 = ByteBuffer.allocate(MAX_SIZE);
beginTime = System.currentTimeMillis();
for (int i = 0 ; i < MAX_SIZE ; i++){
byteBuffer1.put((byte)123456789);
}
endTime = System.currentTimeMillis();
System.out.println("非直接缓冲区用时:");
System.out.println(endTime - beginTime);
//释放内存:
//手动释放:
Method cleanerMethod = byteBuffer.getClass().getMethod("cleaner");
cleanerMethod.setAccessible(true);
Object returnValue = cleanerMethod.invoke(byteBuffer);
Method cleanMethod = returnValue.getClass().getMethod("clean");
cleanMethod.setAccessible(true);
cleanMethod.invoke(returnValue);
//JVM自动回收内存:进程结束后,JVM在某个时机触发垃圾回收器进行内存的回收
//wrap方法wrap(byte[] array,int offset , int length),偏移量,长度
byte[] bytes = new byte[]{1,2,3,4,5,6,7,8,9};
ByteBuffer byteBuffer2 = ByteBuffer.wrap(bytes,2,4);
while (byteBuffer2.remaining()>0){
System.out.println(byteBuffer2.get());
}
//输出:3 4 5 6
//put和get方法
//put方法
byte[] bytes1 = new byte[]{1,2,3,4,5,6,7,8,9,10};
byte[] bytes2 = new byte[]{11,22,33,44,55};
ByteBuffer byteBuffer3 = ByteBuffer.allocate(10);
byteBuffer3.put(bytes1);
byteBuffer3.rewind();
while (byteBuffer3.remaining()>0){
System.out.printf("%d ",byteBuffer3.get());
}
System.out.println("\n");
//从当前position位置开始加入,从加入数组的offset开始加,加length个,异常什么都不传输,因为是先判断的
byteBuffer3.position(2);
byteBuffer3.put(bytes2,2,3);
byteBuffer3.rewind();
while (byteBuffer3.remaining()>0){
System.out.printf("%d ",byteBuffer3.get());
}
//get方法
System.out.println("\n");
byte[] bytes3 = new byte[byteBuffer3.capacity()];
//从Buffer的position开始输出,加入到数组的offset开始,一共加length个,异常什么都不传输,因为是先判断的
byteBuffer3.position(2);
byteBuffer3.get(bytes3,3,4);
for (int i = 0; i < bytes3.length ; i++) {
System.out.print(bytes3[i]+" ");
}
System.out.println("\n");
//putType和getType方法
ByteBuffer byteBuffer4 = ByteBuffer.allocate(100);
byteBuffer4.putChar('a');//char两个个字节,占0-1
byteBuffer4.putChar('b');//2-3
byteBuffer4.position(4);
byteBuffer4.putDouble(1.1);//double占8个字节,4-11
byteBuffer4.putDouble(2.2);//12-19
byteBuffer4.position(20);
byteBuffer4.putFloat(3.3F);//float占4个字节 20-23
byteBuffer4.putFloat(4.4F);//24-27
byteBuffer4.position(28);
byteBuffer4.putInt(1);//int占4个字节28-31
byteBuffer4.putInt(2);//32-35
byteBuffer4.position(36);
byteBuffer4.putLong(4L);//long占8个字节 36-43
byteBuffer4.putLong(5L);//44-51
byteBuffer4.position(52);
byteBuffer4.putShort((short) 1);//short占2个字节 52-53
byteBuffer4.putShort((short)2);//54-55
byteBuffer4.position(0);
byte[] bytesOut = byteBuffer4.array();
for (int i = 0; i < bytesOut.length ; i++) {
System.out.print(bytesOut[i]+" ");
}
System.out.println();
System.out.println(byteBuffer4.getChar(0));
System.out.println(byteBuffer4.getChar(2));
System.out.println(byteBuffer4.getDouble(4));
System.out.println(byteBuffer4.getDouble(12));
System.out.println(byteBuffer4.getFloat(20));
System.out.println(byteBuffer4.getFloat(24));
System.out.println(byteBuffer4.getInt(28));
System.out.println(byteBuffer4.getInt(32));
System.out.println(byteBuffer4.getLong(36));
System.out.println(byteBuffer4.getLong(44));
System.out.println(byteBuffer4.getShort(52));
System.out.println(byteBuffer4.getShort(54));
}
}
7天掌握NIO,没错只需7天,第一天----put和get方法
最新推荐文章于 2024-04-21 16:28:44 发布