ReplayingDecoder receives large amount of bytes when only 4 bytes expected

318 views
Skip to first unread message

Chaz Ashley

unread,
Jun 1, 2016, 12:41:15 PM6/1/16
to Netty discussions
Hey there, I use ReplayingDecoder from the official Netty tutorial, here is the code


import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

public class MessageDecoder extends ReplayingDecoder<DecoderState> {

    private int length;

    public MessageDecoder()
    {
        super(DecoderState.READ_LENGTH);
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception{
        System.out.println(buf.readableBytes());
        switch(state()){
            case READ_LENGTH:
                length=buf.readInt();
                System.out.println("length is: "+length);
                checkpoint(DecoderState.READ_CONTENT);
            case READ_CONTENT:
                ByteBuf frame = buf.readBytes(length);
                checkpoint(DecoderState.READ_LENGTH);
                out.add(frame);
                break;
            default:
                throw new Error("Shouldn't reach here");
        }
    }
}
And when the remote peer sends only 4 bytes, in Eclipse console i see that System.out.println(buf.readableBytes()); shows me that 2147483647 bytes are available. Without replaying decoder all is good.
Why it so?

Norman Maurer

unread,
Jun 1, 2016, 1:11:17 PM6/1/16
to ne...@googlegroups.com
Its a “limitation” of the ReplayingDecoder. If you need to depend on readableBytes() you will need to use ByteToMessageDecoder.

--
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/926ac24f-4100-491e-bdcd-a9784ce31606%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chaz Ashley

unread,
Jun 1, 2016, 1:18:13 PM6/1/16
to Netty discussions
What does it mean "depend on readableBytes" ? actually I don't care about readableBytes, i used it just to check why ReplayingDecoder does not work, because even without calling readableBytes() it does not works right and says that the remote peer sent 419430400 integer (4 bytes), but the remote peer sent another integer

среда, 1 июня 2016 г., 20:11:17 UTC+3 пользователь Norman Maurer написал:

Norman Maurer

unread,
Jun 1, 2016, 1:19:22 PM6/1/16
to ne...@googlegroups.com
What is the integer it sends ? Maybe it uses different endianness ?

Chaz Ashley

unread,
Jun 1, 2016, 1:21:57 PM6/1/16
to Netty discussions
I have changed the value of integer to 1, and now decoder says that the client sent 16777216

среда, 1 июня 2016 г., 19:41:15 UTC+3 пользователь Chaz Ashley написал:

Norman Maurer

unread,
Jun 1, 2016, 1:27:01 PM6/1/16
to ne...@googlegroups.com
Yep… so its like I suspected… Your other code is using LITTLE_ENDIAN and Netty app is using BIG_ENDIAN (which is the default in java).

public class Test {
public static void main(String[] args) {
System.err.println(ByteBufUtil.swapInt(1));
}
}

This will print 16777216.

So you can either change your remote app to use BIG_ENDIAN or use `buf.readIntLE()` in Netty 4.1 to read it as LITTLE_ENDIAN.



-- 
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.

Chaz Ashley

unread,
Jun 1, 2016, 1:30:58 PM6/1/16
to Netty discussions
yeah xD Thanks! Now i just use this code 
msg1.order(ByteOrder.LITTLE_ENDIAN).readInt()
and it gives right value


среда, 1 июня 2016 г., 19:41:15 UTC+3 пользователь Chaz Ashley написал:
Hey there, I use ReplayingDecoder from the official Netty tutorial, here is the code
Reply all
Reply to author
Forward
0 new messages