public class ChunkedWriteHandler extends ChannelHandlerAdapter
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()
.ChannelHandler.Sharable, ChannelHandler.Skip
Constructor and Description |
---|
ChunkedWriteHandler() |
ChunkedWriteHandler(int maxPendingWrites)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
void |
channelInactive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelHandler in the ChannelPipeline . |
void |
channelWritabilityChanged(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelWritabilityChanged() to forward
to the next ChannelHandler in the ChannelPipeline . |
void |
flush(ChannelHandlerContext ctx)
|
void |
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
void |
resumeTransfer()
Continues to fetch the chunks from the input.
|
void |
write(ChannelHandlerContext ctx,
Object msg,
ChannelPromise promise)
Calls
ChannelHandlerContext.write(Object) to forward
to the next ChannelHandler in the ChannelPipeline . |
bind, channelActive, channelRead, channelReadComplete, channelRegistered, channelUnregistered, close, connect, deregister, disconnect, exceptionCaught, handlerRemoved, isSharable, read, userEventTriggered
public ChunkedWriteHandler()
@Deprecated public ChunkedWriteHandler(int maxPendingWrites)
ChunkedWriteHandler()
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerAdded
in interface ChannelHandler
handlerAdded
in class ChannelHandlerAdapter
Exception
public void resumeTransfer()
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.write(Object)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
in interface ChannelHandler
write
in class ChannelHandlerAdapter
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error accourpublic void flush(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.flush()
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.flush
in interface ChannelHandler
flush
in class ChannelHandlerAdapter
ctx
- the ChannelHandlerContext
for which the flush operation is madeException
- thrown if an error accourpublic void channelInactive(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.fireChannelInactive()
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelInactive
in interface ChannelHandler
channelInactive
in class ChannelHandlerAdapter
Exception
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.fireChannelWritabilityChanged()
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelWritabilityChanged
in interface ChannelHandler
channelWritabilityChanged
in class ChannelHandlerAdapter
Exception
Copyright © 2008–2015 The Netty Project. All rights reserved.