public class ChunkedWriteHandler extends ChannelDuplexHandler
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"));
ChunkedInput
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()
.Modifier and Type | Class and Description |
---|---|
private static class |
ChunkedWriteHandler.PendingWrite |
ChannelHandler.Sharable
Modifier and Type | Field and Description |
---|---|
private ChannelHandlerContext |
ctx |
private ChunkedWriteHandler.PendingWrite |
currentWrite |
private static InternalLogger |
logger |
private java.util.Queue<ChunkedWriteHandler.PendingWrite> |
queue |
Constructor and Description |
---|
ChunkedWriteHandler() |
ChunkedWriteHandler(int maxPendingWrites)
Deprecated.
|
bind, close, connect, deregister, disconnect, read
channelActive, channelRead, channelReadComplete, channelRegistered, channelUnregistered, exceptionCaught, userEventTriggered
handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
exceptionCaught, handlerRemoved
private static final InternalLogger logger
private final java.util.Queue<ChunkedWriteHandler.PendingWrite> queue
private volatile ChannelHandlerContext ctx
private ChunkedWriteHandler.PendingWrite currentWrite
public ChunkedWriteHandler()
@Deprecated public ChunkedWriteHandler(int maxPendingWrites)
ChunkedWriteHandler()
public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapter
handlerAdded
in interface ChannelHandler
handlerAdded
in class ChannelHandlerAdapter
java.lang.Exception
public void resumeTransfer()
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandler
ChannelHandlerContext.write(Object, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
in interface ChannelOutboundHandler
write
in class ChannelDuplexHandler
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesjava.lang.Exception
- thrown if an error accourpublic void flush(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelDuplexHandler
ChannelHandlerContext.flush()
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.flush
in interface ChannelOutboundHandler
flush
in class ChannelDuplexHandler
ctx
- the ChannelHandlerContext
for which the flush operation is madejava.lang.Exception
- thrown if an error accourpublic void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelInactive()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelInactive
in interface ChannelInboundHandler
channelInactive
in class ChannelInboundHandlerAdapter
java.lang.Exception
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelWritabilityChanged()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelWritabilityChanged
in interface ChannelInboundHandler
channelWritabilityChanged
in class ChannelInboundHandlerAdapter
java.lang.Exception
private void discard(java.lang.Throwable cause)
private boolean doFlush(ChannelHandlerContext ctx) throws java.lang.Exception
java.lang.Exception
static void closeInput(ChunkedInput<?> chunks)
private static int amount(java.lang.Object msg)