java nio 和 ByteBuffer 问题
我遇到了一个问题。我使用 nio 套接字来接收消息。收到完整的消息后,我将保存收到的消息的 dataBuffer 发送给另一个用户。但下面有一个例外。问题出在哪里?我尝试调用 dataBuffer.duplicate() 并将其写出来。但在接收端,读操作会抛出这样的异常。我必须分配一个新的 ByteBuffer 并制作一个新的消息副本并将其写出来。在这种情况下,没有错误。但我不想要复制步骤。还有其他方法可以解决吗?
抛出异常
java.nio.BufferOverflowException
at java.nio.HeapByteBuffer.put(Unknown Source)
at java.nio.ByteBuffer.put(Unknown Source)
at serviceHandlerPackage.ServiceHandler.readComplete(ServiceHandler.java:218)
代码
readEventHAndler(SocketChannel socket) {
readCompleteData(socket);
}
readCompleteData(Socket) {
ByteBuffer dataBuffer; //hold complete message
if(!dataComplete) return;
else process(dataBuffer);
}
process(dataBuffer) {
...
processHandler();
sendNext(dataBuffer);
}
sendNext(dataBuffer) {
write(dataBuffer);
}
I met a problem. I use nio socket to receive message. Upon a complete message is received, I send the dataBuffer which holds the received message to another user. But there is exception below. Where is the problem? I try to call dataBuffer.duplicate() and write it out. But at the receiver side, the read operation throws such exception. I have to assign a new ByteBuffer and make a new copy of message and write it out. In this case, there is no error. But I do not want the copy step. Is there any other way to solve it?
Exception thrown
java.nio.BufferOverflowException
at java.nio.HeapByteBuffer.put(Unknown Source)
at java.nio.ByteBuffer.put(Unknown Source)
at serviceHandlerPackage.ServiceHandler.readComplete(ServiceHandler.java:218)
Code
readEventHAndler(SocketChannel socket) {
readCompleteData(socket);
}
readCompleteData(Socket) {
ByteBuffer dataBuffer; //hold complete message
if(!dataComplete) return;
else process(dataBuffer);
}
process(dataBuffer) {
...
processHandler();
sendNext(dataBuffer);
}
sendNext(dataBuffer) {
write(dataBuffer);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当将数据读入缓冲区时,如果要将缓冲区写出另一个通道,则需要在写入之前调用:
buffer.flip()
。如果您在读取时在同一线程上写入,则无需复制缓冲区。另外 - BufferOverflowException 意味着您将比其容量更多的数据放入缓冲区。这听起来像是缓冲区没有被调用来将位置重置为零。
上面编写的代码不足以准确诊断问题所在。
Whenever you read data into a buffer, if you want to write the buffer out another channel, you need to call:
buffer.flip()
before writing. You don't need to duplicate the buffer if you are writing on the same thread as you are reading.Also - BufferOverflowException means you are putting more data into the buffer than its capacity. This sounds like a buffer is not getting
buffer.clear()
called to reset the position to zero.There isn't enough code written above to diagnose exactly what the problem is.
您的程序在放置数据时抛出异常,所以我想说位置/限制有问题。
看起来您正在尝试将数据放入读取缓冲区或尝试放入比其大小更多的数据。 Buffer 不会自行增长(ByteArrayOutputStream 对此更好)。
阅读 java 文档< /a>.这将重置缓冲区的位置、限制或大小。
Your program throws an exception while putting data so I would say that something is wrong with position/limit.
It looks like you are trying to put data into read buffer or trying to put more data than it's size. Buffer will not grow itself (ByteArrayOutputStream is better for this).
Read about clearing, rewinding and flipping in java documentation. This will reset position, limit or size of buffer.