Efficient data transferring within single ByteBuf.

35 views
Skip to first unread message

zubra...@gmail.com

unread,
Jun 22, 2016, 3:43:09 PM6/22/16
to Netty discussions
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!

Norman Maurer

unread,
Jun 23, 2016, 5:51:04 AM6/23/16
to ne...@googlegroups.com
I think I may not correctly understand what you try to do.

So you are saying you want to basically move some bytes (which are somewhere in the buffer) to the beginning of the same buffer ?

If so why you can’t just slice the part out ?

--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/7ac75f53-d410-4096-a07f-4c964d56cd60%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rafael Zubairov

unread,
Jun 24, 2016, 4:42:14 PM6/24/16
to Netty discussions
Hello, Norman

My bad, later when reading javadoc realized there is discardReadBytes() method which does all i need.
Reply all
Reply to author
Forward
0 new messages