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 aChannelPipeline
p = ...; 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:
Channel
ch = ...; 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 ChannelHandlerContext
private static final InternalLogger
private Queue
<ChunkedWriteHandler.PendingWrite> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
void
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.fireChannelWritabilityChanged()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.private static void
closeInput
(ChunkedInput<?> chunks) private void
private void
void
CallsChannelHandlerContext.flush()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.private static void
handleEndOfInputFuture
(ChannelFuture future, ChunkedInput<?> input, ChunkedWriteHandler.PendingWrite currentWrite) private void
handleFuture
(ChannelFuture future, ChunkedInput<?> input, ChunkedWriteHandler.PendingWrite currentWrite, boolean resume) void
Do nothing by default, sub-classes may override this method.private boolean
void
Continues to fetch the chunks from the input.private void
void
write
(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, read
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRead, channelReadComplete, channelRegistered, channelUnregistered, exceptionCaught, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerRemoved, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
- Throws:
Exception
-
resumeTransfer
public void resumeTransfer()Continues to fetch the chunks from the input. -
resumeTransfer0
-
write
Description copied from class:ChannelDuplexHandler
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
flush
Description copied from class:ChannelDuplexHandler
CallsChannelHandlerContext.flush()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
flush
in interfaceChannelOutboundHandler
- Overrides:
flush
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the flush operation is made- Throws:
Exception
- thrown if an error occurs
-
channelInactive
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelInactive
in interfaceChannelInboundHandler
- Overrides:
channelInactive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelWritabilityChanged
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelWritabilityChanged()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelWritabilityChanged
in interfaceChannelInboundHandler
- Overrides:
channelWritabilityChanged
in 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()