|
13 | 13 | */
|
14 | 14 | package org.asynchttpclient.netty.request.body;
|
15 | 15 |
|
16 |
| -import static org.asynchttpclient.util.MiscUtils.closeSilently; |
17 | 16 | import io.netty.channel.Channel;
|
18 |
| -import io.netty.channel.ChannelFuture; |
19 |
| -import io.netty.channel.ChannelProgressiveFuture; |
20 | 17 | import io.netty.channel.DefaultFileRegion;
|
21 |
| -import io.netty.channel.FileRegion; |
22 | 18 | import io.netty.handler.codec.http.LastHttpContent;
|
23 |
| -import io.netty.handler.stream.ChunkedFile; |
| 19 | +import io.netty.handler.stream.ChunkedNioFile; |
24 | 20 |
|
25 | 21 | import java.io.File;
|
26 | 22 | import java.io.IOException;
|
27 | 23 | import java.io.RandomAccessFile;
|
| 24 | +import java.nio.channels.FileChannel; |
28 | 25 |
|
29 | 26 | import org.asynchttpclient.AsyncHttpClientConfig;
|
30 | 27 | import org.asynchttpclient.netty.NettyResponseFuture;
|
@@ -72,26 +69,16 @@ public String getContentType() {
|
72 | 69 |
|
73 | 70 | @Override
|
74 | 71 | public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
|
75 |
| - final RandomAccessFile raf = new RandomAccessFile(file, "r"); |
| 72 | + @SuppressWarnings("resource") |
| 73 | + // Netty will close the ChunkedNioFile or the DefaultFileRegion |
| 74 | + final FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); |
76 | 75 |
|
77 |
| - try { |
78 |
| - ChannelFuture writeFuture; |
79 |
| - if (ChannelManager.isSslHandlerConfigured(channel.pipeline()) || config.isDisableZeroCopy()) { |
80 |
| - writeFuture = channel.write(new ChunkedFile(raf, offset, length, config.getChunkedFileChunkSize()), channel.newProgressivePromise()); |
81 |
| - } else { |
82 |
| - FileRegion region = new DefaultFileRegion(raf.getChannel(), offset, length); |
83 |
| - writeFuture = channel.write(region, channel.newProgressivePromise()); |
84 |
| - } |
85 |
| - writeFuture.addListener(new ProgressListener(future.getAsyncHandler(), future, false, getContentLength()) { |
86 |
| - public void operationComplete(ChannelProgressiveFuture cf) { |
87 |
| - closeSilently(raf); |
88 |
| - super.operationComplete(cf); |
89 |
| - } |
90 |
| - }); |
91 |
| - channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); |
92 |
| - } catch (IOException ex) { |
93 |
| - closeSilently(raf); |
94 |
| - throw ex; |
95 |
| - } |
| 76 | + Object message = (ChannelManager.isSslHandlerConfigured(channel.pipeline()) || config.isDisableZeroCopy()) ? // |
| 77 | + new ChunkedNioFile(fileChannel, offset, length, config.getChunkedFileChunkSize()) |
| 78 | + : new DefaultFileRegion(fileChannel, offset, length); |
| 79 | + |
| 80 | + channel.write(message, channel.newProgressivePromise())// |
| 81 | + .addListener(new ProgressListener(future.getAsyncHandler(), future, false, getContentLength())); |
| 82 | + channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); |
96 | 83 | }
|
97 | 84 | }
|
0 commit comments