commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1546784 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/compressors/ test/java/org/apache/commons/compress/ test/java/org/apache/commons/compress/compressors/
Date Sun, 01 Dec 2013 07:03:57 GMT
Author: bodewig
Date: Sun Dec  1 07:03:57 2013
New Revision: 1546784

URL: http://svn.apache.org/r1546784
Log:
COMPRESS-147 and COMPRESS-243 - add .Z and Snappy to Factory

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java?rev=1546784&r1=1546783&r2=1546784&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
Sun Dec  1 07:03:57 2013
@@ -32,6 +32,9 @@ import org.apache.commons.compress.compr
 import org.apache.commons.compress.compressors.xz.XZUtils;
 import org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream;
 import org.apache.commons.compress.compressors.pack200.Pack200CompressorOutputStream;
+import org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorInputStream;
+import org.apache.commons.compress.compressors.snappy.SnappyCompressorInputStream;
+import org.apache.commons.compress.compressors.z.ZCompressorInputStream;
 import org.apache.commons.compress.utils.IOUtils;
 
 /**
@@ -91,6 +94,24 @@ public class CompressorStreamFactory {
      */
     public static final String LZMA = "lzma";
 
+    /**
+     * Constant used to identify the "framed" Snappy compression method.
+     * @since 1.7
+     */
+    public static final String SNAPPY_FRAMED = "snappy-framed";
+
+    /**
+     * Constant used to identify the "raw" Snappy compression method.
+     * @since 1.7
+     */
+    public static final String SNAPPY_RAW = "snappy-raw";
+
+    /**
+     * Constant used to identify the traditional Unix compress method.
+     * @since 1.7
+     */
+    public static final String Z = "z";
+
     private boolean decompressConcatenated = false;
 
     /**
@@ -154,6 +175,10 @@ public class CompressorStreamFactory {
                 return new Pack200CompressorInputStream(in);
             }
 
+            if (FramedSnappyCompressorInputStream.matches(signature, signatureLength)) {
+                return new FramedSnappyCompressorInputStream(in);
+            }
+
         } catch (IOException e) {
             throw new CompressorException("Failed to detect Compressor from InputStream.",
e);
         }
@@ -164,7 +189,8 @@ public class CompressorStreamFactory {
     /**
      * Create a compressor input stream from a compressor name and an input stream.
      * 
-     * @param name of the compressor, i.e. "gz", "bzip2", "xz", "lzma", or "pack200"
+     * @param name of the compressor, i.e. "gz", "bzip2", "xz",
+     *        "lzma", "snappy-raw", "snappy-framed", "pack200", "z"
      * @param in the input stream
      * @return compressor input stream
      * @throws CompressorException if the compressor name is not known
@@ -199,6 +225,18 @@ public class CompressorStreamFactory {
                 return new Pack200CompressorInputStream(in);
             }
 
+            if (SNAPPY_RAW.equalsIgnoreCase(name)) {
+                return new SnappyCompressorInputStream(in);
+            }
+
+            if (SNAPPY_FRAMED.equalsIgnoreCase(name)) {
+                return new FramedSnappyCompressorInputStream(in);
+            }
+
+            if (Z.equalsIgnoreCase(name)) {
+                return new ZCompressorInputStream(in);
+            }
+
         } catch (IOException e) {
             throw new CompressorException(
                     "Could not create CompressorInputStream.", e);

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java?rev=1546784&r1=1546783&r2=1546784&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
Sun Dec  1 07:03:57 2013
@@ -414,4 +414,8 @@ public abstract class AbstractTestCase e
             }
         }
     }
+
+    protected static interface StreamWrapper<I extends InputStream> {
+        I wrap(InputStream in) throws Exception;
+    }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java?rev=1546784&r1=1546783&r2=1546784&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
Sun Dec  1 07:03:57 2013
@@ -18,10 +18,12 @@
  */
 package org.apache.commons.compress.compressors;
 
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Arrays;
 
 import org.apache.commons.compress.AbstractTestCase;
@@ -46,13 +48,40 @@ public final class FramedSnappyTestCase
         assertTrue(FramedSnappyCompressorInputStream.matches(b, 12));
     }
 
-    public void testDefaultExtraction() throws IOException {
+    public void testDefaultExtraction() throws Exception {
+        testUnarchive(new StreamWrapper<CompressorInputStream>() {
+            public CompressorInputStream wrap(InputStream is) throws IOException {
+                return new FramedSnappyCompressorInputStream(is);
+            }
+        });
+    }
+
+    public void testDefaultExtractionViaFactory() throws Exception {
+        testUnarchive(new StreamWrapper<CompressorInputStream>() {
+            public CompressorInputStream wrap(InputStream is) throws Exception {
+                return new CompressorStreamFactory()
+                    .createCompressorInputStream(CompressorStreamFactory.SNAPPY_FRAMED,
+                                                 is);
+            }
+        });
+    }
+
+    public void testDefaultExtractionViaFactoryAutodetection() throws Exception {
+        testUnarchive(new StreamWrapper<CompressorInputStream>() {
+            public CompressorInputStream wrap(InputStream is) throws Exception {
+                return new CompressorStreamFactory().createCompressorInputStream(is);
+            }
+        });
+    }
+
+    private void testUnarchive(StreamWrapper<CompressorInputStream> wrapper) throws
Exception {
         final File input = getFile("bla.tar.sz");
         final File output = new File(dir, "bla.tar");
         final FileInputStream is = new FileInputStream(input);
         try {
-            final CompressorInputStream in =
-                new FramedSnappyCompressorInputStream(is);
+            // the intermediate BufferedInputStream is there for mark
+            // support in the autodetection test
+            final CompressorInputStream in = wrapper.wrap(new BufferedInputStream(is));
             FileOutputStream out = null;
             try {
                 out = new FileOutputStream(output);

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java?rev=1546784&r1=1546783&r2=1546784&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java
Sun Dec  1 07:03:57 2013
@@ -21,6 +21,7 @@ package org.apache.commons.compress.comp
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 
 import org.apache.commons.compress.AbstractTestCase;
@@ -30,11 +31,28 @@ import org.apache.commons.compress.utils
 public final class ZTestCase extends AbstractTestCase {
 
     public void testZUnarchive() throws Exception {
+        testUnarchive(new StreamWrapper<CompressorInputStream>() {
+            public CompressorInputStream wrap(InputStream is) throws IOException {
+                return new ZCompressorInputStream(is);
+            }
+        });
+    }
+
+    public void testZUnarchiveViaFactory() throws Exception {
+        testUnarchive(new StreamWrapper<CompressorInputStream>() {
+            public CompressorInputStream wrap(InputStream is) throws Exception {
+                return new CompressorStreamFactory()
+                    .createCompressorInputStream(CompressorStreamFactory.Z, is);
+            }
+        });
+    }
+
+    private void testUnarchive(StreamWrapper wrapper) throws Exception {
         final File input = getFile("bla.tar.Z");
         final File output = new File(dir, "bla.tar");
         final InputStream is = new FileInputStream(input);
         try {
-            final CompressorInputStream in = new ZCompressorInputStream(is);
+            final InputStream in = wrapper.wrap(is);
             FileOutputStream out = null;
             try {
                 out = new FileOutputStream(output);
@@ -49,4 +67,5 @@ public final class ZTestCase extends Abs
             is.close();
         }
     }
+
 }



Mime
View raw message