Skip to content

Commit 90f4aba

Browse files
committed
Use java 7 to close stream
1 parent 89bb406 commit 90f4aba

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/main/java/com/github/dockerjava/jaxrs/CopyFileToContainerCmdExec.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.github.dockerjava.core.CompressArchiveUtil;
1818
import com.github.dockerjava.core.DockerClientConfig;
1919
import com.github.dockerjava.core.command.CopyFileToContainerCmd;
20-
import com.google.common.io.Closeables;
2120

2221
public class CopyFileToContainerCmdExec extends AbstrSyncDockerCmdExec<CopyFileToContainerCmd, Void> implements CopyFileToContainerCmd.Exec {
2322

@@ -27,17 +26,19 @@ public CopyFileToContainerCmdExec(WebTarget baseResource, DockerClientConfig doc
2726
super(baseResource, dockerClientConfig);
2827
}
2928

29+
private InputStream buildUploadStream(CopyFileToContainerCmd command) throws IOException {
30+
Path toUpload = Files.createTempFile("docker-java", ".tar.gz");
31+
CompressArchiveUtil.tar(Paths.get(command.getHostResource()), toUpload, true, command.isDirChildrenOnly());
32+
return Files.newInputStream(toUpload);
33+
}
34+
3035
@Override
3136
protected Void execute(CopyFileToContainerCmd command) {
3237
WebTarget webResource = getBaseResource().path("/containers/{id}/archive").resolveTemplate("id",
3338
command.getContainerId());
3439

3540
LOGGER.trace("PUT: " + webResource.toString());
36-
InputStream streamToUpload = null;
37-
try {
38-
Path toUpload = Files.createTempFile("docker-java", "tar.gz");
39-
CompressArchiveUtil.tar(Paths.get(command.getHostResource()), toUpload, true, command.isDirChildrenOnly());
40-
streamToUpload = Files.newInputStream(toUpload);
41+
try (InputStream streamToUpload = buildUploadStream(command)) {
4142
webResource
4243
.queryParam("path", command.getRemotePath())
4344
.queryParam("noOverwriteDirNonDir", command.isNoOverwriteDirNonDir())
@@ -47,11 +48,6 @@ protected Void execute(CopyFileToContainerCmd command) {
4748
return null;
4849
} catch (IOException e) {
4950
throw new DockerClientException("Error occurred while preparing uploading host resource <" + command.getHostResource() + ">", e);
50-
} finally {
51-
if (streamToUpload != null) {
52-
Closeables.closeQuietly(streamToUpload);
53-
}
5451
}
5552
}
56-
5753
}

src/test/java/com/github/dockerjava/core/command/CopyFileToContainerCmdImplTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.github.dockerjava.api.NotFoundException;
1818
import com.github.dockerjava.api.command.CreateContainerResponse;
1919
import com.github.dockerjava.client.AbstractDockerClientTest;
20-
import com.google.common.io.Closeables;
2120

2221
public class CopyFileToContainerCmdImplTest extends AbstractDockerClientTest {
2322
@BeforeTest
@@ -52,10 +51,10 @@ public void copyToContainer() throws Exception {
5251
dockerClient.startContainerCmd(container.getId()).exec();
5352

5453
dockerClient.copyFileToContainerCmd(container.getId(), "src/test/resources/testReadFile").exec();
55-
InputStream response = dockerClient.copyFileFromContainerCmd(container.getId(), "testReadFile").exec();
56-
boolean bytesAvailable = response.available() > 0;
57-
assertTrue(bytesAvailable, "The file was not copied to the container.");
58-
Closeables.closeQuietly(response);
54+
try (InputStream response = dockerClient.copyFileFromContainerCmd(container.getId(), "testReadFile").exec()) {
55+
boolean bytesAvailable = response.available() > 0;
56+
assertTrue(bytesAvailable, "The file was not copied to the container.");
57+
}
5958
}
6059

6160
@Test

0 commit comments

Comments
 (0)