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

public Class ChunkedWriteHandler

extends Object
implements ChannelHandler
Class Inheritance
All Implemented Interfaces
io.netty5.channel.ChannelHandler
Imports
io.netty5.buffer.BufferAllocator, io.netty5.util.Resource, io.netty5.channel.Channel, .ChannelHandler, .ChannelHandlerContext, .ChannelPipeline, io.netty5.util.concurrent.Future, .Promise, io.netty5.util.internal.SilentDispose, io.netty5.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.nio.channels.ClosedChannelException, java.util.ArrayDeque, .Queue

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(BufferAllocator), 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 final Queue<ChunkedWriteHandler.PendingWrite>

Constructor Summary

AccessConstructor and Description
public
public
ChunkedWriteHandler(int maxPendingWrites)

Deprecated use ChunkedWriteHandler()

Method Summary

Modifier and TypeMethod and Description
public void
channelInactive(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.channelInactive.

The Channel of the ChannelHandlerContext was registered is now inactive and reached its end of lifetime.
public void
channelWritabilityChanged(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.channelWritabilityChanged.

Gets called once the writable state of a Channel changed.
private static void
private void
private void
public void
flush(ChannelHandlerContext
the ChannelHandlerContext for which the flush operation is made
ctx
)

Overrides default io.netty5.channel.ChannelHandler.flush.

Called once a flush operation is made.
private static void
private void
handleFuture(Channel channel, Future<?> future, ChunkedWriteHandler.PendingWrite currentWrite, boolean resume)

public void
handlerAdded(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.handlerAdded.

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

Continues to fetch the chunks from the input.

private void
public Future<Void>
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
)

Overrides default io.netty5.channel.ChannelHandler.write.

Called once a write operation is made.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ctxback to summary
private volatile ChannelHandlerContext ctx
loggerback to summary
private static final InternalLogger logger
queueback to summary
private final 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

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

Overrides default io.netty5.channel.ChannelHandler.channelInactive.

Doc from io.netty5.channel.ChannelHandler.channelInactive.

The Channel of the ChannelHandlerContext was registered is now inactive and reached its end of lifetime.

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

Overrides default io.netty5.channel.ChannelHandler.channelWritabilityChanged.

Doc from io.netty5.channel.ChannelHandler.channelWritabilityChanged.

Gets called once the writable state of a Channel changed. You can check the state with Channel#writableBytes() or Channel#isWritable().

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)

Overrides default io.netty5.channel.ChannelHandler.flush.

Doc from io.netty5.channel.ChannelHandler.flush.

Called once a flush operation is made. The flush operation will try to flush out all previous written messages that are pending.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which the flush operation is made

Annotations
@Override
handleEndOfInputFutureback to summary
private static void handleEndOfInputFuture(Future<?> future, ChunkedWriteHandler.PendingWrite currentWrite)
handleFutureback to summary
private void handleFuture(Channel channel, Future<?> future, ChunkedWriteHandler.PendingWrite currentWrite, boolean resume)
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.handlerAdded.

Doc from io.netty5.channel.ChannelHandler.handlerAdded.

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

Annotations
@Override
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 Future<Void> write(ChannelHandlerContext ctx, Object msg)

Overrides default io.netty5.channel.ChannelHandler.write.

Doc from io.netty5.channel.ChannelHandler.write.

Called once a write operation is made. The write operation will write the messages through the ChannelPipeline. Those are then ready to be flushed to the actual Channel once Channel#flush() is called.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which the write operation is made

msg:Object

the message to write

Returns:Future<Void>

the Future which will be notified once the operation completes.

Annotations
@Override
io.netty5.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 Promise<Void>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

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

pack-priv void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

msgback to summary
pack-priv final Object msg
promiseback to summary
pack-priv final Promise<Void> promise

Constructor Detail

PendingWriteback to summary
pack-priv PendingWrite(Object msg, Promise<Void> promise)

Method Detail

failback to summary
pack-priv void fail(Throwable cause)
successback to summary
pack-priv void success()