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