2
votes

As stated in the question, is it necessary to close a Netty ChunkedInput after sending its data through ChannelHandlerContext.writeAndFlush()? I know this operation is asynchronous so probably I am not supposed to close it immediately after calling writeAndFlush(), therefore do I need to add a FutureListener to close it?

In the official File server example, I fuond that the the close() function of the ChunkedInput is not called:

sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise());

Does it mean that the ChunkedInput is closed automatically when all the data is read, or does the channel actually help me close it?

1

1 Answers

1
votes

Yes, you almost never need to close the ChunkedInput yourself, since you will normally have a ChunkedWriteHandler in the pipeline which will close it for you after the last chunk is written. See code.

Does it mean that the ChunkedInput is closed automatically when all the data is read

There was a bug long back that closed the input after the data was read, but it has been fixed to close the input after the last chunk is written. See https://github.com/netty/netty/commit/fb52b8a3b2e3940a0bcf6e9167b157d59a811691

In the example you reference, the pipeline includes a ChunkedWriteHandler and the ChunkedFile (implementation of ChunkedFile) is closed automatically.