Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id F404F200C11 for ; Sat, 4 Feb 2017 20:21:19 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id F2BB6160B63; Sat, 4 Feb 2017 19:21:19 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id F3237160B66 for ; Sat, 4 Feb 2017 20:21:18 +0100 (CET) Received: (qmail 8282 invoked by uid 500); 4 Feb 2017 19:21:18 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 7703 invoked by uid 99); 4 Feb 2017 19:21:17 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 04 Feb 2017 19:21:17 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A76F1E0165; Sat, 4 Feb 2017 19:21:17 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: bodewig@apache.org To: commits@commons.apache.org Date: Sat, 04 Feb 2017 19:21:19 -0000 Message-Id: <0a5ca89a29004275992d47665c8a5253@git.apache.org> In-Reply-To: <1f6f55427d7d4089893abf40fbe0bd2a@git.apache.org> References: <1f6f55427d7d4089893abf40fbe0bd2a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [3/5] commons-compress git commit: BZip2CompressorInputStream: Use BitInputStream. archived-at: Sat, 04 Feb 2017 19:21:20 -0000 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 Authored: Sun Jan 15 17:50:13 2017 +0100 Committer: Stefan Bodewig 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]]; }