public class ChunkedWriteHandler extends java.lang.Object implements ChannelUpstreamHandler, ChannelDownstreamHandler, LifeCycleAwareChannelHandler
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"));
ChunkedInput generates a chunk on a certain event or timing.
Such ChunkedInput implementation often returns null on
ChunkedInput.nextChunk(), resulting in the indefinitely suspended
transfer. To resume the transfer when a new chunk is available, you have to
call resumeTransfer().ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
private ChannelHandlerContext |
ctx |
private MessageEvent |
currentEvent |
private java.util.concurrent.atomic.AtomicBoolean |
flush |
private boolean |
flushNeeded |
private static InternalLogger |
logger |
private java.util.Queue<MessageEvent> |
queue |
| Constructor and Description |
|---|
ChunkedWriteHandler() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterAdd(ChannelHandlerContext ctx) |
void |
afterRemove(ChannelHandlerContext ctx) |
void |
beforeAdd(ChannelHandlerContext ctx) |
void |
beforeRemove(ChannelHandlerContext ctx) |
(package private) static void |
closeInput(ChunkedInput chunks) |
private void |
discard(ChannelHandlerContext ctx,
boolean fireNow) |
private void |
flush(ChannelHandlerContext ctx,
boolean fireNow) |
void |
handleDownstream(ChannelHandlerContext ctx,
ChannelEvent e)
Handles the specified downstream event.
|
void |
handleUpstream(ChannelHandlerContext ctx,
ChannelEvent e)
Handles the specified upstream event.
|
void |
resumeTransfer()
Continues to fetch the chunks from the input.
|
private static final InternalLogger logger
private final java.util.Queue<MessageEvent> queue
private volatile ChannelHandlerContext ctx
private final java.util.concurrent.atomic.AtomicBoolean flush
private MessageEvent currentEvent
private volatile boolean flushNeeded
public void resumeTransfer()
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws java.lang.Exception
ChannelDownstreamHandlerhandleDownstream in interface ChannelDownstreamHandlerctx - the context object for this handlere - the downstream event to process or interceptjava.lang.Exceptionpublic void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws java.lang.Exception
ChannelUpstreamHandlerhandleUpstream in interface ChannelUpstreamHandlerctx - the context object for this handlere - the upstream event to process or interceptjava.lang.Exceptionprivate void discard(ChannelHandlerContext ctx, boolean fireNow)
private void flush(ChannelHandlerContext ctx, boolean fireNow) throws java.lang.Exception
java.lang.Exceptionstatic void closeInput(ChunkedInput chunks)
public void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
beforeAdd in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
afterAdd in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
beforeRemove in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
afterRemove in interface LifeCycleAwareChannelHandlerjava.lang.Exception