java中是否有类似于C中指针函数的ByteBuffer函数
我正在使用 ByteBuffer 通过 java nio 传输数据。同一条消息可以发送给多个接收者。消息格式为“消息头+消息内容”。一种直接的方法是为每个接收器分配一个新的字节缓冲区。这效率不高。
我的问题是是否有类似的java函数用于ByteBuffer到C/C++中的指针函数。因此,我可以使用一个缓冲区来保存消息内容并与不同的标头连接。这样,就是效率。
谢谢。
I am using ByteBuffer to transfer data with java nio. A same message can be sent to multiple receivers. The message format is "message header + message content". A staright way is to allocate a new byte buffer for each receiver. This is not effiecient.
My question is whether there is similar java function for ByteBuffer to pointer funciton in C/C++. So I can use one buffer to hold message content and concate with different headers. In this way, it is efficiency.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Java 中,您可以使用 GatheringByteChannel(您很可能正在处理它)。它允许拥有一个包含标头的静态缓冲区,以及为每个客户端保存不同内容的单独缓冲区。对于一些入门材料,您可能需要查看此博客文章:
http://javaol.wordpress.com/2011/05/06/java-nio-scatter-gather/
In Java your can use a
GatheringByteChannel
(which you most probably are dealing with). It allows to have one static buffer containing the header and an individual buffer for each client holding the varying contents. For some material to get started you might want to check out this blog post:http://javaol.wordpress.com/2011/05/06/java-nio-scatter-gather/
我使用单个 ByteBuffer 发送到多个接收者。
这样,您可以为每个连接使用相同的 ByteBuffer。内容只有一份副本。
conn.prependHeader() 的示例
I use a single ByteBuffer to send to multiple receivers.
This way, you can use the same ByteBuffer for every connection. There is only ever one copy of the content.
An example of what conn.prependHeader() might look like