Repository: docker-java/docker-java
Pull request #284 (2015-07-28 19:13:23)

Added GZIP compression for build context creation


Patch:
diff --git a/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java b/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java
index 6a1a5480..0a93af3d 100644
--- a/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java
+++ b/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java
@@ -2,9 +2,11 @@ package com.github.dockerjava.core;
 
 import static com.github.dockerjava.core.FilePathUtil.relativize;
 
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.zip.GZIPOutputStream;
 
 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
@@ -16,8 +18,8 @@ public class CompressArchiveUtil {
             throws IOException {
         File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar");
         tarFile.deleteOnExit();
-        TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
-        try {
+        try(TarArchiveOutputStream tos = new TarArchiveOutputStream(new GZIPOutputStream(
+                new BufferedOutputStream(new FileOutputStream(tarFile))))) {
             tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
             for (File file : files) {
                 TarArchiveEntry tarEntry = new TarArchiveEntry(file);
@@ -36,8 +38,6 @@ public class CompressArchiveUtil {
                 }
                 tos.closeArchiveEntry();
             }
-        } finally {
-            tos.close();
         }
 
         return tarFile;
diff --git a/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java b/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java
index d94ce195..857ca917 100644
--- a/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java
+++ b/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java
@@ -63,10 +63,10 @@ public abstract class ResultCallbackTemplate<RC_T extends ResultCallback<A_RES_T
 
     @Override
     public void close() throws IOException {
+        closed = true;
         if (stream != null)
             stream.close();
         completed.countDown();
-        closed = true;
     }
 
     /**


Test patch:
diff --git a/src/test/java/com/github/dockerjava/core/CompressArchiveUtilTest.java b/src/test/java/com/github/dockerjava/core/CompressArchiveUtilTest.java
index 827f8d3b..90ca91fc 100644
--- a/src/test/java/com/github/dockerjava/core/CompressArchiveUtilTest.java
+++ b/src/test/java/com/github/dockerjava/core/CompressArchiveUtilTest.java
@@ -5,10 +5,12 @@ import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.zip.GZIPInputStream;
 
 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
@@ -43,7 +45,8 @@ public class CompressArchiveUtilTest {
         expectedFile.delete();
         assertThat(expectedFile.exists(), is(false));
 
-        TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new FileInputStream(archive));
+        TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GZIPInputStream(
+                new BufferedInputStream(new FileInputStream(archive))));
         TarArchiveEntry entry;
         while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
             String individualFiles = entry.getName();
diff --git a/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java b/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java
index 6d2b4c80..5385287e 100644
--- a/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java
+++ b/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java
@@ -225,4 +225,17 @@ public class BuildImageCmdImplTest extends AbstractDockerClientTest {
         String response = dockerfileBuild(baseDir);
         assertThat(response, containsString("testENVSubstitution successfully completed"));
     }
+    
+    @Test
+    public void testBuilderPerformance() throws Exception {
+        File baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("nginx").getFile());
+
+        String imageId = buildImage(baseDir);
+
+        InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
+        assertThat(inspectImageResponse, not(nullValue()));
+        LOG.info("Image Inspect: {}", inspectImageResponse.toString());
+
+        assertThat(inspectImageResponse.getAuthor(), equalTo("Guillaume J. Charmes \"guillaume@dotcloud.com\""));
+    }
 }