NIO的陷阱

                   // Create a byte array
	              byte[] bytes = { 0x00, 0x01, 0x02, 0x03, 0x04};
	          // Wrap a byte array into a buffer
	      
	              ByteBuffer buf = ByteBuffer.wrap(bytes);
 	              // Get the buffer's capacity
	              int capacity = buf.capacity();
	              // Get the buffer's limit
	              int limit = buf.limit();
	              // Get the buffer's position
	              int position = buf.position();
	             
                  System.out.println("Buffer capacity: " + capacity); 
	              System.out.println("Buffer limit: " + limit);
	      
	              System.out.println("Buffer position: " + position);
	              
	              
	              byte[] dst = new byte[1];
	              buf.position(2);
	              buf.get(dst, 0, dst.length);
		      System.out.println("Buffer data: " + dst); 


get 前必须 postion 定好位置。nio 真坑

猜你喜欢

转载自xixiyanqi.iteye.com/blog/2222155
NIO