java nio 和 ByteBuffer 问题

发布于 2024-11-11 17:55:39 字数 889 浏览 0 评论 0原文

我遇到了一个问题。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖心男生 2024-11-18 17:55:39

每当将数据读入缓冲区时,如果要将缓冲区写出另一个通道,则需要在写入之前调用: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.

夜空下最亮的亮点 2024-11-18 17:55:39

您的程序在放置数据时抛出异常,所以我想说位置/限制有问题。

看起来您正在尝试将数据放入读取缓冲区或尝试放入比其大小更多的数据。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文