我有一个来自 ByteBuffer
的 byte[] bytes
,其中包含一个数据包。我想将数据包放入 String
中。
目前我有以下内容
byte[] bytes = packet.array();
System.out.println("Packet String:" + new String(bytes));
但是我最终得到以下输出
Packet String:E����<Ҧ@��@�.
03-22 04:30:28.187 9296-10152/willem.com.vpn I/System.out﹕ ����J}�j���k�:����������9�������
03-22 04:30:28.197 9296-10152/willem.com.vpn I/System.out﹕ ��&�4��������ddrarpa��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
我试过这样编码
System.out.println("Packet String:" + new String(bytes, Charset.forName("UTF-8")));
但这不是正确的字符集。谁能告诉我是什么?
最佳答案
您需要使用缓冲区的位置和限制来确定要读取的字节数。
// ...populate the buffer...
buffer.flip(); // flip the buffer for reading
byte[] bytes = new byte[buffer.remaining()]; // create a byte array the length of the number of bytes written to the buffer
buffer.get(bytes); // read the bytes that were written
String packet = new String(bytes);
在我看来,您根本不应该真正使用后备 array()
;这是不好的做法。直接字节缓冲区(由 ByteBuffer.allocateDirect()
创建)没有后备数组,当您尝试调用 ByteBuffer.array()
时会抛出异常。因为这个,为了可移植性,你应该尝试坚持使用标准缓冲区 get
和 put
方法。当然,如果你真的想使用数组,你可以使用 ByteBuffer .hasArray()
检查缓冲区是否有后备数组。
关于java - 在 Java 中将 ByteBuffer 转换为 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29190684/