在 Java NIO 中使用 ByteBuffer 读写 Int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13375742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read and Write Int using ByteBuffer in Java NIO
提问by Aditya
I want to transmit an int array over a network using Datagram Channel in Java NIO API. However, the functions read/write can only take a ByteBuffer
as an input. So I need to store the int data to a ByteBuffer
and then read it back at the receiver. I am facing the java.nio.BufferUnderflowException
. Here is what I do at the Sender Side:
我想使用 Java NIO API 中的数据报通道通过网络传输一个 int 数组。但是,读/写函数只能将 aByteBuffer
作为输入。所以我需要将 int 数据存储到 aByteBuffer
然后在接收器处读回。我面对java.nio.BufferUnderflowException
. 这是我在发送方所做的:
for(i = 0; i < send_data.length; i++)
{
//Write the data onto Buffer
ByteBuffer buffer1 = ByteBuffer.allocate(send_data[i].length*4);
for(j = 0; j < send_data[i].length; j++)
buffer1.putInt(send_data[i][j]);
buffer1.flip();
//Transmit the data
while(buffer1.hasRemaining())
channel.write(buffer1);
}
Here, send_data
is a 2D array where each row is treated as a separate data packet.
这里send_data
是一个二维数组,其中每一行都被视为一个单独的数据包。
At the Receiver Side
在接收端
for(i = 0; i < k; i++)
{
ByteBuffer buffer = ByteBuffer.allocate((recvpkt[0].length)*4);
channel.receive(buffer);
j = 0;
while(buffer.hasRemaining())
{
recvpkt[i][j] = buffer.getInt();
j = j+1;
}
}
Similarly, recvpkt
is a 2D array and each row of it will receive one packet from the network.
同样,recvpkt
是一个二维数组,它的每一行都会从网络接收一个数据包。
I read that getInt()
reads 4 bytes from the ByteBuffer
and moves the current position by 4 bytes. Is it that I am using the buffer.flip()
in a wrong manner. I am new to using NIO
and am having difficulty in how to go about debugging such issues.
我读到getInt()
从中读取 4 个字节ByteBuffer
并将当前位置移动 4 个字节。是不是我buffer.flip()
以错误的方式使用了。我刚开始使用,NIO
并且在如何调试此类问题方面遇到困难。
UPDATE:
更新:
The error no longer occurs. But now all that the Receiver gets is all zeros. Whereas I am transmitting a binary sequence in each packet. When I read back from the buffer at the Sender side itself, all the entries turn up correctly but the buffer received at Receiver side has all zeros. No idea why this is happening.
错误不再发生。但是现在接收器得到的全部都是零。而我在每个数据包中传输一个二进制序列。当我从发送方本身的缓冲区读回时,所有条目都正确显示,但接收方收到的缓冲区全为零。不知道为什么会这样。
UPDATE 2:
更新 2:
Finally, after hours of struggling, the problem is fixed. When you receive the buffer , you need to rewind it.
最后,经过数小时的努力,问题得到解决。当您收到缓冲区时,您需要将其倒带。
channel.receive(buffer);
buffer.rewind();
If you now use the getInt method on the buffer, you will be able to read the buffer successfully.
如果您现在在缓冲区上使用 getInt 方法,您将能够成功读取缓冲区。
回答by Peter Lawrey
You have th flip() the buffer each time you swap from write to read and from read to writing. Add flip() to the second example after receive()
每次从写到读和从读到写交换时,你都有一个 flip() 缓冲区。将 flip() 添加到第二个示例之后receive()
Once you have this working you should look at how you can re-use your ByteBuffers and EJP suggests.
一旦你完成了这项工作,你应该看看如何重新使用你的 ByteBuffers 和 EJP 建议。