001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    /*
021     * This package is based on the work done by Keiron Liddle, Aftex Software
022     * <keiron@aftexsw.com> to whom the Ant project is very grateful for his
023     * great code.
024     */
025    package org.apache.commons.compress.compressors.bzip2;
026    
027    import java.io.IOException;
028    import java.io.InputStream;
029    
030    import org.apache.commons.compress.compressors.CompressorInputStream;
031    
032    /**
033     * An input stream that decompresses from the BZip2 format to be read as any other stream.
034     * 
035     * @NotThreadSafe
036     */
037    public class BZip2CompressorInputStream extends CompressorInputStream implements
038                                                                              BZip2Constants {
039    
040        /**
041         * Index of the last char in the block, so the block size == last + 1.
042         */
043        private int last;
044    
045        /**
046         * Index in zptr[] of original string after sorting.
047         */
048        private int origPtr;
049    
050        /**
051         * always: in the range 0 .. 9. The current block size is 100000 * this
052         * number.
053         */
054        private int blockSize100k;
055    
056        private boolean blockRandomised;
057    
058        private int bsBuff;
059        private int bsLive;
060        private final CRC crc = new CRC();
061    
062        private int nInUse;
063    
064        private InputStream in;
065    
066        private int currentChar = -1;
067    
068        private static final int EOF = 0;
069        private static final int START_BLOCK_STATE = 1;
070        private static final int RAND_PART_A_STATE = 2;
071        private static final int RAND_PART_B_STATE = 3;
072        private static final int RAND_PART_C_STATE = 4;
073        private static final int NO_RAND_PART_A_STATE = 5;
074        private static final int NO_RAND_PART_B_STATE = 6;
075        private static final int NO_RAND_PART_C_STATE = 7;
076    
077        private int currentState = START_BLOCK_STATE;
078    
079        private int storedBlockCRC, storedCombinedCRC;
080        private int computedBlockCRC, computedCombinedCRC;
081    
082        // Variables used by setup* methods exclusively
083    
084        private int su_count;
085        private int su_ch2;
086        private int su_chPrev;
087        private int su_i2;
088        private int su_j2;
089        private int su_rNToGo;
090        private int su_rTPos;
091        private int su_tPos;
092        private char su_z;
093    
094        /**
095         * All memory intensive stuff. This field is initialized by initBlock().
096         */
097        private BZip2CompressorInputStream.Data data;
098    
099        /**
100         * Constructs a new BZip2CompressorInputStream which decompresses bytes read from the
101         * specified stream.
102         * 
103         * @throws IOException
104         *             if the stream content is malformed or an I/O error occurs.
105         * @throws NullPointerException
106         *             if <tt>in == null</tt>
107         */
108        public BZip2CompressorInputStream(final InputStream in) throws IOException {
109            super();
110    
111            this.in = in;
112            init();
113        }
114    
115        /*
116         * (non-Javadoc)
117         * 
118         * @see java.io.InputStream#read()
119         */
120        public int read() throws IOException {
121            if (this.in != null) {
122                return read0();
123            } else {
124                throw new IOException("stream closed");
125            }
126        }
127    
128        /*
129         * (non-Javadoc)
130         * 
131         * @see java.io.InputStream#read(byte[], int, int)
132         */
133        public int read(final byte[] dest, final int offs, final int len)
134            throws IOException {
135            if (offs < 0) {
136                throw new IndexOutOfBoundsException("offs(" + offs + ") < 0.");
137            }
138            if (len < 0) {
139                throw new IndexOutOfBoundsException("len(" + len + ") < 0.");
140            }
141            if (offs + len > dest.length) {
142                throw new IndexOutOfBoundsException("offs(" + offs + ") + len("
143                                                    + len + ") > dest.length(" + dest.length + ").");
144            }
145            if (this.in == null) {
146                throw new IOException("stream closed");
147            }
148    
149            final int hi = offs + len;
150            int destOffs = offs;
151            for (int b; (destOffs < hi) && ((b = read0()) >= 0);) {
152                dest[destOffs++] = (byte) b;
153            }
154    
155            return (destOffs == offs) ? -1 : (destOffs - offs);
156        }
157    
158        private void makeMaps() {
159            final boolean[] inUse = this.data.inUse;
160            final byte[] seqToUnseq = this.data.seqToUnseq;
161    
162            int nInUseShadow = 0;
163    
164            for (int i = 0; i < 256; i++) {
165                if (inUse[i])
166                    seqToUnseq[nInUseShadow++] = (byte) i;
167            }
168    
169            this.nInUse = nInUseShadow;
170        }
171    
172        private int read0() throws IOException {
173            final int retChar = this.currentChar;
174    
175            switch (this.currentState) {
176            case EOF:
177                return -1;
178    
179            case START_BLOCK_STATE:
180                throw new IllegalStateException();
181    
182            case RAND_PART_A_STATE:
183                throw new IllegalStateException();
184    
185            case RAND_PART_B_STATE:
186                setupRandPartB();
187                break;
188    
189            case RAND_PART_C_STATE:
190                setupRandPartC();
191                break;
192    
193            case NO_RAND_PART_A_STATE:
194                throw new IllegalStateException();
195    
196            case NO_RAND_PART_B_STATE:
197                setupNoRandPartB();
198                break;
199    
200            case NO_RAND_PART_C_STATE:
201                setupNoRandPartC();
202                break;
203    
204            default:
205                throw new IllegalStateException();
206            }
207    
208            return retChar;
209        }
210    
211        private void init() throws IOException {
212            if (null == in) {
213                throw new IOException("No InputStream");
214            }
215            if (in.available() == 0) {
216                throw new IOException("Empty InputStream");
217            }
218            checkMagicChar('B', "first");
219            checkMagicChar('Z', "second");
220            checkMagicChar('h', "third");
221    
222            int blockSize = this.in.read();
223            if ((blockSize < '1') || (blockSize > '9')) {
224                throw new IOException("Stream is not BZip2 formatted: illegal "
225                                      + "blocksize " + (char) blockSize);
226            }
227    
228            this.blockSize100k = blockSize - '0';
229    
230            initBlock();
231            setupBlock();
232        }
233    
234        private void checkMagicChar(char expected, String position)
235            throws IOException {
236            int magic = this.in.read();
237            if (magic != expected) {
238                throw new IOException("Stream is not BZip2 formatted: expected '"
239                                      + expected + "' as " + position + " byte but got '"
240                                      + (char) magic + "'");
241            }
242        }
243    
244        private void initBlock() throws IOException {
245            char magic0 = bsGetUByte();
246            char magic1 = bsGetUByte();
247            char magic2 = bsGetUByte();
248            char magic3 = bsGetUByte();
249            char magic4 = bsGetUByte();
250            char magic5 = bsGetUByte();
251    
252            if (magic0 == 0x17 && magic1 == 0x72 && magic2 == 0x45
253                && magic3 == 0x38 && magic4 == 0x50 && magic5 == 0x90) {
254                complete(); // end of file
255            } else if (magic0 != 0x31 || // '1'
256                       magic1 != 0x41 || // ')'
257                       magic2 != 0x59 || // 'Y'
258                       magic3 != 0x26 || // '&'
259                       magic4 != 0x53 || // 'S'
260                       magic5 != 0x59 // 'Y'
261                       ) {
262                this.currentState = EOF;
263                throw new IOException("bad block header");
264            } else {
265                this.storedBlockCRC = bsGetInt();
266                this.blockRandomised = bsR(1) == 1;
267    
268                /**
269                 * Allocate data here instead in constructor, so we do not allocate
270                 * it if the input file is empty.
271                 */
272                if (this.data == null) {
273                    this.data = new Data(this.blockSize100k);
274                }
275    
276                // currBlockNo++;
277                getAndMoveToFrontDecode();
278    
279                this.crc.initialiseCRC();
280                this.currentState = START_BLOCK_STATE;
281            }
282        }
283    
284        private void endBlock() throws IOException {
285            this.computedBlockCRC = this.crc.getFinalCRC();
286    
287            // A bad CRC is considered a fatal error.
288            if (this.storedBlockCRC != this.computedBlockCRC) {
289                // make next blocks readable without error
290                // (repair feature, not yet documented, not tested)
291                this.computedCombinedCRC = (this.storedCombinedCRC << 1)
292                    | (this.storedCombinedCRC >>> 31);
293                this.computedCombinedCRC ^= this.storedBlockCRC;
294    
295                throw new IOException("BZip2 CRC error");
296            }
297    
298            this.computedCombinedCRC = (this.computedCombinedCRC << 1)
299                | (this.computedCombinedCRC >>> 31);
300            this.computedCombinedCRC ^= this.computedBlockCRC;
301        }
302    
303        private void complete() throws IOException {
304            this.storedCombinedCRC = bsGetInt();
305            this.currentState = EOF;
306            this.data = null;
307    
308            if (this.storedCombinedCRC != this.computedCombinedCRC) {
309                throw new IOException("BZip2 CRC error");
310            }
311        }
312    
313        public void close() throws IOException {
314            InputStream inShadow = this.in;
315            if (inShadow != null) {
316                try {
317                    if (inShadow != System.in) {
318                        inShadow.close();
319                    }
320                } finally {
321                    this.data = null;
322                    this.in = null;
323                }
324            }
325        }
326    
327        private int bsR(final int n) throws IOException {
328            int bsLiveShadow = this.bsLive;
329            int bsBuffShadow = this.bsBuff;
330    
331            if (bsLiveShadow < n) {
332                final InputStream inShadow = this.in;
333                do {
334                    int thech = inShadow.read();
335    
336                    if (thech < 0) {
337                        throw new IOException("unexpected end of stream");
338                    }
339    
340                    bsBuffShadow = (bsBuffShadow << 8) | thech;
341                    bsLiveShadow += 8;
342                } while (bsLiveShadow < n);
343    
344                this.bsBuff = bsBuffShadow;
345            }
346    
347            this.bsLive = bsLiveShadow - n;
348            return (bsBuffShadow >> (bsLiveShadow - n)) & ((1 << n) - 1);
349        }
350    
351        private boolean bsGetBit() throws IOException {
352            int bsLiveShadow = this.bsLive;
353            int bsBuffShadow = this.bsBuff;
354    
355            if (bsLiveShadow < 1) {
356                int thech = this.in.read();
357    
358                if (thech < 0) {
359                    throw new IOException("unexpected end of stream");
360                }
361    
362                bsBuffShadow = (bsBuffShadow << 8) | thech;
363                bsLiveShadow += 8;
364                this.bsBuff = bsBuffShadow;
365            }
366    
367            this.bsLive = bsLiveShadow - 1;
368            return ((bsBuffShadow >> (bsLiveShadow - 1)) & 1) != 0;
369        }
370    
371        private char bsGetUByte() throws IOException {
372            return (char) bsR(8);
373        }
374    
375        private int bsGetInt() throws IOException {
376            return (((((bsR(8) << 8) | bsR(8)) << 8) | bsR(8)) << 8) | bsR(8);
377        }
378    
379        /**
380         * Called by createHuffmanDecodingTables() exclusively.
381         */
382        private static void hbCreateDecodeTables(final int[] limit,
383                                                 final int[] base, final int[] perm, final char[] length,
384                                                 final int minLen, final int maxLen, final int alphaSize) {
385            for (int i = minLen, pp = 0; i <= maxLen; i++) {
386                for (int j = 0; j < alphaSize; j++) {
387                    if (length[j] == i) {
388                        perm[pp++] = j;
389                    }
390                }
391            }
392    
393            for (int i = MAX_CODE_LEN; --i > 0;) {
394                base[i] = 0;
395                limit[i] = 0;
396            }
397    
398            for (int i = 0; i < alphaSize; i++) {
399                base[length[i] + 1]++;
400            }
401    
402            for (int i = 1, b = base[0]; i < MAX_CODE_LEN; i++) {
403                b += base[i];
404                base[i] = b;
405            }
406    
407            for (int i = minLen, vec = 0, b = base[i]; i <= maxLen; i++) {
408                final int nb = base[i + 1];
409                vec += nb - b;
410                b = nb;
411                limit[i] = vec - 1;
412                vec <<= 1;
413            }
414    
415            for (int i = minLen + 1; i <= maxLen; i++) {
416                base[i] = ((limit[i - 1] + 1) << 1) - base[i];
417            }
418        }
419    
420        private void recvDecodingTables() throws IOException {
421            final Data dataShadow = this.data;
422            final boolean[] inUse = dataShadow.inUse;
423            final byte[] pos = dataShadow.recvDecodingTables_pos;
424            final byte[] selector = dataShadow.selector;
425            final byte[] selectorMtf = dataShadow.selectorMtf;
426    
427            int inUse16 = 0;
428    
429            /* Receive the mapping table */
430            for (int i = 0; i < 16; i++) {
431                if (bsGetBit()) {
432                    inUse16 |= 1 << i;
433                }
434            }
435    
436            for (int i = 256; --i >= 0;) {
437                inUse[i] = false;
438            }
439    
440            for (int i = 0; i < 16; i++) {
441                if ((inUse16 & (1 << i)) != 0) {
442                    final int i16 = i << 4;
443                    for (int j = 0; j < 16; j++) {
444                        if (bsGetBit()) {
445                            inUse[i16 + j] = true;
446                        }
447                    }
448                }
449            }
450    
451            makeMaps();
452            final int alphaSize = this.nInUse + 2;
453    
454            /* Now the selectors */
455            final int nGroups = bsR(3);
456            final int nSelectors = bsR(15);
457    
458            for (int i = 0; i < nSelectors; i++) {
459                int j = 0;
460                while (bsGetBit()) {
461                    j++;
462                }
463                selectorMtf[i] = (byte) j;
464            }
465    
466            /* Undo the MTF values for the selectors. */
467            for (int v = nGroups; --v >= 0;) {
468                pos[v] = (byte) v;
469            }
470    
471            for (int i = 0; i < nSelectors; i++) {
472                int v = selectorMtf[i] & 0xff;
473                final byte tmp = pos[v];
474                while (v > 0) {
475                    // nearly all times v is zero, 4 in most other cases
476                    pos[v] = pos[v - 1];
477                    v--;
478                }
479                pos[0] = tmp;
480                selector[i] = tmp;
481            }
482    
483            final char[][] len = dataShadow.temp_charArray2d;
484    
485            /* Now the coding tables */
486            for (int t = 0; t < nGroups; t++) {
487                int curr = bsR(5);
488                final char[] len_t = len[t];
489                for (int i = 0; i < alphaSize; i++) {
490                    while (bsGetBit()) {
491                        curr += bsGetBit() ? -1 : 1;
492                    }
493                    len_t[i] = (char) curr;
494                }
495            }
496    
497            // finally create the Huffman tables
498            createHuffmanDecodingTables(alphaSize, nGroups);
499        }
500    
501        /**
502         * Called by recvDecodingTables() exclusively.
503         */
504        private void createHuffmanDecodingTables(final int alphaSize,
505                                                 final int nGroups) {
506            final Data dataShadow = this.data;
507            final char[][] len = dataShadow.temp_charArray2d;
508            final int[] minLens = dataShadow.minLens;
509            final int[][] limit = dataShadow.limit;
510            final int[][] base = dataShadow.base;
511            final int[][] perm = dataShadow.perm;
512    
513            for (int t = 0; t < nGroups; t++) {
514                int minLen = 32;
515                int maxLen = 0;
516                final char[] len_t = len[t];
517                for (int i = alphaSize; --i >= 0;) {
518                    final char lent = len_t[i];
519                    if (lent > maxLen) {
520                        maxLen = lent;
521                    }
522                    if (lent < minLen) {
523                        minLen = lent;
524                    }
525                }
526                hbCreateDecodeTables(limit[t], base[t], perm[t], len[t], minLen,
527                                     maxLen, alphaSize);
528                minLens[t] = minLen;
529            }
530        }
531    
532        private void getAndMoveToFrontDecode() throws IOException {
533            this.origPtr = bsR(24);
534            recvDecodingTables();
535    
536            final InputStream inShadow = this.in;
537            final Data dataShadow = this.data;
538            final byte[] ll8 = dataShadow.ll8;
539            final int[] unzftab = dataShadow.unzftab;
540            final byte[] selector = dataShadow.selector;
541            final byte[] seqToUnseq = dataShadow.seqToUnseq;
542            final char[] yy = dataShadow.getAndMoveToFrontDecode_yy;
543            final int[] minLens = dataShadow.minLens;
544            final int[][] limit = dataShadow.limit;
545            final int[][] base = dataShadow.base;
546            final int[][] perm = dataShadow.perm;
547            final int limitLast = this.blockSize100k * 100000;
548    
549            /*
550             * Setting up the unzftab entries here is not strictly necessary, but it
551             * does save having to do it later in a separate pass, and so saves a
552             * block's worth of cache misses.
553             */
554            for (int i = 256; --i >= 0;) {
555                yy[i] = (char) i;
556                unzftab[i] = 0;
557            }
558    
559            int groupNo = 0;
560            int groupPos = G_SIZE - 1;
561            final int eob = this.nInUse + 1;
562            int nextSym = getAndMoveToFrontDecode0(0);
563            int bsBuffShadow = this.bsBuff;
564            int bsLiveShadow = this.bsLive;
565            int lastShadow = -1;
566            int zt = selector[groupNo] & 0xff;
567            int[] base_zt = base[zt];
568            int[] limit_zt = limit[zt];
569            int[] perm_zt = perm[zt];
570            int minLens_zt = minLens[zt];
571    
572            while (nextSym != eob) {
573                if ((nextSym == RUNA) || (nextSym == RUNB)) {
574                    int s = -1;
575    
576                    for (int n = 1; true; n <<= 1) {
577                        if (nextSym == RUNA) {
578                            s += n;
579                        } else if (nextSym == RUNB) {
580                            s += n << 1;
581                        } else {
582                            break;
583                        }
584    
585                        if (groupPos == 0) {
586                            groupPos = G_SIZE - 1;
587                            zt = selector[++groupNo] & 0xff;
588                            base_zt = base[zt];
589                            limit_zt = limit[zt];
590                            perm_zt = perm[zt];
591                            minLens_zt = minLens[zt];
592                        } else {
593                            groupPos--;
594                        }
595    
596                        int zn = minLens_zt;
597    
598                        // Inlined:
599                        // int zvec = bsR(zn);
600                        while (bsLiveShadow < zn) {
601                            final int thech = inShadow.read();
602                            if (thech >= 0) {
603                                bsBuffShadow = (bsBuffShadow << 8) | thech;
604                                bsLiveShadow += 8;
605                                continue;
606                            } else {
607                                throw new IOException("unexpected end of stream");
608                            }
609                        }
610                        int zvec = (bsBuffShadow >> (bsLiveShadow - zn))
611                            & ((1 << zn) - 1);
612                        bsLiveShadow -= zn;
613    
614                        while (zvec > limit_zt[zn]) {
615                            zn++;
616                            while (bsLiveShadow < 1) {
617                                final int thech = inShadow.read();
618                                if (thech >= 0) {
619                                    bsBuffShadow = (bsBuffShadow << 8) | thech;
620                                    bsLiveShadow += 8;
621                                    continue;
622                                } else {
623                                    throw new IOException(
624                                                          "unexpected end of stream");
625                                }
626                            }
627                            bsLiveShadow--;
628                            zvec = (zvec << 1)
629                                | ((bsBuffShadow >> bsLiveShadow) & 1);
630                        }
631                        nextSym = perm_zt[zvec - base_zt[zn]];
632                    }
633    
634                    final byte ch = seqToUnseq[yy[0]];
635                    unzftab[ch & 0xff] += s + 1;
636    
637                    while (s-- >= 0) {
638                        ll8[++lastShadow] = ch;
639                    }
640    
641                    if (lastShadow >= limitLast) {
642                        throw new IOException("block overrun");
643                    }
644                } else {
645                    if (++lastShadow >= limitLast) {
646                        throw new IOException("block overrun");
647                    }
648    
649                    final char tmp = yy[nextSym - 1];
650                    unzftab[seqToUnseq[tmp] & 0xff]++;
651                    ll8[lastShadow] = seqToUnseq[tmp];
652    
653                    /*
654                     * This loop is hammered during decompression, hence avoid
655                     * native method call overhead of System.arraycopy for very
656                     * small ranges to copy.
657                     */
658                    if (nextSym <= 16) {
659                        for (int j = nextSym - 1; j > 0;) {
660                            yy[j] = yy[--j];
661                        }
662                    } else {
663                        System.arraycopy(yy, 0, yy, 1, nextSym - 1);
664                    }
665    
666                    yy[0] = tmp;
667    
668                    if (groupPos == 0) {
669                        groupPos = G_SIZE - 1;
670                        zt = selector[++groupNo] & 0xff;
671                        base_zt = base[zt];
672                        limit_zt = limit[zt];
673                        perm_zt = perm[zt];
674                        minLens_zt = minLens[zt];
675                    } else {
676                        groupPos--;
677                    }
678    
679                    int zn = minLens_zt;
680    
681                    // Inlined:
682                    // int zvec = bsR(zn);
683                    while (bsLiveShadow < zn) {
684                        final int thech = inShadow.read();
685                        if (thech >= 0) {
686                            bsBuffShadow = (bsBuffShadow << 8) | thech;
687                            bsLiveShadow += 8;
688                            continue;
689                        } else {
690                            throw new IOException("unexpected end of stream");
691                        }
692                    }
693                    int zvec = (bsBuffShadow >> (bsLiveShadow - zn))
694                        & ((1 << zn) - 1);
695                    bsLiveShadow -= zn;
696    
697                    while (zvec > limit_zt[zn]) {
698                        zn++;
699                        while (bsLiveShadow < 1) {
700                            final int thech = inShadow.read();
701                            if (thech >= 0) {
702                                bsBuffShadow = (bsBuffShadow << 8) | thech;
703                                bsLiveShadow += 8;
704                                continue;
705                            } else {
706                                throw new IOException("unexpected end of stream");
707                            }
708                        }
709                        bsLiveShadow--;
710                        zvec = (zvec << 1) | ((bsBuffShadow >> bsLiveShadow) & 1);
711                    }
712                    nextSym = perm_zt[zvec - base_zt[zn]];
713                }
714            }
715    
716            this.last = lastShadow;
717            this.bsLive = bsLiveShadow;
718            this.bsBuff = bsBuffShadow;
719        }
720    
721        private int getAndMoveToFrontDecode0(final int groupNo) throws IOException {
722            final InputStream inShadow = this.in;
723            final Data dataShadow = this.data;
724            final int zt = dataShadow.selector[groupNo] & 0xff;
725            final int[] limit_zt = dataShadow.limit[zt];
726            int zn = dataShadow.minLens[zt];
727            int zvec = bsR(zn);
728            int bsLiveShadow = this.bsLive;
729            int bsBuffShadow = this.bsBuff;
730    
731            while (zvec > limit_zt[zn]) {
732                zn++;
733                while (bsLiveShadow < 1) {
734                    final int thech = inShadow.read();
735    
736                    if (thech >= 0) {
737                        bsBuffShadow = (bsBuffShadow << 8) | thech;
738                        bsLiveShadow += 8;
739                        continue;
740                    } else {
741                        throw new IOException("unexpected end of stream");
742                    }
743                }
744                bsLiveShadow--;
745                zvec = (zvec << 1) | ((bsBuffShadow >> bsLiveShadow) & 1);
746            }
747    
748            this.bsLive = bsLiveShadow;
749            this.bsBuff = bsBuffShadow;
750    
751            return dataShadow.perm[zt][zvec - dataShadow.base[zt][zn]];
752        }
753    
754        private void setupBlock() throws IOException {
755            if (this.data == null) {
756                return;
757            }
758    
759            final int[] cftab = this.data.cftab;
760            final int[] tt = this.data.initTT(this.last + 1);
761            final byte[] ll8 = this.data.ll8;
762            cftab[0] = 0;
763            System.arraycopy(this.data.unzftab, 0, cftab, 1, 256);
764    
765            for (int i = 1, c = cftab[0]; i <= 256; i++) {
766                c += cftab[i];
767                cftab[i] = c;
768            }
769    
770            for (int i = 0, lastShadow = this.last; i <= lastShadow; i++) {
771                tt[cftab[ll8[i] & 0xff]++] = i;
772            }
773    
774            if ((this.origPtr < 0) || (this.origPtr >= tt.length)) {
775                throw new IOException("stream corrupted");
776            }
777    
778            this.su_tPos = tt[this.origPtr];
779            this.su_count = 0;
780            this.su_i2 = 0;
781            this.su_ch2 = 256; /* not a char and not EOF */
782    
783            if (this.blockRandomised) {
784                this.su_rNToGo = 0;
785                this.su_rTPos = 0;
786                setupRandPartA();
787            } else {
788                setupNoRandPartA();
789            }
790        }
791    
792        private void setupRandPartA() throws IOException {
793            if (this.su_i2 <= this.last) {
794                this.su_chPrev = this.su_ch2;
795                int su_ch2Shadow = this.data.ll8[this.su_tPos] & 0xff;
796                this.su_tPos = this.data.tt[this.su_tPos];
797                if (this.su_rNToGo == 0) {
798                    this.su_rNToGo = Rand.rNums(this.su_rTPos) - 1;
799                    if (++this.su_rTPos == 512) {
800                        this.su_rTPos = 0;
801                    }
802                } else {
803                    this.su_rNToGo--;
804                }
805                this.su_ch2 = su_ch2Shadow ^= (this.su_rNToGo == 1) ? 1 : 0;
806                this.su_i2++;
807                this.currentChar = su_ch2Shadow;
808                this.currentState = RAND_PART_B_STATE;
809                this.crc.updateCRC(su_ch2Shadow);
810            } else {
811                endBlock();
812                initBlock();
813                setupBlock();
814            }
815        }
816    
817        private void setupNoRandPartA() throws IOException {
818            if (this.su_i2 <= this.last) {
819                this.su_chPrev = this.su_ch2;
820                int su_ch2Shadow = this.data.ll8[this.su_tPos] & 0xff;
821                this.su_ch2 = su_ch2Shadow;
822                this.su_tPos = this.data.tt[this.su_tPos];
823                this.su_i2++;
824                this.currentChar = su_ch2Shadow;
825                this.currentState = NO_RAND_PART_B_STATE;
826                this.crc.updateCRC(su_ch2Shadow);
827            } else {
828                this.currentState = NO_RAND_PART_A_STATE;
829                endBlock();
830                initBlock();
831                setupBlock();
832            }
833        }
834    
835        private void setupRandPartB() throws IOException {
836            if (this.su_ch2 != this.su_chPrev) {
837                this.currentState = RAND_PART_A_STATE;
838                this.su_count = 1;
839                setupRandPartA();
840            } else if (++this.su_count >= 4) {
841                this.su_z = (char) (this.data.ll8[this.su_tPos] & 0xff);
842                this.su_tPos = this.data.tt[this.su_tPos];
843                if (this.su_rNToGo == 0) {
844                    this.su_rNToGo = Rand.rNums(this.su_rTPos) - 1;
845                    if (++this.su_rTPos == 512) {
846                        this.su_rTPos = 0;
847                    }
848                } else {
849                    this.su_rNToGo--;
850                }
851                this.su_j2 = 0;
852                this.currentState = RAND_PART_C_STATE;
853                if (this.su_rNToGo == 1) {
854                    this.su_z ^= 1;
855                }
856                setupRandPartC();
857            } else {
858                this.currentState = RAND_PART_A_STATE;
859                setupRandPartA();
860            }
861        }
862    
863        private void setupRandPartC() throws IOException {
864            if (this.su_j2 < this.su_z) {
865                this.currentChar = this.su_ch2;
866                this.crc.updateCRC(this.su_ch2);
867                this.su_j2++;
868            } else {
869                this.currentState = RAND_PART_A_STATE;
870                this.su_i2++;
871                this.su_count = 0;
872                setupRandPartA();
873            }
874        }
875    
876        private void setupNoRandPartB() throws IOException {
877            if (this.su_ch2 != this.su_chPrev) {
878                this.su_count = 1;
879                setupNoRandPartA();
880            } else if (++this.su_count >= 4) {
881                this.su_z = (char) (this.data.ll8[this.su_tPos] & 0xff);
882                this.su_tPos = this.data.tt[this.su_tPos];
883                this.su_j2 = 0;
884                setupNoRandPartC();
885            } else {
886                setupNoRandPartA();
887            }
888        }
889    
890        private void setupNoRandPartC() throws IOException {
891            if (this.su_j2 < this.su_z) {
892                int su_ch2Shadow = this.su_ch2;
893                this.currentChar = su_ch2Shadow;
894                this.crc.updateCRC(su_ch2Shadow);
895                this.su_j2++;
896                this.currentState = NO_RAND_PART_C_STATE;
897            } else {
898                this.su_i2++;
899                this.su_count = 0;
900                setupNoRandPartA();
901            }
902        }
903    
904        private static final class Data extends Object {
905    
906            // (with blockSize 900k)
907            final boolean[] inUse = new boolean[256]; // 256 byte
908    
909            final byte[] seqToUnseq = new byte[256]; // 256 byte
910            final byte[] selector = new byte[MAX_SELECTORS]; // 18002 byte
911            final byte[] selectorMtf = new byte[MAX_SELECTORS]; // 18002 byte
912    
913            /**
914             * Freq table collected to save a pass over the data during
915             * decompression.
916             */
917            final int[] unzftab = new int[256]; // 1024 byte
918    
919            final int[][] limit = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
920            final int[][] base = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
921            final int[][] perm = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
922            final int[] minLens = new int[N_GROUPS]; // 24 byte
923    
924            final int[] cftab = new int[257]; // 1028 byte
925            final char[] getAndMoveToFrontDecode_yy = new char[256]; // 512 byte
926            final char[][] temp_charArray2d = new char[N_GROUPS][MAX_ALPHA_SIZE]; // 3096
927            // byte
928            final byte[] recvDecodingTables_pos = new byte[N_GROUPS]; // 6 byte
929            // ---------------
930            // 60798 byte
931    
932            int[] tt; // 3600000 byte
933            byte[] ll8; // 900000 byte
934    
935            // ---------------
936            // 4560782 byte
937            // ===============
938    
939            Data(int blockSize100k) {
940                super();
941    
942                this.ll8 = new byte[blockSize100k * BZip2Constants.BASEBLOCKSIZE];
943            }
944    
945            /**
946             * Initializes the {@link #tt} array.
947             * 
948             * This method is called when the required length of the array is known.
949             * I don't initialize it at construction time to avoid unneccessary
950             * memory allocation when compressing small files.
951             */
952            final int[] initTT(int length) {
953                int[] ttShadow = this.tt;
954    
955                // tt.length should always be >= length, but theoretically
956                // it can happen, if the compressor mixed small and large
957                // blocks. Normally only the last block will be smaller
958                // than others.
959                if ((ttShadow == null) || (ttShadow.length < length)) {
960                    this.tt = ttShadow = new int[length];
961                }
962    
963                return ttShadow;
964            }
965    
966        }
967    
968    }