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 Object
implements ChannelUpstreamHandler, ChannelDownstreamHandler, LifeCycleAwareChannelHandler
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.nextChunk()
, 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 classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ChannelHandlerContext
private MessageEvent
private final AtomicBoolean
private boolean
private static final InternalLogger
private final Queue
<MessageEvent> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
void
void
(package private) static void
closeInput
(ChunkedInput chunks) private void
discard
(ChannelHandlerContext ctx, boolean fireNow) private void
flush
(ChannelHandlerContext ctx, boolean fireNow) void
Handles the specified downstream event.void
Handles the specified upstream event.void
Continues to fetch the chunks from the input.
-
Field Details
-
logger
-
queue
-
ctx
-
flush
-
currentEvent
-
flushNeeded
private volatile boolean flushNeeded
-
-
Constructor Details
-
ChunkedWriteHandler
public ChunkedWriteHandler()
-
-
Method Details
-
resumeTransfer
public void resumeTransfer()Continues to fetch the chunks from the input. -
handleDownstream
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:
Exception
-
handleUpstream
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:
Exception
-
discard
-
flush
- Throws:
Exception
-
closeInput
-
beforeAdd
- Specified by:
beforeAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
Exception
-
afterAdd
- Specified by:
afterAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
Exception
-
beforeRemove
- Specified by:
beforeRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
Exception
-
afterRemove
- Specified by:
afterRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
Exception
-