- java.lang.Object
-
- io.netty5.handler.stream.ChunkedWriteHandler
-
- All Implemented Interfaces:
ChannelHandler
public class ChunkedWriteHandler extends Object implements ChannelHandler
AChannelHandler
that 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 aChannelHandler
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 newChunkedWriteHandler
instance:ChannelPipeline
p = ...; p.addLast("streamer", newChunkedWriteHandler
()); p.addLast("handler", new MyHandler());ChunkedInput
so that theChunkedWriteHandler
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. SuchChunkedInput
implementation often returnsnull
onChunkedInput.readChunk(BufferAllocator)
, resulting in the indefinitely suspended transfer. To resume the transfer when a new chunk is available, you have to callresumeTransfer()
.
-
-
Constructor Summary
Constructors Constructor Description ChunkedWriteHandler()
ChunkedWriteHandler(int maxPendingWrites)
Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelInactive(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.void
channelWritabilityChanged(ChannelHandlerContext ctx)
Gets called once the writable state of aChannel
changed.void
flush(ChannelHandlerContext ctx)
Called once a flush operation is made.void
handlerAdded(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.void
resumeTransfer()
Continues to fetch the chunks from the input.Future<Void>
write(ChannelHandlerContext ctx, Object msg)
Called once a write operation is made.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelExceptionCaught, channelInboundEvent, channelRead, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, close, connect, deregister, disconnect, handlerRemoved, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
ChunkedWriteHandler
public ChunkedWriteHandler()
-
ChunkedWriteHandler
@Deprecated public ChunkedWriteHandler(int maxPendingWrites)
Deprecated.
-
-
Method Detail
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.- Specified by:
handlerAdded
in interfaceChannelHandler
- Throws:
Exception
-
resumeTransfer
public void resumeTransfer()
Continues to fetch the chunks from the input.
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandler
Called once a write operation is made. The write operation will write the messages through theChannelPipeline
. Those are then ready to be flushed to the actualChannel
onceChannel.flush()
is called.- Specified by:
write
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to write- Returns:
- the
Future
which will be notified once the operation completes.
-
flush
public void flush(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a flush operation is made. The flush operation will try to flush out all previous written messages that are pending.- Specified by:
flush
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the flush operation is made
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.- Specified by:
channelInactive
in interfaceChannelHandler
- Throws:
Exception
-
channelWritabilityChanged
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called once the writable state of aChannel
changed. You can check the state withChannel.writableBytes()
orChannel.isWritable()
.- Specified by:
channelWritabilityChanged
in interfaceChannelHandler
- Throws:
Exception
-
-