2

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.

3
  • To me it translates to concatenation of two byte arrays which can be handled easily using System.arraycopy. If you mean something else, please provide some examples of what you have and what you want to achieve. Commented Jun 1, 2011 at 14:28
  • 1
    @d-live This can be done without copying, see my answer. Commented Jun 1, 2011 at 14:30
  • @Waldheinz, In fact, it can be done with just a ByteBuffer, see my answer. ;) Commented Jun 1, 2011 at 14:55

2 Answers 2

2

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/

Sign up to request clarification or add additional context in comments.

Comments

0

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);
}

7 Comments

Can I apply this method to SocketChannel?
Honestly I don't understand your answer. Where is prependHeader coming from?
@Susan, you can apply this anywhere you can use a ByteBuffer. This works because write() is not destructive and you can use the same buffer with the same data again and again.
@Waldheinz, Just like appendContentTo, you would have to write it. Follow the comment as to what it would need to do. ;)
@Waldheinz, I have added an example for this method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.