You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2018/01/14 11:26:37 UTC

commons-compress git commit: COMPRESS-441 simplify code for zlib dummy byte workarounds

Repository: commons-compress
Updated Branches:
  refs/heads/master c66db899c -> 43e8d3d5f


COMPRESS-441 simplify code for zlib dummy byte workarounds


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/43e8d3d5
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/43e8d3d5
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/43e8d3d5

Branch: refs/heads/master
Commit: 43e8d3d5f13209ee0c0bcf4b5a7d785fcae796da
Parents: c66db89
Author: Stefan Bodewig <bo...@apache.org>
Authored: Sun Jan 14 12:24:20 2018 +0100
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Sun Jan 14 12:25:42 2018 +0100

----------------------------------------------------------------------
 src/changes/changes.xml                         |  4 ++
 .../compress/archivers/sevenz/Coders.java       | 43 ++++----------------
 .../commons/compress/archivers/zip/ZipFile.java | 23 ++++-------
 3 files changed, 18 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/43e8d3d5/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 46c9b56..c36ba45 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -101,6 +101,10 @@ The <action> type attribute can be add,update,fix,remove.
         Added a few extra sanity checks for the rarer compression
         methods used in ZIP archives.
       </action>
+      <action issue="COMPRESS-441" type="update" date="2018-01-14">
+        Simplified the special handling for the dummy byte required by
+        zlib when using java.util.zip.Inflater.
+      </action>
     </release>
     <release version="1.15" date="2017-10-17"
              description="Release 1.15

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/43e8d3d5/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
index 065562d..9bd6b28 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
@@ -17,10 +17,12 @@
  */
 package org.apache.commons.compress.archivers.sevenz;
 
+import java.io.ByteArrayInputStream;
 import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.SequenceInputStream;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
@@ -126,6 +128,7 @@ class Coders {
     }
 
     static class DeflateDecoder extends CoderBase {
+        private static final byte[] ONE_ZERO_BYTE = new byte[1];
         DeflateDecoder() {
             super(Number.class);
         }
@@ -136,8 +139,10 @@ class Coders {
                 final Coder coder, final byte[] password)
             throws IOException {
             final Inflater inflater = new Inflater(true);
-            final InflaterInputStream inflaterInputStream = new InflaterInputStream(new DummyByteAddingInputStream(in),
-                    inflater);
+            // Inflater requires an extra dummy byte, see
+            // https://docs.oracle.com/javase/7/docs/api/java/util/zip/Inflater.html#Inflater(boolean)
+            final InflaterInputStream inflaterInputStream = new InflaterInputStream(new SequenceInputStream(in,
+                new ByteArrayInputStream(ONE_ZERO_BYTE)), inflater);
             return new DeflateDecoderInputStream(inflaterInputStream, inflater);
         }
         @Override
@@ -254,38 +259,4 @@ class Coders {
         }
     }
 
-    /**
-     * ZLIB requires an extra dummy byte.
-     *
-     * @see java.util.zip.Inflater#Inflater(boolean)
-     * @see org.apache.commons.compress.archivers.zip.ZipFile.BoundedInputStream
-     */
-    private static class DummyByteAddingInputStream extends FilterInputStream {
-        private boolean addDummyByte = true;
-
-        private DummyByteAddingInputStream(final InputStream in) {
-            super(in);
-        }
-
-        @Override
-        public int read() throws IOException {
-            int result = super.read();
-            if (result == -1 && addDummyByte) {
-                addDummyByte = false;
-                result = 0;
-            }
-            return result;
-        }
-
-        @Override
-        public int read(final byte[] b, final int off, final int len) throws IOException {
-            final int result = super.read(b, off, len);
-            if (result == -1 && addDummyByte) {
-                addDummyByte = false;
-                b[off] = 0;
-                return 1;
-            }
-            return result;
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/43e8d3d5/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
index f9fe41b..1627301 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
@@ -18,11 +18,13 @@
 package org.apache.commons.compress.archivers.zip;
 
 import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
 import java.io.Closeable;
 import java.io.EOFException;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.SequenceInputStream;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.channels.SeekableByteChannel;
@@ -90,6 +92,7 @@ public class ZipFile implements Closeable {
     private static final int POS_1 = 1;
     private static final int POS_2 = 2;
     private static final int POS_3 = 3;
+    private static final byte[] ONE_ZERO_BYTE = new byte[1];
 
     /**
      * List of entries in the order they appear inside the central
@@ -493,9 +496,11 @@ public class ZipFile implements Closeable {
                 return new ExplodingInputStream(ze.getGeneralPurposeBit().getSlidingDictionarySize(),
                         ze.getGeneralPurposeBit().getNumberOfShannonFanoTrees(), buf);
             case DEFLATED:
-                bis.addDummy();
                 final Inflater inflater = new Inflater(true);
-                return new InflaterInputStream(buf, inflater) {
+                // Inflater requires an extra dummy byte, see
+                // https://docs.oracle.com/javase/7/docs/api/java/util/zip/Inflater.html#Inflater(boolean)
+                return new InflaterInputStream(new SequenceInputStream(buf, new ByteArrayInputStream(ONE_ZERO_BYTE)),
+                    inflater) {
                     @Override
                     public void close() throws IOException {
                         try {
@@ -1100,7 +1105,6 @@ public class ZipFile implements Closeable {
         private ByteBuffer singleByteBuffer;
         private final long end;
         private long loc;
-        private boolean addDummy = false;
 
         BoundedInputStream(final long start, final long remaining) {
             this.end = start+remaining;
@@ -1114,10 +1118,6 @@ public class ZipFile implements Closeable {
         @Override
         public synchronized int read() throws IOException {
             if (loc >= end) {
-                if (loc == end && addDummy) {
-                    addDummy = false;
-                    return 0;
-                }
                 return -1;
             }
             if (singleByteBuffer == null) {
@@ -1142,11 +1142,6 @@ public class ZipFile implements Closeable {
 
             if (len > end-loc) {
                 if (loc >= end) {
-                    if (loc == end && addDummy) {
-                        addDummy = false;
-                        b[off] = 0;
-                        return 1;
-                    }
                     return -1;
                 }
                 len = (int)(end-loc);
@@ -1171,10 +1166,6 @@ public class ZipFile implements Closeable {
             buf.flip();
             return read;
         }
-
-        synchronized void addDummy() {
-            this.addDummy = true;
-        }
     }
 
     /**