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
AChannelHandlerthat 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 aChannelHandlerimplementation.ChunkedWriteHandlermanages such complicated states so that you can send a large data stream without difficulties.To use
ChunkedWriteHandlerin your application, you have to insert a newChunkedWriteHandlerinstance:
Once inserted, you can write aChannelPipelinep = ...; p.addLast("streamer", newChunkedWriteHandler()); p.addLast("handler", new MyHandler());ChunkedInputso that theChunkedWriteHandlercan 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
SomeChunkedInputgenerates a chunk on a certain event or timing. SuchChunkedInputimplementation often returnsnullonChunkedInput.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 ChannelHandlerContextctxprivate MessageEventcurrentEventprivate java.util.concurrent.atomic.AtomicBooleanflushprivate booleanflushNeededprivate static InternalLoggerloggerprivate 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 voidafterAdd(ChannelHandlerContext ctx)voidafterRemove(ChannelHandlerContext ctx)voidbeforeAdd(ChannelHandlerContext ctx)voidbeforeRemove(ChannelHandlerContext ctx)(package private) static voidcloseInput(ChunkedInput chunks)private voiddiscard(ChannelHandlerContext ctx, boolean fireNow)private voidflush(ChannelHandlerContext ctx, boolean fireNow)voidhandleDownstream(ChannelHandlerContext ctx, ChannelEvent e)Handles the specified downstream event.voidhandleUpstream(ChannelHandlerContext ctx, ChannelEvent e)Handles the specified upstream event.voidresumeTransfer()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:ChannelDownstreamHandlerHandles the specified downstream event.- Specified by:
handleDownstreamin 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:ChannelUpstreamHandlerHandles the specified upstream event.- Specified by:
handleUpstreamin 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:
beforeAddin interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
afterAdd
public void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterAddin interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
beforeRemove
public void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeRemovein interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
afterRemove
public void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterRemovein interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
-