Skip to content

How to send both commands and files using Netty

I don’t understand what I’m doing wrong. How to send both commands and files. Below are the server and client pipelines.

client pipeline:

 pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));    //in
            pipeline.addLast(new ObjectEncoder());                           //out
            pipeline.addLast(new ChunkedWriteHandler());                     //out
            pipeline.addLast(new FileSenderHandler());                       //in (write in responce)
            pipeline.addLast(new FileInfoSenderHandler());                   //out

server pipeline:

        pipeline.addLast(new ObjectEncoder());                                   //out
        pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null))); //in
        pipeline.addLast(new FileReceiverHandler());                             //in
        pipeline.addLast(new FileInfoReceiverHandler());                         //in (write in responce)

The fact is that before I send the file, I send a command (FileInfo) that the file described in this FileInfo will be sent soon. Then the file itself is sent (byte Buf), Like FileInfo, ByteBuf also passes through to ObjectDecoder. At the same time , Java swears on the first chunk:

2021-10-09 01:26:02 WARN  DefaultChannelPipeline:1152 - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 1048576: 4292411364 - discarded
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.failIfNecessary(
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.exceededFrameLength(
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(
    at io.netty.handler.codec.serialization.ObjectDecoder.decode(
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(
    at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(
    at io.netty.util.concurrent.SingleThreadEventExecutor$
    at io.netty.util.internal.ThreadExecutorMap$
    at java.base/

If you override decode() and add logs, then you can see that the file transfer does not end after that and each piece passes through the ObjectDecoder and is “decoded to null”. There is no such exception on the remaining chunks.

What have I tried:

  1. In the ObjectDecoder constructor, set the number maxObjectSize = MAX.INTEGER, but it is still small.

  2. In the decode() set the condition:

     protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
         Object o = super.decode(ctx, in);
         if(o == null){
             return in;
         return o;

In this case, as a result, 2048 bytes are missing in the file.

3)If I dynamically change the pipeline in the code (delete/add an ObjectDecoder depending on the state), then the files are accepted by the server. However, I am not sure about the correctness of this decision.

Maybe I’m doing something fundamentally wrong? I have read almost all the documentation, and I have been solving this problem for the second week. What to read? Please help me. Thanks!



It seems like you have a custom protocol that consists of metadata about the file and then actual file data. Since both metadata handler (ObjectEncoder/Decoder) and file handlers work on buffers, it seems there is no way to distinguish which message is metadata and which is data. If there is no indication as such on the wire and you know that the first message is always metadata then starting the pipeline with the metadata handler and then swapping it later with the file handlers seem like the best option for you.

User contributions licensed under: CC BY-SA
9 People found this is helpful