Documentation suggestion / question

Frederic Bregier fredbregier at free.fr
Wed Aug 24 16:17:05 EDT 2011


Hi,

The documentation for me is clear. 
Indeed, all channelbuffer implementation do not depend necesseraly on one
unique array. And the main reason is to avoid memory copy for nothing (if
you prefered, zero copy concept). For instance : CompositeChannelBuffer,
DynamicChannelBuffer, SlicedChannelBuffer... where the data could be
splitted in several arrays internally.
The idea is to prevent multiple copies in memory while a simple "linked
list" would be enough to quickly store the subsequent bytes coming from the
network.

Generally, in a network profile, your codec knows how many bytes it has to
read for each piece of object. Therefore you will create your own copy of
data once needed through one of the multiple methods available in
ChannelBuffer.

If you want to have in all case one array of bytes (if it is really what you
want to do, and not other types as String or anything else), then a safe way
would be :

cb.getBytes(0, myarray); where myarray is a new array of size
readableBytes()

Or if you want to have a chance to not make any copy but at the risk to
handle yourself the start index (readerIndex()) and the size of readable
bytes (readableBytes()) by :

byte [] myarray = null;
int start = 0;
int length = cb.readableBytes();
if (cb.hasArray()) {
   start = cb.readerIndex();
   myarray = cb.array(); // WARNING 0 might be different than readerIndex()
} else {
   myarray = new byte[length];
   cb.getBytes(0, myarray);
}
Then you have to use myarray[start+index] where index is 0 <= index <
length.

Of course you can make your own helper function for that...

Hoping it helps you...

Frederic

-----
Hardware/Software Architect
--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Documentation-suggestion-question-tp6718812p6721853.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list