Skip to content

Commit 2c7a9e2

Browse files
committed
Don't use FileInputStream that creates Finalizers
1 parent 9ed0991 commit 2c7a9e2

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

client/src/main/java/org/asynchttpclient/AsyncCompletionHandler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ public void onThrowable(Throwable t) {
8181
abstract public T onCompleted(Response response) throws Exception;
8282

8383
/**
84-
* Invoked when the content (a {@link java.io.File}, {@link String} or {@link java.io.FileInputStream} has been fully
85-
* written on the I/O socket.
84+
* Invoked when the HTTP headers have been fully written on the I/O socket.
8685
*
8786
* @return a {@link org.asynchttpclient.AsyncHandler.State} telling to CONTINUE or ABORT the current processing.
8887
*/
@@ -91,7 +90,7 @@ public State onHeadersWritten() {
9190
}
9291

9392
/**
94-
* Invoked when the content (a {@link java.io.File}, {@link String} or {@link java.io.FileInputStream} has been fully
93+
* Invoked when the content (a {@link java.io.File}, {@link String} or {@link java.io.InputStream} has been fully
9594
* written on the I/O socket.
9695
*
9796
* @return a {@link org.asynchttpclient.AsyncHandler.State} telling to CONTINUE or ABORT the current processing.

client/src/main/java/org/asynchttpclient/request/body/multipart/part/FileMultipartPart.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
import static org.asynchttpclient.util.MiscUtils.closeSilently;
1717
import io.netty.buffer.ByteBuf;
1818

19-
import java.io.FileInputStream;
2019
import java.io.FileNotFoundException;
2120
import java.io.IOException;
21+
import java.io.RandomAccessFile;
2222
import java.nio.channels.FileChannel;
2323
import java.nio.channels.WritableByteChannel;
2424

@@ -34,7 +34,7 @@ public class FileMultipartPart extends FileLikeMultipartPart<FilePart> {
3434
public FileMultipartPart(FilePart part, byte[] boundary) {
3535
super(part, boundary);
3636
try {
37-
channel = new FileInputStream(part.getFile()).getChannel();
37+
channel = new RandomAccessFile(part.getFile(), "r").getChannel();
3838
} catch (FileNotFoundException e) {
3939
throw new IllegalArgumentException("File part doesn't exist: " + part.getFile().getAbsolutePath(), e);
4040
}

client/src/test/java/org/asynchttpclient/request/body/ChunkingTest.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818
import static org.testng.FileAssert.fail;
1919

2020
import java.io.BufferedInputStream;
21-
import java.io.FileInputStream;
2221
import java.io.IOException;
2322
import java.io.InputStream;
2423
import java.nio.ByteBuffer;
24+
import java.nio.file.Files;
2525

2626
import org.asynchttpclient.AbstractBasicTest;
2727
import org.asynchttpclient.AsyncHttpClient;
2828
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
2929
import org.asynchttpclient.ListenableFuture;
3030
import org.asynchttpclient.Request;
31-
import org.asynchttpclient.RequestBuilder;
3231
import org.asynchttpclient.Response;
3332
import org.asynchttpclient.request.body.generator.FeedableBodyGenerator;
3433
import org.asynchttpclient.request.body.generator.InputStreamBodyGenerator;
@@ -41,47 +40,46 @@ public class ChunkingTest extends AbstractBasicTest {
4140
// and doesn't contain the chunked delimeters.
4241
@Test(groups = "standalone")
4342
public void testBufferLargerThanFileWithStreamBodyGenerator() throws Throwable {
44-
doTestWithInputStreamBodyGenerator(new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE), 400000));
43+
doTestWithInputStreamBodyGenerator(new BufferedInputStream(Files.newInputStream(LARGE_IMAGE_FILE.toPath()), 400000));
4544
}
4645

4746
@Test(groups = "standalone")
4847
public void testBufferSmallThanFileWithStreamBodyGenerator() throws Throwable {
49-
doTestWithInputStreamBodyGenerator(new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)));
48+
doTestWithInputStreamBodyGenerator(new BufferedInputStream(Files.newInputStream(LARGE_IMAGE_FILE.toPath())));
5049
}
5150

5251
@Test(groups = "standalone")
5352
public void testDirectFileWithStreamBodyGenerator() throws Throwable {
54-
doTestWithInputStreamBodyGenerator(new FileInputStream(LARGE_IMAGE_FILE));
53+
doTestWithInputStreamBodyGenerator(Files.newInputStream(LARGE_IMAGE_FILE.toPath()));
5554
}
5655

5756
@Test(groups = "standalone")
5857
public void testDirectFileWithFeedableBodyGenerator() throws Throwable {
59-
doTestWithFeedableBodyGenerator(new FileInputStream(LARGE_IMAGE_FILE));
58+
doTestWithFeedableBodyGenerator(Files.newInputStream(LARGE_IMAGE_FILE.toPath()));
6059
}
6160

6261
public void doTestWithInputStreamBodyGenerator(InputStream is) throws Throwable {
63-
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
64-
65-
RequestBuilder builder = post(getTargetUrl()).setBody(new InputStreamBodyGenerator(is));
66-
67-
Request r = builder.build();
68-
69-
final ListenableFuture<Response> responseFuture = c.executeRequest(r);
70-
waitForAndAssertResponse(responseFuture);
62+
try {
63+
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
64+
ListenableFuture<Response> responseFuture = c.executeRequest(post(getTargetUrl()).setBody(new InputStreamBodyGenerator(is)));
65+
waitForAndAssertResponse(responseFuture);
66+
}
67+
} finally {
68+
is.close();
7169
}
7270
}
7371

7472
public void doTestWithFeedableBodyGenerator(InputStream is) throws Throwable {
75-
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
76-
77-
final FeedableBodyGenerator feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
78-
Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build();
79-
80-
ListenableFuture<Response> responseFuture = c.executeRequest(r);
81-
82-
feed(feedableBodyGenerator, is);
83-
84-
waitForAndAssertResponse(responseFuture);
73+
try {
74+
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
75+
final FeedableBodyGenerator feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
76+
Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build();
77+
ListenableFuture<Response> responseFuture = c.executeRequest(r);
78+
feed(feedableBodyGenerator, is);
79+
waitForAndAssertResponse(responseFuture);
80+
}
81+
} finally {
82+
is.close();
8583
}
8684
}
8785

client/src/test/java/org/asynchttpclient/request/body/multipart/MultipartUploadTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
import java.io.ByteArrayOutputStream;
2121
import java.io.File;
22-
import java.io.FileInputStream;
2322
import java.io.FileNotFoundException;
2423
import java.io.FileOutputStream;
2524
import java.io.IOException;
2625
import java.io.InputStream;
2726
import java.io.OutputStream;
2827
import java.io.Writer;
28+
import java.nio.file.Files;
2929
import java.util.ArrayList;
3030
import java.util.Arrays;
3131
import java.util.List;
@@ -215,7 +215,7 @@ private void testSentFile(List<String> expectedContents, List<File> sourceFiles,
215215

216216
ByteArrayOutputStream baos = new ByteArrayOutputStream();
217217
byte[] sourceBytes = null;
218-
try (FileInputStream instream = new FileInputStream(sourceFile)) {
218+
try (InputStream instream = Files.newInputStream(sourceFile.toPath())) {
219219
byte[] buf = new byte[8092];
220220
int len = 0;
221221
while ((len = instream.read(buf)) > 0) {
@@ -237,7 +237,7 @@ private void testSentFile(List<String> expectedContents, List<File> sourceFiles,
237237
assertTrue(tmp.exists());
238238

239239
byte[] bytes;
240-
try (FileInputStream instream = new FileInputStream(tmp)) {
240+
try (InputStream instream = Files.newInputStream(tmp.toPath())) {
241241
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
242242
byte[] buf = new byte[8092];
243243
int len = 0;
@@ -253,7 +253,7 @@ private void testSentFile(List<String> expectedContents, List<File> sourceFiles,
253253
String helloString = new String(bytes);
254254
assertEquals(helloString, expectedContents.get(i));
255255
} else {
256-
try (FileInputStream instream = new FileInputStream(tmp)) {
256+
try (InputStream instream = Files.newInputStream(tmp.toPath())) {
257257
ByteArrayOutputStream baos3 = new ByteArrayOutputStream();
258258
GZIPInputStream deflater = new GZIPInputStream(instream);
259259
try {

0 commit comments

Comments
 (0)