Top Description Inners Fields Constructors Methods
io.netty.handler.stream

public Class ChunkedWriteHandler

extends ChannelDuplexHandler
Class Inheritance
Static Imports
io.netty.util.internal.ObjectUtil.checkPositive

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:

ChannelPipeline p = ...;
p.addLast("streamer", new ChunkedWriteHandler());
p.addLast("handler", new MyHandler());
Once inserted, you can write a 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(new ChunkedFile(new File("video.mkv"));

Sending a stream which generates a chunk intermittently

Some 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().

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private volatile ChannelHandlerContext
private static final InternalLogger
private Queue<ChunkedWriteHandler.PendingWrite>

Constructor Summary

AccessConstructor and Description
public
public
ChunkedWriteHandler(int maxPendingWrites)

Deprecated use ChunkedWriteHandler()

Method Summary

Modifier and TypeMethod and Description
private void
public void
public void
private static void
private void
private void
public void
private static void
private void
handleFuture(ChannelFuture future, ChunkedInput<?> input, ChunkedWriteHandler.PendingWrite currentWrite, boolean resume)

public void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
private boolean
public void
resumeTransfer()

Continues to fetch the chunks from the input.

private void
public void
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
Inherited from io.netty.channel.ChannelDuplexHandler:
bindcloseconnectderegisterdisconnectread

Field Detail

ctxback to summary
private volatile ChannelHandlerContext ctx
loggerback to summary
private static final InternalLogger logger
queueback to summary
private Queue<ChunkedWriteHandler.PendingWrite> queue

Constructor Detail

ChunkedWriteHandlerback to summary
public ChunkedWriteHandler()
ChunkedWriteHandlerback to summary
public ChunkedWriteHandler(int maxPendingWrites)

Deprecated

use ChunkedWriteHandler()

Annotations
@Deprecated

Method Detail

allocateQueueback to summary
private void allocateQueue()
channelInactiveback to summary
public void channelInactive(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelInactive.

Implements io.netty.channel.ChannelInboundHandler.channelInactive.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelInactive.

Calls ChannelHandlerContext#fireChannelInactive() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelWritabilityChangedback to summary
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged.

Implements io.netty.channel.ChannelInboundHandler.channelWritabilityChanged.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged.

Calls ChannelHandlerContext#fireChannelWritabilityChanged() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
closeInputback to summary
private static void closeInput(ChunkedInput<?> chunks)
discardback to summary
private void discard(Throwable cause)
doFlushback to summary
private void doFlush(final ChannelHandlerContext ctx)
flushback to summary
public void flush(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.flush.

Implements io.netty.channel.ChannelOutboundHandler.flush.

Doc from io.netty.channel.ChannelDuplexHandler.flush.

Calls ChannelHandlerContext#flush() to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.flush.

the ChannelHandlerContext for which the flush operation is made

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.flush.

thrown if an error occurs

handleEndOfInputFutureback to summary
private static void handleEndOfInputFuture(ChannelFuture future, ChunkedInput<?> input, ChunkedWriteHandler.PendingWrite currentWrite)
handleFutureback to summary
private void handleFuture(ChannelFuture future, ChunkedInput<?> input, ChunkedWriteHandler.PendingWrite currentWrite, boolean resume)
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Doc from io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

Annotations
@Override
queueIsEmptyback to summary
private boolean queueIsEmpty()
resumeTransferback to summary
public void resumeTransfer()

Continues to fetch the chunks from the input.

resumeTransfer0back to summary
private void resumeTransfer0(ChannelHandlerContext ctx)
writeback to summary
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelDuplexHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs

io.netty.handler.stream back to summary

private final Class ChunkedWriteHandler.PendingWrite

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final Object
pack-priv final ChannelPromise

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
fail(Throwable cause)

pack-priv void
progress(long progress, long total)

pack-priv void
success(long total)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

msgback to summary
pack-priv final Object msg
promiseback to summary
pack-priv final ChannelPromise promise

Constructor Detail

PendingWriteback to summary
pack-priv PendingWrite(Object msg, ChannelPromise promise)

Method Detail

failback to summary
pack-priv void fail(Throwable cause)
progressback to summary
pack-priv void progress(long progress, long total)
successback to summary
pack-priv void success(long total)