1

Let's say i have a list/arrayList or an array of ByteBuffers (List<ByteBuffer> or ByteBuffer[])

It is possible from this to directly get the bytes from the above array without iterating through all the items or computing their total size? I am looking for something like this:

ByteBuffer[] bufferList = new ByteBuffer[7]; //this can be any kind of list of ByteBuffers
//add items to the array
ByteBuffer buffer = (ByteBuffer) bufferList; //what i want to achieve  

Of course the last line is not correct.

The thing is that i already have in the array all the bytes in order, but I want them not to be in a list anymore, but in a single ByteBuffer. So is there something that makes possible to create a ByteBuffer from a list/array of ByteBuffers?

Thank you!

4
  • You have individual ByteBuffer elements in an array or list that each have X bytes and you want to add all those bytes to a single ByteBuffer? Commented Aug 27, 2013 at 15:14
  • You're likely going to just have to write a method for it. Commented Aug 27, 2013 at 15:15
  • 2
    Make a new ByteBuffer, iterate through your array of buffers, get each bytebuffer as an array using .array(), and use put(byte[]) into your new ByteBuffer Commented Aug 27, 2013 at 15:16
  • Yes @SotiriosDelimanolis. I have a list of ByteBuffers that have different byte size and i want to create a single ByteBuffer from them. Commented Aug 27, 2013 at 15:18

2 Answers 2

4
public ByteBuffer convertToOne(ByteBuffer[] src)
{
    int size=0;
    for(int i = 0 ; i < src.length ; i++) {
        size+=src[i].array().length;
    }

    ByteBuffer newBuffer = ByteBuffer.allocate(size);

    int sizeAt = 0;
    for(int i = 0 ; i < src.length ; i++) {
        newBuffer.put(src[i].array(), sizeAt);
        sizeAt += src[i].array().length;
    }
    return newBuffer;
}
Sign up to request clarification or add additional context in comments.

11 Comments

the problem with this solution would be that i have to make another iteration to compute the elements total bytes size. (The ByteBuffer items don't have constant size) So the problem would be how i initialize the final ByteBuffer (the newBuffer in this case).
How do you initialize the original buffer? I'm assuming you have a concrete class somehwere.
then how do i create 'ByteBuffer dest' ? Don't i have to allocate the size before putting things into it?
There's no way around it. If java had a method to do this, it would be implemented by doing that anyway. Native java methods aren't magic.
There is only one way to create a ByteBuffer, by specifying size. Also, there are no ByteBuffer methods which return a ByteBuffer of a different size. It therefore then follows that you need to compute size first.
|
0

I believe you're going to have to create a new ByteBuffer and copy each of the buffers in bufferList into the new one. You should put this functionality in a method so as to not repeat the code each time you use it.

3 Comments

Yes. I know i can do this. but this will require to iterate through all the ByteBuffers to compute their total size, then create the big ByteBuffer, and the interate again to add them. I was looking for something more optimum, if it is possible.
Iterating over the collection is going to take a fraction of the time that creating the big ByteBuffer and copying the other ByteBuffers is going to take. Any built-in function is most likely going to do this anyway.
the issue is.. that i am trying to create a serializer, so in the end I need a single ByteBuffer not a list of ByteBuffers. That is why i want to transform a list of BB to a single BB

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.