Skip to content

Commit

Permalink
Don't use FileInputStream that creates Finalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Apr 18, 2017
1 parent 9ed0991 commit 2c7a9e2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
Expand Up @@ -81,8 +81,7 @@ public void onThrowable(Throwable t) {
abstract public T onCompleted(Response response) throws Exception;

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

/**
* Invoked when the content (a {@link java.io.File}, {@link String} or {@link java.io.FileInputStream} has been fully
* Invoked when the content (a {@link java.io.File}, {@link String} or {@link java.io.InputStream} has been fully
* written on the I/O socket.
*
* @return a {@link org.asynchttpclient.AsyncHandler.State} telling to CONTINUE or ABORT the current processing.
Expand Down
Expand Up @@ -16,9 +16,9 @@
import static org.asynchttpclient.util.MiscUtils.closeSilently;
import io.netty.buffer.ByteBuf;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

Expand All @@ -34,7 +34,7 @@ public class FileMultipartPart extends FileLikeMultipartPart<FilePart> {
public FileMultipartPart(FilePart part, byte[] boundary) {
super(part, boundary);
try {
channel = new FileInputStream(part.getFile()).getChannel();
channel = new RandomAccessFile(part.getFile(), "r").getChannel();
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("File part doesn't exist: " + part.getFile().getAbsolutePath(), e);
}
Expand Down
Expand Up @@ -18,17 +18,16 @@
import static org.testng.FileAssert.fail;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;

import org.asynchttpclient.AbstractBasicTest;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.asynchttpclient.ListenableFuture;
import org.asynchttpclient.Request;
import org.asynchttpclient.RequestBuilder;
import org.asynchttpclient.Response;
import org.asynchttpclient.request.body.generator.FeedableBodyGenerator;
import org.asynchttpclient.request.body.generator.InputStreamBodyGenerator;
Expand All @@ -41,47 +40,46 @@ public class ChunkingTest extends AbstractBasicTest {
// and doesn't contain the chunked delimeters.
@Test(groups = "standalone")
public void testBufferLargerThanFileWithStreamBodyGenerator() throws Throwable {
doTestWithInputStreamBodyGenerator(new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE), 400000));
doTestWithInputStreamBodyGenerator(new BufferedInputStream(Files.newInputStream(LARGE_IMAGE_FILE.toPath()), 400000));
}

@Test(groups = "standalone")
public void testBufferSmallThanFileWithStreamBodyGenerator() throws Throwable {
doTestWithInputStreamBodyGenerator(new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)));
doTestWithInputStreamBodyGenerator(new BufferedInputStream(Files.newInputStream(LARGE_IMAGE_FILE.toPath())));
}

@Test(groups = "standalone")
public void testDirectFileWithStreamBodyGenerator() throws Throwable {
doTestWithInputStreamBodyGenerator(new FileInputStream(LARGE_IMAGE_FILE));
doTestWithInputStreamBodyGenerator(Files.newInputStream(LARGE_IMAGE_FILE.toPath()));
}

@Test(groups = "standalone")
public void testDirectFileWithFeedableBodyGenerator() throws Throwable {
doTestWithFeedableBodyGenerator(new FileInputStream(LARGE_IMAGE_FILE));
doTestWithFeedableBodyGenerator(Files.newInputStream(LARGE_IMAGE_FILE.toPath()));
}

public void doTestWithInputStreamBodyGenerator(InputStream is) throws Throwable {
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {

RequestBuilder builder = post(getTargetUrl()).setBody(new InputStreamBodyGenerator(is));

Request r = builder.build();

final ListenableFuture<Response> responseFuture = c.executeRequest(r);
waitForAndAssertResponse(responseFuture);
try {
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
ListenableFuture<Response> responseFuture = c.executeRequest(post(getTargetUrl()).setBody(new InputStreamBodyGenerator(is)));
waitForAndAssertResponse(responseFuture);
}
} finally {
is.close();
}
}

public void doTestWithFeedableBodyGenerator(InputStream is) throws Throwable {
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {

final FeedableBodyGenerator feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build();

ListenableFuture<Response> responseFuture = c.executeRequest(r);

feed(feedableBodyGenerator, is);

waitForAndAssertResponse(responseFuture);
try {
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
final FeedableBodyGenerator feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build();
ListenableFuture<Response> responseFuture = c.executeRequest(r);
feed(feedableBodyGenerator, is);
waitForAndAssertResponse(responseFuture);
}
} finally {
is.close();
}
}

Expand Down
Expand Up @@ -19,13 +19,13 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -215,7 +215,7 @@ private void testSentFile(List<String> expectedContents, List<File> sourceFiles,

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

byte[] bytes;
try (FileInputStream instream = new FileInputStream(tmp)) {
try (InputStream instream = Files.newInputStream(tmp.toPath())) {
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
byte[] buf = new byte[8092];
int len = 0;
Expand All @@ -253,7 +253,7 @@ private void testSentFile(List<String> expectedContents, List<File> sourceFiles,
String helloString = new String(bytes);
assertEquals(helloString, expectedContents.get(i));
} else {
try (FileInputStream instream = new FileInputStream(tmp)) {
try (InputStream instream = Files.newInputStream(tmp.toPath())) {
ByteArrayOutputStream baos3 = new ByteArrayOutputStream();
GZIPInputStream deflater = new GZIPInputStream(instream);
try {
Expand Down

0 comments on commit 2c7a9e2

Please sign in to comment.