Class ChunkedWriteHandler

  • All Implemented Interfaces:
    io.netty5.channel.ChannelHandler

    public class ChunkedWriteHandler
    extends Object
    implements io.netty5.channel.ChannelHandler
    A ChannelHandler that adds support for writing a large data stream asynchronously neither spending a lot of memory nor getting OutOfMemoryError. Large data streaming such as file transfer requires complicated state management in a ChannelHandler implementation. ChunkedWriteHandler manages such complicated states so that you can send a large data stream without difficulties.

    To use ChunkedWriteHandler in your application, you have to insert a new ChunkedWriteHandler instance:

     ChannelPipeline p = ...;
     p.addLast("streamer", new ChunkedWriteHandler());
     p.addLast("handler", new MyHandler());
     
    Once inserted, you can write a ChunkedInput so that the ChunkedWriteHandler can pick it up and fetch the content of the stream chunk by chunk and write the fetched chunk downstream:
     Channel ch = ...;
     ch.write(new ChunkedFile(new File("video.mkv"));
     

    Sending a stream which generates a chunk intermittently

    Some ChunkedInput generates a chunk on a certain event or timing. Such ChunkedInput implementation often returns null on ChunkedInput.readChunk(BufferAllocator), resulting in the indefinitely suspended transfer. To resume the transfer when a new chunk is available, you have to call resumeTransfer().
    • Constructor Detail

      • ChunkedWriteHandler

        public ChunkedWriteHandler()
    • Method Detail

      • handlerAdded

        public void handlerAdded​(io.netty5.channel.ChannelHandlerContext ctx)
                          throws Exception
        Specified by:
        handlerAdded in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • resumeTransfer

        public void resumeTransfer()
        Continues to fetch the chunks from the input.
      • write

        public io.netty5.util.concurrent.Future<Void> write​(io.netty5.channel.ChannelHandlerContext ctx,
                                                            Object msg)
        Specified by:
        write in interface io.netty5.channel.ChannelHandler
      • flush

        public void flush​(io.netty5.channel.ChannelHandlerContext ctx)
        Specified by:
        flush in interface io.netty5.channel.ChannelHandler
      • channelInactive

        public void channelInactive​(io.netty5.channel.ChannelHandlerContext ctx)
                             throws Exception
        Specified by:
        channelInactive in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • channelWritabilityChanged

        public void channelWritabilityChanged​(io.netty5.channel.ChannelHandlerContext ctx)
                                       throws Exception
        Specified by:
        channelWritabilityChanged in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception