3
votes

I'm currently working with Netty 3.5.10.Final to implement a client for a server that uses a binary protocol with a mixed byte order. That is to say, there are many situations in the protocol where I will have to read/write one set of bytes in big-endian order, then another set in little-endian order, and then again switch back to big-endian.

I've discovered that there is not (as far as I know) an easy way to deal with this situation, especially with ChannelBuffers.wrappedBuffer(ChannelBuffer buf) throwing an IllegalArgumentException when the endianness differs. I am learning both about Netty and this protocol as I go, so I was not aware I would run into this issue when I began.

How have others have dealt with this issue, especially within the confines of the Netty 3.x framework? I'd love to hear about all solutions though.

1

1 Answers

4
votes

If the protocol you are implementing has the fields with mixed byte orders, then you could choose one byte order as the default (e.g. big endian) and swap the byte order for a specific field:

ChannelBuffer buf = ...;
int littleEndianField = ChannelBuffers.swapInt(buf.readInt());

Depending on the width of the field, you can use one of these: swapShort(), swapMedium(), swapInt(), and swapLong().