ChannelBuffer ByteOrder

"Trustin Lee (이희승)" trustin at gmail.com
Tue Jun 1 04:22:39 EDT 2010


Hi Matt,

You could do the following:

    ChannelBuffer bebuf = ...; // big endian
    ChannelBuffer lebuf = ChannelBuffers.wrappedBuffer(
            bebuf.toByteBuffer().order(ByteOrder.LITTLE_ENDIAN));

Please note that toByteBuffer() does not involve memory copy in most cases.

If bebuf.hasArray() returns true, you can make it even more efficient:

    ChannelBuffer bebuf = ...; // big endian
    ChannelBuffer lebuf = ChannelBuffers.wrappedBuffer(
            ByteOrder.LITTLE_ENDIAN,
            bebuf.array(), bebuf.arrayOffset(), bebuf.capacity());

I'd better add some shortcut methods for this in the next feature
release.  Thanks for inspiration.

HTH,
Trustin

mwhited wrote:
> Is there a way to switch the byteOrder on an existing ChannelBuffer.
> 
> The issue I am running into is that I have a FrameDecoder to decode incoming
> packets that have the following format
> 
> Byte Order Identifier
> Length
> Data
> 
> The server can accept connections from LITTLE_ENDIAN or BIG_ENDIAN clients,
> and I don't know what order a Channel is using until I receive the first
> packet on the channel.
> 
> Currently I have an implementation that works, but it involves copying the
> existing buffer into a temporary ChannelBuffer with the correct byte order.
> 
> I realize I can set the BufferFactory for the Channel to the correct byte
> order so subsequent ChannelBuffers will be the correct order, but that still
> leaves me copying buffers around for the first packet.
> 
> Thanks,
> Matt

-- 
what we call human nature in actuality is human habit
http://gleamynode.net/



More information about the netty-users mailing list