Package org.jboss.netty.handler.stream
Class ChunkedWriteHandler
- java.lang.Object
-
- org.jboss.netty.handler.stream.ChunkedWriteHandler
-
- All Implemented Interfaces:
ChannelDownstreamHandler
,ChannelHandler
,ChannelUpstreamHandler
,LifeCycleAwareChannelHandler
public class ChunkedWriteHandler extends java.lang.Object implements ChannelUpstreamHandler, ChannelDownstreamHandler, LifeCycleAwareChannelHandler
AChannelHandler
that adds support for writing a large data stream asynchronously neither spending a lot of memory nor gettingOutOfMemoryError
. Large data streaming such as file transfer requires complicated state management in aChannelHandler
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 newChunkedWriteHandler
instance:ChannelPipeline
p = ...; p.addLast("streamer", newChunkedWriteHandler
()); p.addLast("handler", new MyHandler());ChunkedInput
so that theChunkedWriteHandler
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. SuchChunkedInput
implementation often returnsnull
onChunkedInput.nextChunk()
, resulting in the indefinitely suspended transfer. To resume the transfer when a new chunk is available, you have to callresumeTransfer()
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor Description ChunkedWriteHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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.
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
queue
private final java.util.Queue<MessageEvent> queue
-
ctx
private volatile ChannelHandlerContext ctx
-
flush
private final java.util.concurrent.atomic.AtomicBoolean flush
-
currentEvent
private MessageEvent currentEvent
-
flushNeeded
private volatile boolean flushNeeded
-
-
Method Detail
-
resumeTransfer
public void resumeTransfer()
Continues to fetch the chunks from the input.
-
handleDownstream
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws java.lang.Exception
Description copied from interface:ChannelDownstreamHandler
Handles the specified downstream event.- Specified by:
handleDownstream
in interfaceChannelDownstreamHandler
- Parameters:
ctx
- the context object for this handlere
- the downstream event to process or intercept- Throws:
java.lang.Exception
-
handleUpstream
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws java.lang.Exception
Description copied from interface:ChannelUpstreamHandler
Handles the specified upstream event.- Specified by:
handleUpstream
in interfaceChannelUpstreamHandler
- Parameters:
ctx
- the context object for this handlere
- the upstream event to process or intercept- Throws:
java.lang.Exception
-
discard
private void discard(ChannelHandlerContext ctx, boolean fireNow)
-
flush
private void flush(ChannelHandlerContext ctx, boolean fireNow) throws java.lang.Exception
- Throws:
java.lang.Exception
-
closeInput
static void closeInput(ChunkedInput chunks)
-
beforeAdd
public void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
afterAdd
public void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
beforeRemove
public void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
afterRemove
public void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
-