Skip to content

Commit 21c1005

Browse files
committed
Don't use FileOutputStream that creates Finalizers
1 parent 2c7a9e2 commit 21c1005

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

client/src/main/java/org/asynchttpclient/handler/BodyDeferringAsyncHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* handling than "recommended" way. Some examples:
4747
* <br>
4848
* <pre>
49-
* FileOutputStream fos = ...
49+
* OutputStream fos = ...
5050
* BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(fos);
5151
* // client executes async
5252
* Future&lt;Response&gt; fr = client.prepareGet(&quot;http://foo.com/aresource&quot;).execute(

client/src/main/java/org/asynchttpclient/handler/resumable/PropertiesBasedResumableProcessor.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
*/
1313
package org.asynchttpclient.handler.resumable;
1414

15-
import static java.nio.charset.StandardCharsets.*;
15+
import static java.nio.charset.StandardCharsets.UTF_8;
1616
import static org.asynchttpclient.util.MiscUtils.closeSilently;
1717

1818
import java.io.File;
1919
import java.io.FileNotFoundException;
20-
import java.io.FileOutputStream;
20+
import java.io.OutputStream;
21+
import java.nio.file.Files;
2122
import java.util.Map;
2223
import java.util.Scanner;
2324
import java.util.concurrent.ConcurrentHashMap;
@@ -59,7 +60,7 @@ public void remove(String uri) {
5960
@Override
6061
public void save(Map<String, Long> map) {
6162
log.debug("Saving current download state {}", properties.toString());
62-
FileOutputStream os = null;
63+
OutputStream os = null;
6364
try {
6465

6566
if (!TMP.exists() && !TMP.mkdirs()) {
@@ -73,17 +74,15 @@ public void save(Map<String, Long> map) {
7374
throw new IllegalStateException();
7475
}
7576

76-
os = new FileOutputStream(f);
77-
77+
os = Files.newOutputStream(f.toPath());
7878
for (Map.Entry<String, Long> e : properties.entrySet()) {
7979
os.write(append(e).getBytes(UTF_8));
8080
}
8181
os.flush();
8282
} catch (Throwable e) {
8383
log.warn(e.getMessage(), e);
8484
} finally {
85-
if (os != null)
86-
closeSilently(os);
85+
closeSilently(os);
8786
}
8887
}
8988

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
*/
1313
package org.asynchttpclient.request.body;
1414

15-
import static org.asynchttpclient.Dsl.*;
15+
import static org.asynchttpclient.Dsl.asyncHttpClient;
1616
import static org.asynchttpclient.test.TestUtils.*;
1717
import static org.testng.Assert.*;
1818

1919
import java.io.File;
20-
import java.io.FileOutputStream;
2120
import java.io.IOException;
21+
import java.io.OutputStream;
2222
import java.net.URISyntaxException;
23+
import java.nio.file.Files;
2324
import java.util.concurrent.ExecutionException;
2425
import java.util.concurrent.Future;
2526
import java.util.concurrent.TimeoutException;
@@ -117,7 +118,7 @@ public void zeroCopyFileTest() throws IOException, ExecutionException, TimeoutEx
117118
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
118119
tmp.deleteOnExit();
119120
try (AsyncHttpClient client = asyncHttpClient()) {
120-
try (FileOutputStream stream = new FileOutputStream(tmp)) {
121+
try (OutputStream stream = Files.newOutputStream(tmp.toPath())) {
121122
Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {
122123
public void onThrowable(Throwable t) {
123124
}
@@ -150,7 +151,7 @@ public void zeroCopyFileWithBodyManipulationTest() throws IOException, Execution
150151
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
151152
tmp.deleteOnExit();
152153
try (AsyncHttpClient client = asyncHttpClient()) {
153-
try (FileOutputStream stream = new FileOutputStream(tmp)) {
154+
try (OutputStream stream = Files.newOutputStream(tmp.toPath())) {
154155
Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {
155156
public void onThrowable(Throwable t) {
156157
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.ByteArrayOutputStream;
2121
import java.io.File;
2222
import java.io.FileNotFoundException;
23-
import java.io.FileOutputStream;
2423
import java.io.IOException;
2524
import java.io.InputStream;
2625
import java.io.OutputStream;
@@ -130,7 +129,7 @@ public void testSendingSmallFilesAndByteArray() throws IOException {
130129

131130
boolean tmpFileCreated = false;
132131
File tmpFile = File.createTempFile("textbytearray", ".txt");
133-
try (FileOutputStream os = new FileOutputStream(tmpFile)) {
132+
try (OutputStream os = Files.newOutputStream(tmpFile.toPath())) {
134133
IOUtils.write(expectedContents.getBytes(UTF_8), os);
135134
tmpFileCreated = true;
136135

@@ -349,7 +348,7 @@ public void service(HttpServletRequest request, HttpServletResponse response) th
349348
// Process the input stream
350349
File tmpFile = File.createTempFile(UUID.randomUUID().toString() + "_MockUploadServlet", ".tmp");
351350
tmpFile.deleteOnExit();
352-
try (OutputStream os = new FileOutputStream(tmpFile)) {
351+
try (OutputStream os = Files.newOutputStream(tmpFile.toPath())) {
353352
byte[] buffer = new byte[4096];
354353
int bytesRead;
355354
while ((bytesRead = stream.read(buffer)) != -1) {

client/src/test/java/org/asynchttpclient/test/TestUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818

1919
import java.io.File;
2020
import java.io.FileNotFoundException;
21-
import java.io.FileOutputStream;
2221
import java.io.IOException;
2322
import java.io.InputStream;
23+
import java.io.OutputStream;
2424
import java.net.ServerSocket;
2525
import java.net.URI;
2626
import java.net.URISyntaxException;
2727
import java.net.URL;
2828
import java.nio.ByteBuffer;
2929
import java.nio.charset.Charset;
30+
import java.nio.file.Files;
3031
import java.security.GeneralSecurityException;
3132
import java.security.KeyStore;
3233
import java.security.SecureRandom;
@@ -137,7 +138,7 @@ public static File createTempFile(int approxSize) throws IOException {
137138
long repeats = approxSize / TestUtils.PATTERN_BYTES.length + 1;
138139
File tmpFile = File.createTempFile("tmpfile-", ".data", TMP_DIR);
139140
tmpFile.deleteOnExit();
140-
try (FileOutputStream out = new FileOutputStream(tmpFile)) {
141+
try (OutputStream out = Files.newOutputStream(tmpFile.toPath())) {
141142
for (int i = 0; i < repeats; i++) {
142143
out.write(PATTERN_BYTES);
143144
}

0 commit comments

Comments
 (0)