dynamically expanding byte container?

Hello everyone,
Imagine this scenario:

You have a message which you receive as blocks of bytes. The total size of the message is unknown from the start but is indicated to you in the last block (imagine with EOF).
I cannot use a byte array of fixed size because the max size is unknown from the start. I need something that can EXPAND DYNAMICALLY.
There is something called ByteBuffer that could probably be suited for this. Any other ideas/recommendations?

Also...imagine these messages have an ID attached to them, and that many are sent simultaneously, that is: they come intermigled (mixed). What would you do to keep them separated?

You have a message which you receive as blocks of bytes. The total size of the message is unknown from the start but is indicated to you in the last block (imagine with EOF).
I cannot use a byte array of fixed size because the max size is unknown from the start. I need something that can EXPAND DYNAMICALLY.
There is something called ByteBuffer that could probably be suited for this. Any other ideas/recommendations?

Also...imagine these messages have an ID attached to them, and that many are sent simultaneously, that is: they come intermigled (mixed). What would you do to keep them separated?

Thanks for any help you can provide!
Alex

This is off the top of my head, and of course subject to whatever specific requirements you have and what you're actually doing with all this stuff.

Assuming a single message is coming through a single point (ie it isn't split amongst threads), you have an id so you can use a HashMap, or similar, with the id as key, and something to hold the bytes as a value. Add each block to this "something". This something needs to be able to point out when it is finished (ie it has encountered the EOF), so that whatever needs to be done to the bytes can be done, and so it can be removed from the Map, otherwise it'll simply fill up.

As I say above, though, this is off the top of my head, so may be completely rubbish.