Hello!
In our application we are heavily using Netty (v4) ByteBuf objects for caching binary data. We found it to be very useful for data parsing, and we are trying to keep number of created ByteBufs to be limited.
Sometime during data parsing we are facing a task when portion of readable data needs to be moved to the beginning of ByteBuf.
At the moment we are using temporary ByteBuf, here is algo we use
//prepare buffer per connection
mainBuf = createBuffer;
tempBuf = createBuffer;
processData(ByteBuf newData) {
newData.readBytes(newData, newData.readableBytes());
//... perform operations over the mainBuf
mainBuf.readBytes(tempBuf, mainBuf.readableBytes()); // copy data into tempBuf into the beginning
tmp = mainBuf;
mainBuf = tempBuf;
tempBuf = tmp;
//'discard all data'
tempBuf.resetReaderIndex();
tempBuf.resetWriterIndex();
}
Since our applications keeps huge number of buffers, we are looking for the way to remove requirement to have tempBuf ByteBuf.
What is the most optimal way to copy data from the middle to the beginning, and update readed/writer index accordingly?
Thank you!