commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [3/5] commons-compress git commit: BZip2CompressorInputStream: Use BitInputStream.
Date Sat, 04 Feb 2017 19:21:19 GMT
BZip2CompressorInputStream: Use BitInputStream.


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

Branch: refs/heads/PR13
Commit: 96e34c6530bd20f4982d9351138a4cfa623fac9e
Parents: 3af95ce
Author: Thomas Meyer <thomas@m3y3r.de>
Authored: Sun Jan 15 17:50:13 2017 +0100
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Sat Feb 4 18:56:59 2017 +0100

----------------------------------------------------------------------
 .../bzip2/BZip2CompressorInputStream.java       | 169 +++++--------------
 1 file changed, 45 insertions(+), 124 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/96e34c65/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
index 1fc8146..c1ed68f 100644
--- a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
@@ -26,8 +26,11 @@ package org.apache.commons.compress.compressors.bzip2;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.ByteOrder;
+import java.util.Arrays;
 
 import org.apache.commons.compress.compressors.CompressorInputStream;
+import org.apache.commons.compress.utils.BitInputStream;
 
 /**
  * An input stream that decompresses from the BZip2 format to be read as any other stream.
@@ -55,13 +58,11 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
 
     private boolean blockRandomised;
 
-    private int bsBuff;
-    private int bsLive;
     private final CRC crc = new CRC();
 
     private int nInUse;
 
-    private InputStream in;
+    private BitInputStream bin;
     private final boolean decompressConcatenated;
 
     private static final int EOF = 0;
@@ -125,7 +126,7 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
      *             if {@code in == null}, the stream content is malformed, or an I/O error
occurs.
      */
     public BZip2CompressorInputStream(final InputStream in, final boolean decompressConcatenated)
throws IOException {
-        this.in = in;
+        this.bin = new BitInputStream(in, ByteOrder.BIG_ENDIAN);
         this.decompressConcatenated = decompressConcatenated;
 
         init(true);
@@ -134,7 +135,7 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
 
     @Override
     public int read() throws IOException {
-        if (this.in != null) {
+        if (this.bin != null) {
             final int r = read0();
             count(r < 0 ? -1 : 1);
             return r;
@@ -160,7 +161,7 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
             throw new IndexOutOfBoundsException("offs(" + offs + ") + len("
                                                 + len + ") > dest.length(" + dest.length
+ ").");
         }
-        if (this.in == null) {
+        if (this.bin == null) {
             throw new IOException("stream closed");
         }
         if (len == 0) {
@@ -225,17 +226,26 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
         }
     }
 
+    private int readNextByte(BitInputStream in) throws IOException {
+        long b = in.readBits(8);
+        return (int) b;
+    }
+
     private boolean init(final boolean isFirstStream) throws IOException {
-        if (null == in) {
+        if (null == bin) {
             throw new IOException("No InputStream");
         }
 
-        final int magic0 = this.in.read();
+        if(!isFirstStream) {
+            bin.clearBitCache();
+        }
+
+        final int magic0 = readNextByte(this.bin);
         if (magic0 == -1 && !isFirstStream) {
             return false;
         }
-        final int magic1 = this.in.read();
-        final int magic2 = this.in.read();
+        final int magic1 = readNextByte(this.bin);
+        final int magic2 = readNextByte(this.bin);
 
         if (magic0 != 'B' || magic1 != 'Z' || magic2 != 'h') {
             throw new IOException(isFirstStream
@@ -243,14 +253,13 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
                     : "Garbage after a valid BZip2 stream");
         }
 
-        final int blockSize = this.in.read();
+        final int blockSize = readNextByte(this.bin);
         if ((blockSize < '1') || (blockSize > '9')) {
             throw new IOException("BZip2 block size is invalid");
         }
 
         this.blockSize100k = blockSize - '0';
 
-        this.bsLive = 0;
         this.computedCombinedCRC = 0;
 
         return true;
@@ -350,41 +359,31 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
 
     @Override
     public void close() throws IOException {
-        final InputStream inShadow = this.in;
+        final BitInputStream inShadow = this.bin;
         if (inShadow != null) {
             try {
-                if (inShadow != System.in) {
-                    inShadow.close();
-                }
+//                if (inShadow != System.in) {
+//                    inShadow.close();
+//                }
             } finally {
                 this.data = null;
-                this.in = null;
+                this.bin = null;
             }
         }
     }
 
+    /**
+     * read bits from the input stream
+     * @param n the number of bits to read, must not exceed 32?
+     * @return
+     * @throws IOException
+     */
     private int bsR(final int n) throws IOException {
-        int bsLiveShadow = this.bsLive;
-        int bsBuffShadow = this.bsBuff;
-
-        if (bsLiveShadow < n) {
-            final InputStream inShadow = this.in;
-            do {
-                final int thech = inShadow.read();
-
-                if (thech < 0) {
-                    throw new IOException("unexpected end of stream");
-                }
-
-                bsBuffShadow = (bsBuffShadow << 8) | thech;
-                bsLiveShadow += 8;
-            } while (bsLiveShadow < n);
-
-            this.bsBuff = bsBuffShadow;
+        long thech = bin.readBits(n);
+        if (thech < 0) {
+            throw new IOException("unexpected end of stream");
         }
-
-        this.bsLive = bsLiveShadow - n;
-        return (bsBuffShadow >> (bsLiveShadow - n)) & ((1 << n) - 1);
+        return (int) thech;
     }
 
     private boolean bsGetBit() throws IOException {
@@ -396,7 +395,7 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
     }
 
     private int bsGetInt() throws IOException {
-        return (((((bsR(8) << 8) | bsR(8)) << 8) | bsR(8)) << 8) | bsR(8);
+        return (int) bsR(32);
     }
 
     /**
@@ -456,10 +455,7 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
             }
         }
 
-        for (int i = 256; --i >= 0;) {
-            inUse[i] = false;
-        }
-
+        Arrays.fill(inUse, false);
         for (int i = 0; i < 16; i++) {
             if ((inUse16 & (1 << i)) != 0) {
                 final int i16 = i << 4;
@@ -556,7 +552,6 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
         this.origPtr = bsR(24);
         recvDecodingTables();
 
-        final InputStream inShadow = this.in;
         final Data dataShadow = this.data;
         final byte[] ll8 = dataShadow.ll8;
         final int[] unzftab = dataShadow.unzftab;
@@ -583,8 +578,6 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
         int groupPos = G_SIZE - 1;
         final int eob = this.nInUse + 1;
         int nextSym = getAndMoveToFrontDecode0(0);
-        int bsBuffShadow = this.bsBuff;
-        int bsLiveShadow = this.bsLive;
         int lastShadow = -1;
         int zt = selector[groupNo] & 0xff;
         int[] base_zt = base[zt];
@@ -617,37 +610,10 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
                     }
 
                     int zn = minLens_zt;
-
-                    // Inlined:
-                    // int zvec = bsR(zn);
-                    while (bsLiveShadow < zn) {
-                        final int thech = inShadow.read();
-                        if (thech >= 0) {
-                            bsBuffShadow = (bsBuffShadow << 8) | thech;
-                            bsLiveShadow += 8;
-                            continue;
-                        }
-                        throw new IOException("unexpected end of stream");
-                    }
-                    int zvec = (bsBuffShadow >> (bsLiveShadow - zn))
-                        & ((1 << zn) - 1);
-                    bsLiveShadow -= zn;
-
-                    while (zvec > limit_zt[zn]) {
+                    int zvec = (int) bsR(zn);
+                    while(zvec > limit_zt[zn]) {
                         zn++;
-                        while (bsLiveShadow < 1) {
-                            final int thech = inShadow.read();
-                            if (thech >= 0) {
-                                bsBuffShadow = (bsBuffShadow << 8) | thech;
-                                bsLiveShadow += 8;
-                                continue;
-                            }
-                            throw new IOException(
-                                                  "unexpected end of stream");
-                        }
-                        bsLiveShadow--;
-                        zvec = (zvec << 1)
-                            | ((bsBuffShadow >> bsLiveShadow) & 1);
+                        zvec = (zvec << 1) | bsR(1);
                     }
                     nextSym = perm_zt[zvec - base_zt[zn]];
                 }
@@ -698,74 +664,29 @@ public class BZip2CompressorInputStream extends CompressorInputStream
implements
                 }
 
                 int zn = minLens_zt;
-
-                // Inlined:
-                // int zvec = bsR(zn);
-                while (bsLiveShadow < zn) {
-                    final int thech = inShadow.read();
-                    if (thech >= 0) {
-                        bsBuffShadow = (bsBuffShadow << 8) | thech;
-                        bsLiveShadow += 8;
-                        continue;
-                    }
-                    throw new IOException("unexpected end of stream");
-                }
-                int zvec = (bsBuffShadow >> (bsLiveShadow - zn))
-                    & ((1 << zn) - 1);
-                bsLiveShadow -= zn;
-
-                while (zvec > limit_zt[zn]) {
+                int zvec = (int) bsR(zn);
+                while(zvec > limit_zt[zn]) {
                     zn++;
-                    while (bsLiveShadow < 1) {
-                        final int thech = inShadow.read();
-                        if (thech >= 0) {
-                            bsBuffShadow = (bsBuffShadow << 8) | thech;
-                            bsLiveShadow += 8;
-                            continue;
-                        }
-                        throw new IOException("unexpected end of stream");
-                    }
-                    bsLiveShadow--;
-                    zvec = (zvec << 1) | ((bsBuffShadow >> bsLiveShadow) &
1);
+                    zvec = (zvec << 1) | (int) bsR(1);
                 }
                 nextSym = perm_zt[zvec - base_zt[zn]];
             }
         }
 
         this.last = lastShadow;
-        this.bsLive = bsLiveShadow;
-        this.bsBuff = bsBuffShadow;
     }
 
     private int getAndMoveToFrontDecode0(final int groupNo) throws IOException {
-        final InputStream inShadow = this.in;
         final Data dataShadow = this.data;
         final int zt = dataShadow.selector[groupNo] & 0xff;
         final int[] limit_zt = dataShadow.limit[zt];
         int zn = dataShadow.minLens[zt];
         int zvec = bsR(zn);
-        int bsLiveShadow = this.bsLive;
-        int bsBuffShadow = this.bsBuff;
-
         while (zvec > limit_zt[zn]) {
             zn++;
-            while (bsLiveShadow < 1) {
-                final int thech = inShadow.read();
-
-                if (thech >= 0) {
-                    bsBuffShadow = (bsBuffShadow << 8) | thech;
-                    bsLiveShadow += 8;
-                    continue;
-                }
-                throw new IOException("unexpected end of stream");
-            }
-            bsLiveShadow--;
-            zvec = (zvec << 1) | ((bsBuffShadow >> bsLiveShadow) & 1);
+            zvec = (zvec << 1) | (int) bsR(1);
         }
 
-        this.bsLive = bsLiveShadow;
-        this.bsBuff = bsBuffShadow;
-
         return dataShadow.perm[zt][zvec - dataShadow.base[zt][zn]];
     }
 


Mime
View raw message