mcfly soft mcfly soft - 1 year ago 300
Java Question

Concat two ByteBuffers in Java

How can I concat two ByteBuffers to one ByteBuffer?

The following doesn't work:

ByteBuffer bb = ByteBuffer.allocate(100);
ByteBuffer bb2 = ByteBuffer.allocate(200);

The length of
is still

Answer Source

Something like

bb = ByteBuffer.allocate(300).put(bb).put(bb2);

should do the job: Create a buffer that is large enough to hold the contents of both buffers, and then use the relative put-methods to fill it with the first and the second buffer. (The put method returns the instance that the method was called on, by the way)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download