camel netty4 message 1024 truncated..how can i solve it?

66 views
Skip to first unread message

James Kim

unread,
Jul 4, 2017, 5:39:11 AM7/4/17
to Netty discussions
i made a camel netty4 server program.and client sent 1119 byte message but myu decoder truncated by 1024/95

here is my code.

@ChannelHandler.Sharable
    public static class BytesDecoder extends MessageToMessageDecoder<ByteBuf> {
        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
            // it may be empty, then return null
         System.out.println("BytesDecoder readableBytes:"+msg.readableBytes());
            if (msg.isReadable()) {
                // ByteBuf may not expose array method for accessing the under layer bytes
                byte[] bytes = new byte[msg.readableBytes()];
                int readerIndex = msg.readerIndex();
                msg.getBytes(readerIndex, bytes);
                out.add(bytes);
            }
        }
    }
 
result
BytesDecoder readableBytes:1024
BytesDecoder readableBytes:95

i want to get a full message byte 1119 byte.


Norman Maurer

unread,
Jul 4, 2017, 5:40:54 AM7/4/17
to ne...@googlegroups.com
There is no guarantee that you will receive all of it with one read operation. 

You will need to account for it in your decoder and that is exactly what ByteToMessageDecoder is for. Please check the javadocs of it for more details.

--
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/7060c6b2-f193-4da9-a788-7c31269c8d94%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Kim

unread,
Jul 5, 2017, 12:55:35 AM7/5/17
to Netty discussions

public static class NettyDecoder2 extends ByteToMessageDecoder{

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

if((in.readableBytes() < 1119) ) {
 return;
}

String msg = in.toString(CharsetUtil.UTF_8);
in.readerIndex(in.readerIndex() + in.readableBytes());


out.add(msg);
}

}

i create this code. but is there any way to know  in.readableBytes() ?
it is very dynamic data. so i could not know the size.. give me a tip pz.

2017년 7월 4일 화요일 오후 6시 40분 54초 UTC+9, Norman Maurer 님의 말:
Reply all
Reply to author
Forward
0 new messages