Skip to content

Commit 2b2bc8f

Browse files
committed
Avoid closing the file descriptor when sending directly a FileChannel/RandomAccessFile.
Motivation: Sending a FileChannel/RandomAccessFile should not close it, currently unlike for sending file region, it is closed by the ChunkedWriteHandle. Changes: Override ChunkedNioFile to prevent the file descriptor to be closed
1 parent 47dd02d commit 2b2bc8f

3 files changed

Lines changed: 7 additions & 3 deletions

File tree

vertx-core/src/main/java/io/vertx/core/net/impl/VertxConnection.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,12 @@ public ChannelFuture sendFile(FileChannel fc, long offset, long length) {
499499
if (!supportsFileRegion()) {
500500
// Cannot use zero-copy
501501
try {
502-
writeToChannel(new ChunkedNioFile(fc, offset, length, 8192), writeFuture);
502+
writeToChannel(new ChunkedNioFile(fc, offset, length, 8192) {
503+
@Override
504+
public void close() {
505+
// The called is responsible for closing the file
506+
}
507+
}, writeFuture);
503508
} catch (IOException e) {
504509
return chctx.newFailedFuture(e);
505510
}

vertx-core/src/test/java/io/vertx/tests/http/sendfile/Http1xSendFileTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
package io.vertx.tests.http.sendfile;
1212

13-
import io.vertx.core.Future;
1413
import io.vertx.core.http.HttpClientResponse;
1514
import io.vertx.core.http.HttpHeaders;
1615
import io.vertx.core.http.HttpServerOptions;

vertx-core/src/test/java/io/vertx/tests/http/sendfile/HttpSendFileTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public void testSendFileWithRandomAccessFileRange() throws Exception {
354354

355355
private RandomAccessFile testSendFileWithFileChannel(int flen, BiFunction<RandomAccessFile, HttpServerResponse, Future<?>> sender,
356356
String expectedContentType, long expectedLength) throws Exception {
357-
Assume.assumeTrue(this.getClass() == Http1xSendFileTest.class);
357+
Assume.assumeTrue(this instanceof Http1xSendFileTest);
358358
File file = TestUtils.tmpFile(".dat", flen);
359359
RandomAccessFile raf = new RandomAccessFile(file, "r");
360360
server.requestHandler(req -> sender.apply(raf, req.response()).onComplete(onSuccess(v -> testComplete())));

0 commit comments

Comments
 (0)