commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r756508 - /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
Date Fri, 20 Mar 2009 15:05:05 GMT
Author: bodewig
Date: Fri Mar 20 15:05:04 2009
New Revision: 756508

URL: http://svn.apache.org/viewvc?rev=756508&view=rev
Log:
throw an exception on CRC errors, submitted by Christian Grobmeier, SANDBOX-298

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java?rev=756508&r1=756507&r2=756508&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
Fri Mar 20 15:05:04 2009
@@ -33,42 +33,22 @@
  * An input stream that decompresses from the BZip2 format (without the file
  * header chars) to be read as any other stream.
  */
-public class BZip2CompressorInputStream extends CompressorInputStream implements BZip2Constants
{
-    private static void reportCRCError() throws IOException {
-        // The clean way would be to throw an exception.
-        //throw new IOException("crc error");
-
-        // Just print a message, like the previous versions of this class did
-        System.err.println("BZip2 CRC error");
-    }
-
-    private void makeMaps() {
-        final boolean[] inUse   = this.data.inUse;
-        final byte[] seqToUnseq = this.data.seqToUnseq;
-
-        int nInUseShadow = 0;
-
-        for (int i = 0; i < 256; i++) {
-            if (inUse[i])
-                seqToUnseq[nInUseShadow++] = (byte) i;
-        }
-
-        this.nInUse = nInUseShadow;
-    }
+public class BZip2CompressorInputStream extends CompressorInputStream implements
+                                                                          BZip2Constants
{
 
     /**
      * Index of the last char in the block, so the block size == last + 1.
      */
-    private int  last;
+    private int last;
 
     /**
      * Index in zptr[] of original string after sorting.
      */
-    private int  origPtr;
+    private int origPtr;
 
     /**
-     * always: in the range 0 .. 9.
-     * The current block size is 100000 * this number.
+     * always: in the range 0 .. 9. The current block size is 100000 * this
+     * number.
      */
     private int blockSize100k;
 
@@ -84,7 +64,7 @@
 
     private int currentChar = -1;
 
-    private static final int EOF                  = 0;
+    private static final int EOF = 0;
     private static final int START_BLOCK_STATE = 1;
     private static final int RAND_PART_A_STATE = 2;
     private static final int RAND_PART_B_STATE = 3;
@@ -111,25 +91,25 @@
     private char su_z;
 
     /**
-     * All memory intensive stuff.
-     * This field is initialized by initBlock().
+     * All memory intensive stuff. This field is initialized by initBlock().
      */
     private BZip2CompressorInputStream.Data data;
 
     /**
-     * Constructs a new CBZip2InputStream which decompresses bytes read from
-     * the specified stream.
-     *
-     * <p>Although BZip2 headers are marked with the magic
-     * <tt>"Bz"</tt> this constructor expects the next byte in the
-     * stream to be the first one after the magic.  Thus callers have
-     * to skip the first two bytes. Otherwise this constructor will
-     * throw an exception. </p>
-     *
+     * Constructs a new CBZip2InputStream which decompresses bytes read from the
+     * specified stream.
+     * 
+     * <p>
+     * Although BZip2 headers are marked with the magic <tt>"Bz"</tt> this
+     * constructor expects the next byte in the stream to be the first one after
+     * the magic. Thus callers have to skip the first two bytes. Otherwise this
+     * constructor will throw an exception.
+     * </p>
+     * 
      * @throws IOException
-     *  if the stream content is malformed or an I/O error occurs.
+     *             if the stream content is malformed or an I/O error occurs.
      * @throws NullPointerException
-     *  if <tt>in == null</tt>
+     *             if <tt>in == null</tt>
      */
     public BZip2CompressorInputStream(final InputStream in) throws IOException {
         super();
@@ -138,6 +118,11 @@
         init();
     }
 
+    /*
+     * (non-Javadoc)
+     * 
+     * @see java.io.InputStream#read()
+     */
     public int read() throws IOException {
         if (this.in != null) {
             return read0();
@@ -146,6 +131,11 @@
         }
     }
 
+    /*
+     * (non-Javadoc)
+     * 
+     * @see java.io.InputStream#read(byte[], int, int)
+     */
     public int read(final byte[] dest, final int offs, final int len)
         throws IOException {
         if (offs < 0) {
@@ -156,8 +146,7 @@
         }
         if (offs + len > dest.length) {
             throw new IndexOutOfBoundsException("offs(" + offs + ") + len("
-                                                + len + ") > dest.length("
-                                                + dest.length + ").");
+                                                + len + ") > dest.length(" + dest.length
+ ").");
         }
         if (this.in == null) {
             throw new IOException("stream closed");
@@ -172,6 +161,20 @@
         return (destOffs == offs) ? -1 : (destOffs - offs);
     }
 
+    private void makeMaps() {
+        final boolean[] inUse = this.data.inUse;
+        final byte[] seqToUnseq = this.data.seqToUnseq;
+
+        int nInUseShadow = 0;
+
+        for (int i = 0; i < 256; i++) {
+            if (inUse[i])
+                seqToUnseq[nInUseShadow++] = (byte) i;
+        }
+
+        this.nInUse = nInUseShadow;
+    }
+
     private int read0() throws IOException {
         final int retChar = this.currentChar;
 
@@ -239,9 +242,8 @@
         int magic = this.in.read();
         if (magic != expected) {
             throw new IOException("Stream is not BZip2 formatted: expected '"
-                                  + expected + "' as " + position
-                                  + " byte but got '" + (char) magic
-                                  + "'");
+                                  + expected + "' as " + position + " byte but got '"
+                                  + (char) magic + "'");
         }
     }
 
@@ -253,19 +255,15 @@
         char magic4 = bsGetUByte();
         char magic5 = bsGetUByte();
 
-        if (magic0 == 0x17 &&
-            magic1 == 0x72 &&
-            magic2 == 0x45 &&
-            magic3 == 0x38 &&
-            magic4 == 0x50 &&
-            magic5 == 0x90) {
+        if (magic0 == 0x17 && magic1 == 0x72 && magic2 == 0x45
+            && magic3 == 0x38 && magic4 == 0x50 && magic5 == 0x90)
{
             complete(); // end of file
         } else if (magic0 != 0x31 || // '1'
                    magic1 != 0x41 || // ')'
                    magic2 != 0x59 || // 'Y'
                    magic3 != 0x26 || // '&'
                    magic4 != 0x53 || // 'S'
-                   magic5 != 0x59   // 'Y'
+                   magic5 != 0x59 // 'Y'
                    ) {
             this.currentState = EOF;
             throw new IOException("bad block header");
@@ -274,8 +272,8 @@
             this.blockRandomised = bsR(1) == 1;
 
             /**
-             * Allocate data here instead in constructor, so we do not
-             * allocate it if the input file is empty.
+             * Allocate data here instead in constructor, so we do not allocate
+             * it if the input file is empty.
              */
             if (this.data == null) {
                 this.data = new Data(this.blockSize100k);
@@ -296,16 +294,14 @@
         if (this.storedBlockCRC != this.computedBlockCRC) {
             // make next blocks readable without error
             // (repair feature, not yet documented, not tested)
-            this.computedCombinedCRC
-                = (this.storedCombinedCRC << 1)
+            this.computedCombinedCRC = (this.storedCombinedCRC << 1)
                 | (this.storedCombinedCRC >>> 31);
             this.computedCombinedCRC ^= this.storedBlockCRC;
 
-            reportCRCError();
+            throw new IOException("BZip2 CRC error");
         }
 
-        this.computedCombinedCRC
-            = (this.computedCombinedCRC << 1)
+        this.computedCombinedCRC = (this.computedCombinedCRC << 1)
             | (this.computedCombinedCRC >>> 31);
         this.computedCombinedCRC ^= this.computedBlockCRC;
     }
@@ -316,7 +312,7 @@
         this.data = null;
 
         if (this.storedCombinedCRC != this.computedCombinedCRC) {
-            reportCRCError();
+            throw new IOException("BZip2 CRC error");
         }
     }
 
@@ -390,12 +386,8 @@
      * Called by createHuffmanDecodingTables() exclusively.
      */
     private static void hbCreateDecodeTables(final int[] limit,
-                                             final int[] base,
-                                             final int[] perm,
-                                             final char[] length,
-                                             final int minLen,
-                                             final int maxLen,
-                                             final int alphaSize) {
+                                             final int[] base, final int[] perm, final char[]
length,
+                                             final int minLen, final int maxLen, final int
alphaSize) {
         for (int i = minLen, pp = 0; i <= maxLen; i++) {
             for (int j = 0; j < alphaSize; j++) {
                 if (length[j] == i) {
@@ -432,11 +424,11 @@
     }
 
     private void recvDecodingTables() throws IOException {
-        final Data dataShadow     = this.data;
-        final boolean[] inUse     = dataShadow.inUse;
-        final byte[] pos          = dataShadow.recvDecodingTables_pos;
-        final byte[] selector     = dataShadow.selector;
-        final byte[] selectorMtf  = dataShadow.selectorMtf;
+        final Data dataShadow = this.data;
+        final boolean[] inUse = dataShadow.inUse;
+        final byte[] pos = dataShadow.recvDecodingTables_pos;
+        final byte[] selector = dataShadow.selector;
+        final byte[] selectorMtf = dataShadow.selectorMtf;
 
         int inUse16 = 0;
 
@@ -494,7 +486,7 @@
             selector[i] = tmp;
         }
 
-        final char[][] len  = dataShadow.temp_charArray2d;
+        final char[][] len = dataShadow.temp_charArray2d;
 
         /* Now the coding tables */
         for (int t = 0; t < nGroups; t++) {
@@ -518,11 +510,11 @@
     private void createHuffmanDecodingTables(final int alphaSize,
                                              final int nGroups) {
         final Data dataShadow = this.data;
-        final char[][] len  = dataShadow.temp_charArray2d;
+        final char[][] len = dataShadow.temp_charArray2d;
         final int[] minLens = dataShadow.minLens;
         final int[][] limit = dataShadow.limit;
-        final int[][] base  = dataShadow.base;
-        final int[][] perm  = dataShadow.perm;
+        final int[][] base = dataShadow.base;
+        final int[][] perm = dataShadow.perm;
 
         for (int t = 0; t < nGroups; t++) {
             int minLen = 32;
@@ -548,41 +540,40 @@
         recvDecodingTables();
 
         final InputStream inShadow = this.in;
-        final Data dataShadow   = this.data;
-        final byte[] ll8        = dataShadow.ll8;
-        final int[] unzftab     = dataShadow.unzftab;
-        final byte[] selector   = dataShadow.selector;
+        final Data dataShadow = this.data;
+        final byte[] ll8 = dataShadow.ll8;
+        final int[] unzftab = dataShadow.unzftab;
+        final byte[] selector = dataShadow.selector;
         final byte[] seqToUnseq = dataShadow.seqToUnseq;
-        final char[] yy         = dataShadow.getAndMoveToFrontDecode_yy;
-        final int[] minLens     = dataShadow.minLens;
-        final int[][] limit     = dataShadow.limit;
-        final int[][] base      = dataShadow.base;
-        final int[][] perm      = dataShadow.perm;
-        final int limitLast     = this.blockSize100k * 100000;
+        final char[] yy = dataShadow.getAndMoveToFrontDecode_yy;
+        final int[] minLens = dataShadow.minLens;
+        final int[][] limit = dataShadow.limit;
+        final int[][] base = dataShadow.base;
+        final int[][] perm = dataShadow.perm;
+        final int limitLast = this.blockSize100k * 100000;
 
         /*
-          Setting up the unzftab entries here is not strictly
-          necessary, but it does save having to do it later
-          in a separate pass, and so saves a block's worth of
-          cache misses.
-        */
+         * Setting up the unzftab entries here is not strictly necessary, but it
+         * does save having to do it later in a separate pass, and so saves a
+         * block's worth of cache misses.
+         */
         for (int i = 256; --i >= 0;) {
             yy[i] = (char) i;
             unzftab[i] = 0;
         }
 
-        int groupNo     = 0;
-        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];
-        int[] limit_zt  = limit[zt];
-        int[] perm_zt   = perm[zt];
-        int minLens_zt  = minLens[zt];
+        int groupNo = 0;
+        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];
+        int[] limit_zt = limit[zt];
+        int[] perm_zt = perm[zt];
+        int minLens_zt = minLens[zt];
 
         while (nextSym != eob) {
             if ((nextSym == RUNA) || (nextSym == RUNB)) {
@@ -598,12 +589,12 @@
                     }
 
                     if (groupPos == 0) {
-                        groupPos    = G_SIZE - 1;
-                        zt          = selector[++groupNo] & 0xff;
-                        base_zt     = base[zt];
-                        limit_zt    = limit[zt];
-                        perm_zt     = perm[zt];
-                        minLens_zt  = minLens[zt];
+                        groupPos = G_SIZE - 1;
+                        zt = selector[++groupNo] & 0xff;
+                        base_zt = base[zt];
+                        limit_zt = limit[zt];
+                        perm_zt = perm[zt];
+                        minLens_zt = minLens[zt];
                     } else {
                         groupPos--;
                     }
@@ -622,7 +613,8 @@
                             throw new IOException("unexpected end of stream");
                         }
                     }
-                    int zvec = (bsBuffShadow >> (bsLiveShadow - zn)) & ((1 <<
zn) - 1);
+                    int zvec = (bsBuffShadow >> (bsLiveShadow - zn))
+                        & ((1 << zn) - 1);
                     bsLiveShadow -= zn;
 
                     while (zvec > limit_zt[zn]) {
@@ -634,11 +626,13 @@
                                 bsLiveShadow += 8;
                                 continue;
                             } else {
-                                throw new IOException("unexpected end of stream");
+                                throw new IOException(
+                                                      "unexpected end of stream");
                             }
                         }
                         bsLiveShadow--;
-                        zvec = (zvec << 1) | ((bsBuffShadow >> bsLiveShadow)
& 1);
+                        zvec = (zvec << 1)
+                            | ((bsBuffShadow >> bsLiveShadow) & 1);
                     }
                     nextSym = perm_zt[zvec - base_zt[zn]];
                 }
@@ -663,10 +657,10 @@
                 ll8[lastShadow] = seqToUnseq[tmp];
 
                 /*
-                  This loop is hammered during decompression,
-                  hence avoid native method call overhead of
-                  System.arraycopy for very small ranges to copy.
-                */
+                 * This loop is hammered during decompression, hence avoid
+                 * native method call overhead of System.arraycopy for very
+                 * small ranges to copy.
+                 */
                 if (nextSym <= 16) {
                     for (int j = nextSym - 1; j > 0;) {
                         yy[j] = yy[--j];
@@ -678,12 +672,12 @@
                 yy[0] = tmp;
 
                 if (groupPos == 0) {
-                    groupPos    = G_SIZE - 1;
-                    zt          = selector[++groupNo] & 0xff;
-                    base_zt     = base[zt];
-                    limit_zt    = limit[zt];
-                    perm_zt     = perm[zt];
-                    minLens_zt  = minLens[zt];
+                    groupPos = G_SIZE - 1;
+                    zt = selector[++groupNo] & 0xff;
+                    base_zt = base[zt];
+                    limit_zt = limit[zt];
+                    perm_zt = perm[zt];
+                    minLens_zt = minLens[zt];
                 } else {
                     groupPos--;
                 }
@@ -702,7 +696,8 @@
                         throw new IOException("unexpected end of stream");
                     }
                 }
-                int zvec = (bsBuffShadow >> (bsLiveShadow - zn)) & ((1 <<
zn) - 1);
+                int zvec = (bsBuffShadow >> (bsLiveShadow - zn))
+                    & ((1 << zn) - 1);
                 bsLiveShadow -= zn;
 
                 while (zvec > limit_zt[zn]) {
@@ -729,12 +724,11 @@
         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];
+    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;
@@ -769,8 +763,8 @@
         }
 
         final int[] cftab = this.data.cftab;
-        final int[] tt    = this.data.initTT(this.last + 1);
-        final byte[] ll8  = this.data.ll8;
+        final int[] tt = this.data.initTT(this.last + 1);
+        final byte[] ll8 = this.data.ll8;
         cftab[0] = 0;
         System.arraycopy(this.data.unzftab, 0, cftab, 1, 256);
 
@@ -790,7 +784,7 @@
         this.su_tPos = tt[this.origPtr];
         this.su_count = 0;
         this.su_i2 = 0;
-        this.su_ch2 = 256;   /* not a char and not EOF */
+        this.su_ch2 = 256; /* not a char and not EOF */
 
         if (this.blockRandomised) {
             this.su_rNToGo = 0;
@@ -916,35 +910,37 @@
     private static final class Data extends Object {
 
         // (with blockSize 900k)
-        final boolean[] inUse   = new boolean[256];                                   //
     256 byte
+        final boolean[] inUse = new boolean[256]; // 256 byte
 
-        final byte[] seqToUnseq   = new byte[256];                                    //
     256 byte
-        final byte[] selector     = new byte[MAX_SELECTORS];                          //
   18002 byte
-        final byte[] selectorMtf  = new byte[MAX_SELECTORS];                          //
   18002 byte
+        final byte[] seqToUnseq = new byte[256]; // 256 byte
+        final byte[] selector = new byte[MAX_SELECTORS]; // 18002 byte
+        final byte[] selectorMtf = new byte[MAX_SELECTORS]; // 18002 byte
 
         /**
          * Freq table collected to save a pass over the data during
          * decompression.
          */
-        final int[] unzftab = new int[256];                                           //
    1024 byte
+        final int[] unzftab = new int[256]; // 1024 byte
 
-        final int[][] limit = new int[N_GROUPS][MAX_ALPHA_SIZE];                      //
    6192 byte
-        final int[][] base  = new int[N_GROUPS][MAX_ALPHA_SIZE];                      //
    6192 byte
-        final int[][] perm  = new int[N_GROUPS][MAX_ALPHA_SIZE];                      //
    6192 byte
-        final int[] minLens = new int[N_GROUPS];                                      //
      24 byte
-
-        final int[]     cftab     = new int[257];                                     //
    1028 byte
-        final char[]    getAndMoveToFrontDecode_yy = new char[256];                   //
     512 byte
-        final char[][]  temp_charArray2d  = new char[N_GROUPS][MAX_ALPHA_SIZE];       //
    3096 byte
-        final byte[] recvDecodingTables_pos = new byte[N_GROUPS];                     //
       6 byte
-        //---------------
-        //    60798 byte
-
-        int[] tt;                                                                     //
 3600000 byte
-        byte[] ll8;                                                                   //
  900000 byte
-        //---------------
-        //  4560782 byte
-        //===============
+        final int[][] limit = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
+        final int[][] base = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
+        final int[][] perm = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
+        final int[] minLens = new int[N_GROUPS]; // 24 byte
+
+        final int[] cftab = new int[257]; // 1028 byte
+        final char[] getAndMoveToFrontDecode_yy = new char[256]; // 512 byte
+        final char[][] temp_charArray2d = new char[N_GROUPS][MAX_ALPHA_SIZE]; // 3096
+        // byte
+        final byte[] recvDecodingTables_pos = new byte[N_GROUPS]; // 6 byte
+        // ---------------
+        // 60798 byte
+
+        int[] tt; // 3600000 byte
+        byte[] ll8; // 900000 byte
+
+        // ---------------
+        // 4560782 byte
+        // ===============
 
         Data(int blockSize100k) {
             super();
@@ -954,18 +950,17 @@
 
         /**
          * Initializes the {@link #tt} array.
-         *
-         * This method is called when the required length of the array
-         * is known.  I don't initialize it at construction time to
-         * avoid unneccessary memory allocation when compressing small
-         * files.
+         * 
+         * This method is called when the required length of the array is known.
+         * I don't initialize it at construction time to avoid unneccessary
+         * memory allocation when compressing small files.
          */
         final int[] initTT(int length) {
             int[] ttShadow = this.tt;
 
             // tt.length should always be >= length, but theoretically
             // it can happen, if the compressor mixed small and large
-            // blocks.  Normally only the last block will be smaller
+            // blocks. Normally only the last block will be smaller
             // than others.
             if ((ttShadow == null) || (ttShadow.length < length)) {
                 this.tt = ttShadow = new int[length];
@@ -977,4 +972,3 @@
     }
 
 }
-



Mime
View raw message