Is there a Java implementation of ByteBuffer that integrates several retaining ByteBuffers under the hood?

I have one or more byte buffers containing parts of a single message. Now I want to read this post, but I do not want to copy N ByteBuffer to one. My parser expects one ByteBuffer to receive the full message, but my message is split into N ByteBuffers.

Is there a way to combine these N ByteBuffers into one without byte copy? I imagined some intellectual implementation of the abstract ByteBuffer class, which is backed up by these ByteBuffer under the hood and just sets pointers and delegates to the correct ByteBuffer.

If you are wondering why I need this, check the protocol below from BM & F / Bovespa. They break the message into pieces, and they can fail in different packets, in other words, the same sequence of messages can come in several packets, each of which has a piece of message. I cannot write sequentially the same ByteBuffer, because these pieces may be out of order. :(

Did I miss something smarter here? There seems to be no way to write the same ByteBuffer sequentially, given this chunk protocol below :(

enter image description hereenter image description here

+6
source share
2 answers

I think the data structure you are looking for is called (as far as I know) Chain Buffer . Internally, it is a dynamically growing collection (for example, an ArrayList in Java) of byte arrays or buffers, while on the outside the structure acts like a regular Buffer .

This can be very convenient if you are dealing with performance, although the implementation of some operations may not be completely trivial (for example, searching for the position of a given template).

The only public Java implementation I know of is ChainBuffer in Kraken (although it is no longer supported).

0
source

You can bind ByteBuffers to each other by returning ByteBuffer as follows:

 bba.append(bbb.toBytes()).append(bbc.toBytes()) 

The result is one ByteBuffer that can be passed to your analysis method.

It effectively creates a copy, but if you do not assign it, it will be queued for GC.

If you do not know the number of ByteBuffers, use the ByteBuffers list and add them each on the fly, and then add them to the loop. You can sort the list before adding so that the fragments are displayed in the order you want.

-2
source

Source: https://habr.com/ru/post/975462/


All Articles