ByteBuffer.Array()方法表现不一致
public class BufferDemo {
public static void main(String[] args) {
ByteBuffer readBuffer = ByteBuffer.allocate(2048);
readBuffer.put("test".getBytes());
System.out.println(new String(readBuffer.array()));
}
}
一台机器运行异常:
另一台机器运行正常:
当然读取buffer内容最稳妥的方式还是基于bytes:
public class BufferDemo {
public static void main(String[] args) {
ByteBuffer readBuffer = ByteBuffer.allocate(2048);
readBuffer.put("test".getBytes());
readBuffer.flip();
byte[] bytes = new byte[readBuffer.limit()];
readBuffer.get(bytes);
System.out.println(new String(bytes));
}
}