📄 cbzip2outputstream.java
字号:
/* Process big buckets, starting with the least full. */ ss = runningOrder[i]; /* Complete the big bucket [ss] by quicksorting any unsorted small buckets [ss, j]. Hopefully previous pointer-scanning phases have already completed many of the small buckets [ss, j], so we don't have to sort them at all. */ for (j = 0; j <= 255; j++) { sb = (ss << 8) + j; if (!((ftab[sb] & SETMASK) == SETMASK)) { int lo = ftab[sb] & CLEARMASK; int hi = (ftab[sb + 1] & CLEARMASK) - 1; if (hi > lo) { qSort3(lo, hi, 2); numQSorted += (hi - lo + 1); if (workDone > workLimit && firstAttempt) { return; } } ftab[sb] |= SETMASK; } } /* The ss big bucket is now done. Record this fact, and update the quadrant descriptors. Remember to update quadrants in the overshoot area too, if necessary. The "if (i < 255)" test merely skips this updating for the last bucket processed, since updating for the last bucket is pointless. */ bigDone[ss] = true; if (i < 255) { int bbStart = ftab[ss << 8] & CLEARMASK; int bbSize = (ftab[(ss + 1) << 8] & CLEARMASK) - bbStart; int shifts = 0; while ((bbSize >> shifts) > 65534) { shifts++; } for (j = 0; j < bbSize; j++) { int a2update = zptr[bbStart + j]; int qVal = (j >> shifts); quadrant[a2update] = qVal; if (a2update < NUM_OVERSHOOT_BYTES) { quadrant[a2update + last + 1] = qVal; } } if (!(((bbSize - 1) >> shifts) <= 65535)) { panic(); } } /* Now scan this big bucket so as to synthesise the sorted order for small buckets [t, ss] for all t != ss. */ for (j = 0; j <= 255; j++) { copy[j] = ftab[(j << 8) + ss] & CLEARMASK; } for (j = ftab[ss << 8] & CLEARMASK; j < (ftab[(ss + 1) << 8] & CLEARMASK); j++) { c1 = block[zptr[j]]; if (!bigDone[c1]) { zptr[copy[c1]] = zptr[j] == 0 ? last : zptr[j] - 1; copy[c1]++; } } for (j = 0; j <= 255; j++) { ftab[(j << 8) + ss] |= SETMASK; } } } } private void randomiseBlock() { int i; int rNToGo = 0; int rTPos = 0; for (i = 0; i < 256; i++) { inUse[i] = false; } for (i = 0; i <= last; i++) { if (rNToGo == 0) { rNToGo = (char) rNums[rTPos]; rTPos++; if (rTPos == 512) { rTPos = 0; } } rNToGo--; block[i + 1] ^= ((rNToGo == 1) ? 1 : 0); // handle 16 bit signed numbers block[i + 1] &= 0xFF; inUse[block[i + 1]] = true; } } private void doReversibleTransformation() { int i; workLimit = workFactor * last; workDone = 0; blockRandomised = false; firstAttempt = true; mainSort(); if (workDone > workLimit && firstAttempt) { randomiseBlock(); workLimit = workDone = 0; blockRandomised = true; firstAttempt = false; mainSort(); } origPtr = -1; for (i = 0; i <= last; i++) { if (zptr[i] == 0) { origPtr = i; break; } }; if (origPtr == -1) { panic(); } } private boolean fullGtU(int i1, int i2) { int k; char c1, c2; int s1, s2; c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } i1++; i2++; c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } i1++; i2++; c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } i1++; i2++; c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } i1++; i2++; c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } i1++; i2++; c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } i1++; i2++; k = last + 1; do { c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } s1 = quadrant[i1]; s2 = quadrant[i2]; if (s1 != s2) { return (s1 > s2); } i1++; i2++; c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } s1 = quadrant[i1]; s2 = quadrant[i2]; if (s1 != s2) { return (s1 > s2); } i1++; i2++; c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } s1 = quadrant[i1]; s2 = quadrant[i2]; if (s1 != s2) { return (s1 > s2); } i1++; i2++; c1 = block[i1 + 1]; c2 = block[i2 + 1]; if (c1 != c2) { return (c1 > c2); } s1 = quadrant[i1]; s2 = quadrant[i2]; if (s1 != s2) { return (s1 > s2); } i1++; i2++; if (i1 > last) { i1 -= last; i1--; }; if (i2 > last) { i2 -= last; i2--; }; k -= 4; workDone++; } while (k >= 0); return false; } /* Knuth's increments seem to work better than Incerpi-Sedgewick here. Possibly because the number of elems to sort is usually small, typically <= 20. */ private int[] incs = {1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161, 2391484}; private void allocateCompressStructures () { int n = baseBlockSize * blockSize100k; block = new char[(n + 1 + NUM_OVERSHOOT_BYTES)]; quadrant = new int[(n + NUM_OVERSHOOT_BYTES)]; zptr = new int[n]; ftab = new int[65537]; if (block == null || quadrant == null || zptr == null || ftab == null) { //int totalDraw = (n + 1 + NUM_OVERSHOOT_BYTES) + (n + NUM_OVERSHOOT_BYTES) + n + 65537; //compressOutOfMemory ( totalDraw, n ); } /* The back end needs a place to store the MTF values whilst it calculates the coding tables. We could put them in the zptr array. However, these values will fit in a short, so we overlay szptr at the start of zptr, in the hope of reducing the number of cache misses induced by the multiple traversals of the MTF values when calculating coding tables. Seems to improve compression speed by about 1%. */ // szptr = zptr; szptr = new short[2 * n]; } private void generateMTFValues() { char[] yy = new char[256]; int i, j; char tmp; char tmp2; int zPend; int wr; int EOB; makeMaps(); EOB = nInUse + 1; for (i = 0; i <= EOB; i++) { mtfFreq[i] = 0; } wr = 0; zPend = 0; for (i = 0; i < nInUse; i++) { yy[i] = (char) i; } for (i = 0; i <= last; i++) { char ll_i; ll_i = unseqToSeq[block[zptr[i]]]; j = 0; tmp = yy[j]; while (ll_i != tmp) { j++; tmp2 = tmp; tmp = yy[j]; yy[j] = tmp2; }; yy[0] = tmp; if (j == 0) { zPend++; } else { if (zPend > 0) { zPend--; while (true) { switch (zPend % 2) { case 0: szptr[wr] = (short) RUNA; wr++; mtfFreq[RUNA]++; break; case 1: szptr[wr] = (short) RUNB; wr++; mtfFreq[RUNB]++; break; }; if (zPend < 2) { break; } zPend = (zPend - 2) / 2; }; zPend = 0; } szptr[wr] = (short) (j + 1); wr++; mtfFreq[j + 1]++; } } if (zPend > 0) { zPend--; while (true) { switch (zPend % 2) { case 0: szptr[wr] = (short) RUNA; wr++; mtfFreq[RUNA]++; break; case 1: szptr[wr] = (short) RUNB; wr++; mtfFreq[RUNB]++; break; } if (zPend < 2) { break; } zPend = (zPend - 2) / 2; } } szptr[wr] = (short) EOB; wr++; mtfFreq[EOB]++; nMTF = wr; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -