nio 建立緩衝區的方法選擇 java
allocate 仍是 allocateDirect post
http://stevex.blog.51cto.com/4300375/1582209 this
NIO讀寫文件優劣 spa
http://blog.csdn.net/gzu_imis/article/details/21109753 .net
NIO讀取 code
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(10); bb.put(ByteBuffer.wrap("abc".getBytes())); byte[] bytes = new byte[1024]; int postion = bb.position(); bb.flip(); //讀取bb的postion 到 limit 之間的數據 bb.get(bytes, 0, postion); // 輸出abc System.out.println(new String(bytes)); }
public ByteBuffer get(byte[] dst, int offset, int length) { checkBounds(offset, length, dst.length); if (length > remaining()) throw new BufferUnderflowException(); System.arraycopy(hb, ix(position()), dst, offset, length); position(position() + length); return this; }能夠看到是從緩衝區的position 位置開始讀取數據的,因此須要先作一步bb.flip 才能夠讀取數據