commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1571253 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/sevenz/ test/java/org/apache/commons/compress/archivers/sevenz/
Date Mon, 24 Feb 2014 11:34:41 GMT
Author: bodewig
Date: Mon Feb 24 11:34:41 2014
New Revision: 1571253

URL: http://svn.apache.org/r1571253
Log:
allow some numeric values to be configured easily

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java?rev=1571253&r1=1571252&r2=1571253&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
Mon Feb 24 11:34:41 2014
@@ -122,6 +122,14 @@ class Coders {
             throws IOException {
             throw new UnsupportedOperationException("method doesn't support writing");
         }
+
+        /**
+         * If the option represents a number, return its integer
+         * value, otherwise return the given default value.
+         */
+        protected int numberOptionOrDefault(Object options, int defaultValue) {
+            return options instanceof Number ? ((Number) options).intValue() : defaultValue;
+        }
     }
     
     static class CopyDecoder extends CoderBase {
@@ -153,6 +161,10 @@ class Coders {
     }
     
     static class DeflateDecoder extends CoderBase {
+        DeflateDecoder() {
+            super(Number.class);
+        }
+
         @Override
         InputStream decode(final InputStream in, final Coder coder, final byte[] password)
             throws IOException {
@@ -161,20 +173,26 @@ class Coders {
         }
         @Override
         OutputStream encode(final OutputStream out, final Object options) {
-            return new DeflaterOutputStream(out, new Deflater(9, true));
+            int level = numberOptionOrDefault(options, 9);
+            return new DeflaterOutputStream(out, new Deflater(level, true));
         }
     }
 
     static class BZIP2Decoder extends CoderBase {
+        BZIP2Decoder() {
+            super(Number.class);
+        }
+
         @Override
         InputStream decode(final InputStream in, final Coder coder, final byte[] password)
                 throws IOException {
             return new BZip2CompressorInputStream(in);
         }
         @Override
-        OutputStream encode(final OutputStream out, final Object _)
+        OutputStream encode(final OutputStream out, final Object options)
                 throws IOException {
-            return new BZip2CompressorOutputStream(out);
+            int blockSize = numberOptionOrDefault(options, BZip2CompressorOutputStream.MAX_BLOCKSIZE);
+            return new BZip2CompressorOutputStream(out, blockSize);
         }
     }
 

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java?rev=1571253&r1=1571252&r2=1571253&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
Mon Feb 24 11:34:41 2014
@@ -27,7 +27,7 @@ import org.tukaani.xz.LZMA2Options;
 
 class LZMA2Decoder extends Coders.CoderBase {
     LZMA2Decoder() {
-        super(LZMA2Options.class);
+        super(LZMA2Options.class, Number.class);
     }
 
     @Override
@@ -71,7 +71,7 @@ class LZMA2Decoder extends Coders.CoderB
         if (opts instanceof LZMA2Options) {
             return ((LZMA2Options) opts).getDictSize();
         }
-        return LZMA2Options.DICT_SIZE_DEFAULT;
+        return numberOptionOrDefault(opts);
     }
 
     private LZMA2Options getOptions(Object opts) throws IOException {
@@ -79,7 +79,11 @@ class LZMA2Decoder extends Coders.CoderB
             return (LZMA2Options) opts;
         }
         LZMA2Options options = new LZMA2Options();
-        options.setDictSize(LZMA2Options.DICT_SIZE_DEFAULT);
+        options.setDictSize(numberOptionOrDefault(opts));
         return options;
     }
+
+    private int numberOptionOrDefault(Object opts) {
+        return numberOptionOrDefault(opts, LZMA2Options.DICT_SIZE_DEFAULT);
+    }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java?rev=1571253&r1=1571252&r2=1571253&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java
Mon Feb 24 11:34:41 2014
@@ -36,6 +36,24 @@ public class SevenZMethodConfigurationTe
                              .getOptions());
     }
 
+    @Test
+    public void shouldAllowNumberForLZMA2() {
+        Assert.assertNotNull(new SevenZMethodConfiguration(SevenZMethod.LZMA2, 42)
+                             .getOptions());
+    }
+
+    @Test
+    public void shouldAllowNumberForBzip2() {
+        Assert.assertNotNull(new SevenZMethodConfiguration(SevenZMethod.BZIP2, 42)
+                             .getOptions());
+    }
+
+    @Test
+    public void shouldAllowNumberForDeflate() {
+        Assert.assertNotNull(new SevenZMethodConfiguration(SevenZMethod.DEFLATE, 42)
+                             .getOptions());
+    }
+
     @Test(expected = IllegalArgumentException.class)
     public void shouldNotAllowStringOptionsForLZMA2() {
         new SevenZMethodConfiguration(SevenZMethod.LZMA2, "");



Mime
View raw message