Package io.netty.handler.stream
Class ChunkedWriteHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
io.netty.handler.stream.ChunkedWriteHandler
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler,ChannelOutboundHandler
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:
Once inserted, you can write aChannelPipelinep = ...; p.addLast("streamer", newChunkedWriteHandler()); p.addLast("handler", new MyHandler());
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:
Channelch = ...; ch.write(newChunkedFile(new File("video.mkv"));
Sending a stream which generates a chunk intermittently
SomeChunkedInput generates a chunk on a certain event or timing.
Such ChunkedInput implementation often returns null on
ChunkedInput.readChunk(ChannelHandlerContext), resulting in the indefinitely suspended
transfer. To resume the transfer when a new chunk is available, you have to
call resumeTransfer().-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ChannelHandlerContextprivate static final InternalLoggerprivate Queue<ChunkedWriteHandler.PendingWrite> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidvoidCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelWritabilityChanged()to forward to the nextChannelInboundHandlerin theChannelPipeline.private static voidcloseInput(ChunkedInput<?> chunks) private voidprivate voidvoidCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline.private static voidhandleEndOfInputFuture(ChannelFuture future, ChunkedInput<?> input, ChunkedWriteHandler.PendingWrite currentWrite) private voidhandleFuture(ChannelFuture future, ChunkedInput<?> input, ChunkedWriteHandler.PendingWrite currentWrite, boolean resume) voidDo nothing by default, sub-classes may override this method.private booleanvoidContinues to fetch the chunks from the input.private voidvoidwrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, readMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRead, channelReadComplete, channelRegistered, channelUnregistered, exceptionCaught, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerRemoved, isSharableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.netty.channel.ChannelHandler
handlerRemoved
-
Field Details
-
logger
-
queue
-
ctx
-
-
Constructor Details
-
ChunkedWriteHandler
public ChunkedWriteHandler() -
ChunkedWriteHandler
Deprecated.
-
-
Method Details
-
allocateQueue
private void allocateQueue() -
queueIsEmpty
private boolean queueIsEmpty() -
handlerAdded
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
Exception
-
resumeTransfer
public void resumeTransfer()Continues to fetch the chunks from the input. -
resumeTransfer0
-
write
Description copied from class:ChannelDuplexHandlerCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto notify once the operation completes- Throws:
Exception- thrown if an error occurs
-
flush
Description copied from class:ChannelDuplexHandlerCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
flushin interfaceChannelOutboundHandler- Overrides:
flushin classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the flush operation is made- Throws:
Exception- thrown if an error occurs
-
channelInactive
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelInactivein interfaceChannelInboundHandler- Overrides:
channelInactivein classChannelInboundHandlerAdapter- Throws:
Exception
-
channelWritabilityChanged
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelWritabilityChanged()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelWritabilityChangedin interfaceChannelInboundHandler- Overrides:
channelWritabilityChangedin classChannelInboundHandlerAdapter- Throws:
Exception
-
discard
-
doFlush
-
handleEndOfInputFuture
private static void handleEndOfInputFuture(ChannelFuture future, ChunkedInput<?> input, ChunkedWriteHandler.PendingWrite currentWrite) -
handleFuture
private void handleFuture(ChannelFuture future, ChunkedInput<?> input, ChunkedWriteHandler.PendingWrite currentWrite, boolean resume) -
closeInput
-
ChunkedWriteHandler()