Skip to content

Commit 1cc6f3d

Browse files
committed
minor clean up: let Netty deal with the closing
1 parent 24d2006 commit 1cc6f3d

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

client/src/main/java/org/asynchttpclient/netty/request/body/NettyFileBody.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@
1313
*/
1414
package org.asynchttpclient.netty.request.body;
1515

16-
import static org.asynchttpclient.util.MiscUtils.closeSilently;
1716
import io.netty.channel.Channel;
18-
import io.netty.channel.ChannelFuture;
19-
import io.netty.channel.ChannelProgressiveFuture;
2017
import io.netty.channel.DefaultFileRegion;
21-
import io.netty.channel.FileRegion;
2218
import io.netty.handler.codec.http.LastHttpContent;
23-
import io.netty.handler.stream.ChunkedFile;
19+
import io.netty.handler.stream.ChunkedNioFile;
2420

2521
import java.io.File;
2622
import java.io.IOException;
2723
import java.io.RandomAccessFile;
24+
import java.nio.channels.FileChannel;
2825

2926
import org.asynchttpclient.AsyncHttpClientConfig;
3027
import org.asynchttpclient.netty.NettyResponseFuture;
@@ -72,26 +69,16 @@ public String getContentType() {
7269

7370
@Override
7471
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();
7675

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);
9683
}
9784
}

0 commit comments

Comments
 (0)