Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #865 from timkoon/issue-848
Browse files Browse the repository at this point in the history
Close response InputStreams
  • Loading branch information
lognaturel authored Nov 2, 2020
2 parents 97b57f4 + 62f552b commit dd75f13
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/org/opendatakit/briefcase/reused/UncheckedFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ public static InputStream newInputStream(Path path, OpenOption... options) {
}
}

public static void closeInputStream(InputStream inputStream) {
try {
inputStream.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static long size(Path path) {
try {
return Files.size(path);
Expand Down
10 changes: 10 additions & 0 deletions src/org/opendatakit/briefcase/reused/http/CommonsHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.opendatakit.briefcase.reused.BriefcaseException;
import org.opendatakit.briefcase.reused.UncheckedFiles;
import org.opendatakit.briefcase.reused.http.response.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommonsHttp implements Http {
private Executor executor;
private final int maxConnections;
private final BasicCookieStore cookieStore;
private static final Logger log = LoggerFactory.getLogger(CommonsHttp.class);

private CommonsHttp(Executor executor, int maxConnections, BasicCookieStore cookieStore) {
this.executor = executor;
Expand Down Expand Up @@ -147,6 +151,12 @@ private <T> Response<T> uncheckedExecute(Request<T> request, Executor executor)
throw new HttpException("The connection has timed out", e);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
request.ifBody(UncheckedFiles::closeInputStream);
if (request.multipartMessages != null)
request.multipartMessages.stream()
.map(MultipartMessage::getBody)
.forEach(UncheckedFiles::closeInputStream);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/org/opendatakit/briefcase/reused/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import org.opendatakit.briefcase.reused.BriefcaseException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -124,6 +125,10 @@ public InputStream getBody() {
return body.orElseThrow(BriefcaseException::new);
}

public void ifBody(Consumer<InputStream> consumer) {
body.ifPresent(consumer);
}

public boolean ignoreCookies() {
return ignoreCookies;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.opendatakit.briefcase.reused;

import static org.opendatakit.briefcase.reused.UncheckedFiles.createFile;
import static org.opendatakit.briefcase.reused.UncheckedFiles.createTempDirectory;
import static org.opendatakit.briefcase.reused.UncheckedFiles.deleteRecursive;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Path;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class UncheckedFilesInputStreamTest {
private Path tempDir;
private Path temp;


@Before
public void setUp() {
tempDir = createTempDirectory("briefcase");
temp = tempDir.resolve("test.txt");
createFile(temp);
}

@After
public void tearDown() {
deleteRecursive(tempDir);
}

@Test(expected = IOException.class)
public void closeInputStream_should_close() throws IOException {
InputStream inputStream = UncheckedFiles.newInputStream(temp);

UncheckedFiles.closeInputStream(inputStream);

// after close, .available() should throw exception
inputStream.available();
}

@Test(expected = UncheckedIOException.class)
public void closeInputStream_should_throw_exception() throws IOException {
InputStream is = new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
@Override
public void close() throws IOException {
throw new IOException("chuchu");
}
};
UncheckedFiles.closeInputStream(is);
}
}

0 comments on commit dd75f13

Please sign in to comment.