java中是否有类似于C中指针函数的ByteBuffer函数

发布于 2024-11-12 06:02:24 字数 206 浏览 9 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

雪花飘飘的天空 2024-11-19 06:02:24

在 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/

抚你发端 2024-11-19 06:02:24

我使用单个 ByteBuffer 发送到多个接收者。

ByteBuffer bb = ByteBuffer.allocateDirect(LARGE_BUFFER);
bb.clear();
bb.position(START_OF_CONTENT /* 1024 */);
appendContentTo(bb);
int endOfContent = bb.position();

bb.limit(endOfContent);
for(Connection conn: connections) {
    bb.position(START_OF_CONTENT);
    /* prepend header BEFORE the position and move the position back */
    conn.prependHeader(bb); 
    conn.write(bb);
}

这样,您可以为每个连接使用相同的 ByteBuffer。内容只有一份副本。

conn.prependHeader() 的示例

public void prependHeader(ByteBuffer bb) {
    // bb starts at the start of the content.
    int pos = bb.position();
    // it would be better if a byte[] wasn't required. This is just an example
    byte[] header = getHeaderAsBytes();
    bb.position(bb.position()-header.length);
    bb.put(header);
    // bb starts at the start of the header.
    bb.position(bb.position()-header.length);
}

I use a single ByteBuffer to send to multiple receivers.

ByteBuffer bb = ByteBuffer.allocateDirect(LARGE_BUFFER);
bb.clear();
bb.position(START_OF_CONTENT /* 1024 */);
appendContentTo(bb);
int endOfContent = bb.position();

bb.limit(endOfContent);
for(Connection conn: connections) {
    bb.position(START_OF_CONTENT);
    /* prepend header BEFORE the position and move the position back */
    conn.prependHeader(bb); 
    conn.write(bb);
}

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

public void prependHeader(ByteBuffer bb) {
    // bb starts at the start of the content.
    int pos = bb.position();
    // it would be better if a byte[] wasn't required. This is just an example
    byte[] header = getHeaderAsBytes();
    bb.position(bb.position()-header.length);
    bb.put(header);
    // bb starts at the start of the header.
    bb.position(bb.position()-header.length);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文