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 */
019package org.apache.commons.compress.compressors.bzip2;
020
021import java.io.IOException;
022import java.io.OutputStream;
023
024import org.apache.commons.compress.compressors.CompressorOutputStream;
025
026/**
027 * An output stream that compresses into the BZip2 format into another stream.
028 *
029 * <p>
030 * The compression requires large amounts of memory. Thus you should call the
031 * {@link #close() close()} method as soon as possible, to force
032 * {@code BZip2CompressorOutputStream} to release the allocated memory.
033 * </p>
034 *
035 * <p> You can shrink the amount of allocated memory and maybe raise
036 * the compression speed by choosing a lower blocksize, which in turn
037 * may cause a lower compression ratio. You can avoid unnecessary
038 * memory allocation by avoiding using a blocksize which is bigger
039 * than the size of the input.  </p>
040 *
041 * <p> You can compute the memory usage for compressing by the
042 * following formula: </p>
043 *
044 * <pre>
045 * &lt;code&gt;400k + (9 * blocksize)&lt;/code&gt;.
046 * </pre>
047 *
048 * <p> To get the memory required for decompression by {@link
049 * BZip2CompressorInputStream} use </p>
050 *
051 * <pre>
052 * &lt;code&gt;65k + (5 * blocksize)&lt;/code&gt;.
053 * </pre>
054 *
055 * <table width="100%" border="1" summary="Memory usage by blocksize">
056 * <tr>
057 * <th colspan="3">Memory usage by blocksize</th>
058 * </tr>
059 * <tr>
060 * <th align="right">Blocksize</th> <th align="right">Compression<br>
061 * memory usage</th> <th align="right">Decompression<br>
062 * memory usage</th>
063 * </tr>
064 * <tr>
065 * <td align="right">100k</td>
066 * <td align="right">1300k</td>
067 * <td align="right">565k</td>
068 * </tr>
069 * <tr>
070 * <td align="right">200k</td>
071 * <td align="right">2200k</td>
072 * <td align="right">1065k</td>
073 * </tr>
074 * <tr>
075 * <td align="right">300k</td>
076 * <td align="right">3100k</td>
077 * <td align="right">1565k</td>
078 * </tr>
079 * <tr>
080 * <td align="right">400k</td>
081 * <td align="right">4000k</td>
082 * <td align="right">2065k</td>
083 * </tr>
084 * <tr>
085 * <td align="right">500k</td>
086 * <td align="right">4900k</td>
087 * <td align="right">2565k</td>
088 * </tr>
089 * <tr>
090 * <td align="right">600k</td>
091 * <td align="right">5800k</td>
092 * <td align="right">3065k</td>
093 * </tr>
094 * <tr>
095 * <td align="right">700k</td>
096 * <td align="right">6700k</td>
097 * <td align="right">3565k</td>
098 * </tr>
099 * <tr>
100 * <td align="right">800k</td>
101 * <td align="right">7600k</td>
102 * <td align="right">4065k</td>
103 * </tr>
104 * <tr>
105 * <td align="right">900k</td>
106 * <td align="right">8500k</td>
107 * <td align="right">4565k</td>
108 * </tr>
109 * </table>
110 *
111 * <p>
112 * For decompression {@code BZip2CompressorInputStream} allocates less memory if the
113 * bzipped input is smaller than one block.
114 * </p>
115 *
116 * <p>
117 * Instances of this class are not threadsafe.
118 * </p>
119 *
120 * <p>
121 * TODO: Update to BZip2 1.0.1
122 * </p>
123 * @NotThreadSafe
124 */
125public class BZip2CompressorOutputStream extends CompressorOutputStream
126    implements BZip2Constants {
127
128    /**
129     * The minimum supported blocksize {@code  == 1}.
130     */
131    public static final int MIN_BLOCKSIZE = 1;
132
133    /**
134     * The maximum supported blocksize {@code  == 9}.
135     */
136    public static final int MAX_BLOCKSIZE = 9;
137
138    private static final int GREATER_ICOST = 15;
139    private static final int LESSER_ICOST = 0;
140
141    private static void hbMakeCodeLengths(final byte[] len, final int[] freq,
142                                          final Data dat, final int alphaSize,
143                                          final int maxLen) {
144        /*
145         * Nodes and heap entries run from 1. Entry 0 for both the heap and
146         * nodes is a sentinel.
147         */
148        final int[] heap = dat.heap;
149        final int[] weight = dat.weight;
150        final int[] parent = dat.parent;
151
152        for (int i = alphaSize; --i >= 0;) {
153            weight[i + 1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
154        }
155
156        for (boolean tooLong = true; tooLong;) {
157            tooLong = false;
158
159            int nNodes = alphaSize;
160            int nHeap = 0;
161            heap[0] = 0;
162            weight[0] = 0;
163            parent[0] = -2;
164
165            for (int i = 1; i <= alphaSize; i++) {
166                parent[i] = -1;
167                nHeap++;
168                heap[nHeap] = i;
169
170                int zz = nHeap;
171                int tmp = heap[zz];
172                while (weight[tmp] < weight[heap[zz >> 1]]) {
173                    heap[zz] = heap[zz >> 1];
174                    zz >>= 1;
175                }
176                heap[zz] = tmp;
177            }
178
179            while (nHeap > 1) {
180                int n1 = heap[1];
181                heap[1] = heap[nHeap];
182                nHeap--;
183
184                int yy = 0;
185                int zz = 1;
186                int tmp = heap[1];
187
188                while (true) {
189                    yy = zz << 1;
190
191                    if (yy > nHeap) {
192                        break;
193                    }
194
195                    if ((yy < nHeap)
196                        && (weight[heap[yy + 1]] < weight[heap[yy]])) {
197                        yy++;
198                    }
199
200                    if (weight[tmp] < weight[heap[yy]]) {
201                        break;
202                    }
203
204                    heap[zz] = heap[yy];
205                    zz = yy;
206                }
207
208                heap[zz] = tmp;
209
210                int n2 = heap[1];
211                heap[1] = heap[nHeap];
212                nHeap--;
213
214                yy = 0;
215                zz = 1;
216                tmp = heap[1];
217
218                while (true) {
219                    yy = zz << 1;
220
221                    if (yy > nHeap) {
222                        break;
223                    }
224
225                    if ((yy < nHeap)
226                        && (weight[heap[yy + 1]] < weight[heap[yy]])) {
227                        yy++;
228                    }
229
230                    if (weight[tmp] < weight[heap[yy]]) {
231                        break;
232                    }
233
234                    heap[zz] = heap[yy];
235                    zz = yy;
236                }
237
238                heap[zz] = tmp;
239                nNodes++;
240                parent[n1] = parent[n2] = nNodes;
241
242                final int weight_n1 = weight[n1];
243                final int weight_n2 = weight[n2];
244                weight[nNodes] = ((weight_n1 & 0xffffff00)
245                                  + (weight_n2 & 0xffffff00))
246                    | (1 + (((weight_n1 & 0x000000ff)
247                             > (weight_n2 & 0x000000ff))
248                            ? (weight_n1 & 0x000000ff)
249                            : (weight_n2 & 0x000000ff)));
250
251                parent[nNodes] = -1;
252                nHeap++;
253                heap[nHeap] = nNodes;
254
255                tmp = 0;
256                zz = nHeap;
257                tmp = heap[zz];
258                final int weight_tmp = weight[tmp];
259                while (weight_tmp < weight[heap[zz >> 1]]) {
260                    heap[zz] = heap[zz >> 1];
261                    zz >>= 1;
262                }
263                heap[zz] = tmp;
264
265            }
266
267            for (int i = 1; i <= alphaSize; i++) {
268                int j = 0;
269                int k = i;
270
271                for (int parent_k; (parent_k = parent[k]) >= 0;) {
272                    k = parent_k;
273                    j++;
274                }
275
276                len[i - 1] = (byte) j;
277                if (j > maxLen) {
278                    tooLong = true;
279                }
280            }
281
282            if (tooLong) {
283                for (int i = 1; i < alphaSize; i++) {
284                    int j = weight[i] >> 8;
285                    j = 1 + (j >> 1);
286                    weight[i] = j << 8;
287                }
288            }
289        }
290    }
291
292    /**
293     * Index of the last char in the block, so the block size == last + 1.
294     */
295    private int last;
296
297    /**
298     * Always: in the range 0 .. 9. The current block size is 100000 * this
299     * number.
300     */
301    private final int blockSize100k;
302
303    private int bsBuff;
304    private int bsLive;
305    private final CRC crc = new CRC();
306
307    private int nInUse;
308
309    private int nMTF;
310
311    private int currentChar = -1;
312    private int runLength = 0;
313
314    private int blockCRC;
315    private int combinedCRC;
316    private final int allowableBlockSize;
317
318    /**
319     * All memory intensive stuff.
320     */
321    private Data data;
322    private BlockSort blockSorter;
323
324    private OutputStream out;
325
326    /**
327     * Chooses a blocksize based on the given length of the data to compress.
328     *
329     * @return The blocksize, between {@link #MIN_BLOCKSIZE} and
330     *         {@link #MAX_BLOCKSIZE} both inclusive. For a negative
331     *         {@code inputLength} this method returns {@code MAX_BLOCKSIZE}
332     *         always.
333     *
334     * @param inputLength
335     *            The length of the data which will be compressed by
336     *            {@code BZip2CompressorOutputStream}.
337     */
338    public static int chooseBlockSize(long inputLength) {
339        return (inputLength > 0) ? (int) Math
340            .min((inputLength / 132000) + 1, 9) : MAX_BLOCKSIZE;
341    }
342
343    /**
344     * Constructs a new {@code BZip2CompressorOutputStream} with a blocksize of 900k.
345     *
346     * @param out 
347     *            the destination stream.
348     *
349     * @throws IOException
350     *             if an I/O error occurs in the specified stream.
351     * @throws NullPointerException
352     *             if <code>out == null</code>.
353     */
354    public BZip2CompressorOutputStream(final OutputStream out)
355        throws IOException {
356        this(out, MAX_BLOCKSIZE);
357    }
358
359    /**
360     * Constructs a new {@code BZip2CompressorOutputStream} with specified blocksize.
361     *
362     * @param out
363     *            the destination stream.
364     * @param blockSize
365     *            the blockSize as 100k units.
366     *
367     * @throws IOException
368     *             if an I/O error occurs in the specified stream.
369     * @throws IllegalArgumentException
370     *             if <code>(blockSize &lt; 1) || (blockSize &gt; 9)</code>.
371     * @throws NullPointerException
372     *             if <code>out == null</code>.
373     *
374     * @see #MIN_BLOCKSIZE
375     * @see #MAX_BLOCKSIZE
376     */
377    public BZip2CompressorOutputStream(final OutputStream out, final int blockSize) throws IOException {
378        if (blockSize < 1) {
379            throw new IllegalArgumentException("blockSize(" + blockSize + ") < 1");
380        }
381        if (blockSize > 9) {
382            throw new IllegalArgumentException("blockSize(" + blockSize + ") > 9");
383        }
384
385        this.blockSize100k = blockSize;
386        this.out = out;
387
388        /* 20 is just a paranoia constant */
389        this.allowableBlockSize = (this.blockSize100k * BZip2Constants.BASEBLOCKSIZE) - 20;
390        init();
391    }
392
393    @Override
394    public void write(final int b) throws IOException {
395        if (this.out != null) {
396            write0(b);
397        } else {
398            throw new IOException("closed");
399        }
400    }
401
402    /**
403     * Writes the current byte to the buffer, run-length encoding it
404     * if it has been repeated at least four times (the first step
405     * RLEs sequences of four identical bytes).
406     *
407     * <p>Flushes the current block before writing data if it is
408     * full.</p>
409     *
410     * <p>"write to the buffer" means adding to data.buffer starting
411     * two steps "after" this.last - initially starting at index 1
412     * (not 0) - and updating this.last to point to the last index
413     * written minus 1.</p>
414     */
415    private void writeRun() throws IOException {
416        final int lastShadow = this.last;
417
418        if (lastShadow < this.allowableBlockSize) {
419            final int currentCharShadow = this.currentChar;
420            final Data dataShadow = this.data;
421            dataShadow.inUse[currentCharShadow] = true;
422            final byte ch = (byte) currentCharShadow;
423
424            int runLengthShadow = this.runLength;
425            this.crc.updateCRC(currentCharShadow, runLengthShadow);
426
427            switch (runLengthShadow) {
428            case 1:
429                dataShadow.block[lastShadow + 2] = ch;
430                this.last = lastShadow + 1;
431                break;
432
433            case 2:
434                dataShadow.block[lastShadow + 2] = ch;
435                dataShadow.block[lastShadow + 3] = ch;
436                this.last = lastShadow + 2;
437                break;
438
439            case 3: {
440                final byte[] block = dataShadow.block;
441                block[lastShadow + 2] = ch;
442                block[lastShadow + 3] = ch;
443                block[lastShadow + 4] = ch;
444                this.last = lastShadow + 3;
445            }
446                break;
447
448            default: {
449                runLengthShadow -= 4;
450                dataShadow.inUse[runLengthShadow] = true;
451                final byte[] block = dataShadow.block;
452                block[lastShadow + 2] = ch;
453                block[lastShadow + 3] = ch;
454                block[lastShadow + 4] = ch;
455                block[lastShadow + 5] = ch;
456                block[lastShadow + 6] = (byte) runLengthShadow;
457                this.last = lastShadow + 5;
458            }
459                break;
460
461            }
462        } else {
463            endBlock();
464            initBlock();
465            writeRun();
466        }
467    }
468
469    /**
470     * Overriden to close the stream.
471     */
472    @Override
473    protected void finalize() throws Throwable {
474        finish();
475        super.finalize();
476    }
477
478
479    public void finish() throws IOException {
480        if (out != null) {
481            try {
482                if (this.runLength > 0) {
483                    writeRun();
484                }
485                this.currentChar = -1;
486                endBlock();
487                endCompression();
488            } finally {
489                this.out = null;
490                this.data = null;
491                this.blockSorter = null;
492            }
493        }
494    }
495
496    @Override
497    public void close() throws IOException {
498        if (out != null) {
499            OutputStream outShadow = this.out;
500            finish();
501            outShadow.close();
502        }
503    }
504
505    @Override
506    public void flush() throws IOException {
507        OutputStream outShadow = this.out;
508        if (outShadow != null) {
509            outShadow.flush();
510        }
511    }
512
513    /**
514     * Writes magic bytes like BZ on the first position of the stream
515     * and bytes indiciating the file-format, which is 
516     * huffmanised, followed by a digit indicating blockSize100k.
517     * @throws IOException if the magic bytes could not been written
518     */
519    private void init() throws IOException {
520        bsPutUByte('B');
521        bsPutUByte('Z');
522
523        this.data = new Data(this.blockSize100k);
524        this.blockSorter = new BlockSort(this.data);
525
526        // huffmanised magic bytes
527        bsPutUByte('h');
528        bsPutUByte('0' + this.blockSize100k);
529
530        this.combinedCRC = 0;
531        initBlock();
532    }
533
534    private void initBlock() {
535        // blockNo++;
536        this.crc.initialiseCRC();
537        this.last = -1;
538        // ch = 0;
539
540        boolean[] inUse = this.data.inUse;
541        for (int i = 256; --i >= 0;) {
542            inUse[i] = false;
543        }
544
545    }
546
547    private void endBlock() throws IOException {
548        this.blockCRC = this.crc.getFinalCRC();
549        this.combinedCRC = (this.combinedCRC << 1) | (this.combinedCRC >>> 31);
550        this.combinedCRC ^= this.blockCRC;
551
552        // empty block at end of file
553        if (this.last == -1) {
554            return;
555        }
556
557        /* sort the block and establish posn of original string */
558        blockSort();
559
560        /*
561         * A 6-byte block header, the value chosen arbitrarily as 0x314159265359
562         * :-). A 32 bit value does not really give a strong enough guarantee
563         * that the value will not appear by chance in the compressed
564         * datastream. Worst-case probability of this event, for a 900k block,
565         * is about 2.0e-3 for 32 bits, 1.0e-5 for 40 bits and 4.0e-8 for 48
566         * bits. For a compressed file of size 100Gb -- about 100000 blocks --
567         * only a 48-bit marker will do. NB: normal compression/ decompression
568         * donot rely on these statistical properties. They are only important
569         * when trying to recover blocks from damaged files.
570         */
571        bsPutUByte(0x31);
572        bsPutUByte(0x41);
573        bsPutUByte(0x59);
574        bsPutUByte(0x26);
575        bsPutUByte(0x53);
576        bsPutUByte(0x59);
577
578        /* Now the block's CRC, so it is in a known place. */
579        bsPutInt(this.blockCRC);
580
581        /* Now a single bit indicating no randomisation. */
582        bsW(1, 0);
583
584        /* Finally, block's contents proper. */
585        moveToFrontCodeAndSend();
586    }
587
588    private void endCompression() throws IOException {
589        /*
590         * Now another magic 48-bit number, 0x177245385090, to indicate the end
591         * of the last block. (sqrt(pi), if you want to know. I did want to use
592         * e, but it contains too much repetition -- 27 18 28 18 28 46 -- for me
593         * to feel statistically comfortable. Call me paranoid.)
594         */
595        bsPutUByte(0x17);
596        bsPutUByte(0x72);
597        bsPutUByte(0x45);
598        bsPutUByte(0x38);
599        bsPutUByte(0x50);
600        bsPutUByte(0x90);
601
602        bsPutInt(this.combinedCRC);
603        bsFinishedWithStream();
604    }
605
606    /**
607     * Returns the blocksize parameter specified at construction time.
608     * @return the blocksize parameter specified at construction time
609     */
610    public final int getBlockSize() {
611        return this.blockSize100k;
612    }
613
614    @Override
615    public void write(final byte[] buf, int offs, final int len)
616        throws IOException {
617        if (offs < 0) {
618            throw new IndexOutOfBoundsException("offs(" + offs + ") < 0.");
619        }
620        if (len < 0) {
621            throw new IndexOutOfBoundsException("len(" + len + ") < 0.");
622        }
623        if (offs + len > buf.length) {
624            throw new IndexOutOfBoundsException("offs(" + offs + ") + len("
625                                                + len + ") > buf.length("
626                                                + buf.length + ").");
627        }
628        if (this.out == null) {
629            throw new IOException("stream closed");
630        }
631
632        for (int hi = offs + len; offs < hi;) {
633            write0(buf[offs++]);
634        }
635    }
636
637    /**
638     * Keeps track of the last bytes written and implicitly performs
639     * run-length encoding as the first step of the bzip2 algorithm.
640     */
641    private void write0(int b) throws IOException {
642        if (this.currentChar != -1) {
643            b &= 0xff;
644            if (this.currentChar == b) {
645                if (++this.runLength > 254) {
646                    writeRun();
647                    this.currentChar = -1;
648                    this.runLength = 0;
649                }
650                // else nothing to do
651            } else {
652                writeRun();
653                this.runLength = 1;
654                this.currentChar = b;
655            }
656        } else {
657            this.currentChar = b & 0xff;
658            this.runLength++;
659        }
660    }
661
662    private static void hbAssignCodes(final int[] code, final byte[] length,
663                                      final int minLen, final int maxLen,
664                                      final int alphaSize) {
665        int vec = 0;
666        for (int n = minLen; n <= maxLen; n++) {
667            for (int i = 0; i < alphaSize; i++) {
668                if ((length[i] & 0xff) == n) {
669                    code[i] = vec;
670                    vec++;
671                }
672            }
673            vec <<= 1;
674        }
675    }
676
677    private void bsFinishedWithStream() throws IOException {
678        while (this.bsLive > 0) {
679            int ch = this.bsBuff >> 24;
680            this.out.write(ch); // write 8-bit
681            this.bsBuff <<= 8;
682            this.bsLive -= 8;
683        }
684    }
685
686    private void bsW(final int n, final int v) throws IOException {
687        final OutputStream outShadow = this.out;
688        int bsLiveShadow = this.bsLive;
689        int bsBuffShadow = this.bsBuff;
690
691        while (bsLiveShadow >= 8) {
692            outShadow.write(bsBuffShadow >> 24); // write 8-bit
693            bsBuffShadow <<= 8;
694            bsLiveShadow -= 8;
695        }
696
697        this.bsBuff = bsBuffShadow | (v << (32 - bsLiveShadow - n));
698        this.bsLive = bsLiveShadow + n;
699    }
700
701    private void bsPutUByte(final int c) throws IOException {
702        bsW(8, c);
703    }
704
705    private void bsPutInt(final int u) throws IOException {
706        bsW(8, (u >> 24) & 0xff);
707        bsW(8, (u >> 16) & 0xff);
708        bsW(8, (u >> 8) & 0xff);
709        bsW(8, u & 0xff);
710    }
711
712    private void sendMTFValues() throws IOException {
713        final byte[][] len = this.data.sendMTFValues_len;
714        final int alphaSize = this.nInUse + 2;
715
716        for (int t = N_GROUPS; --t >= 0;) {
717            byte[] len_t = len[t];
718            for (int v = alphaSize; --v >= 0;) {
719                len_t[v] = GREATER_ICOST;
720            }
721        }
722
723        /* Decide how many coding tables to use */
724        // assert (this.nMTF > 0) : this.nMTF;
725        final int nGroups = (this.nMTF < 200) ? 2 : (this.nMTF < 600) ? 3
726            : (this.nMTF < 1200) ? 4 : (this.nMTF < 2400) ? 5 : 6;
727
728        /* Generate an initial set of coding tables */
729        sendMTFValues0(nGroups, alphaSize);
730
731        /*
732         * Iterate up to N_ITERS times to improve the tables.
733         */
734        final int nSelectors = sendMTFValues1(nGroups, alphaSize);
735
736        /* Compute MTF values for the selectors. */
737        sendMTFValues2(nGroups, nSelectors);
738
739        /* Assign actual codes for the tables. */
740        sendMTFValues3(nGroups, alphaSize);
741
742        /* Transmit the mapping table. */
743        sendMTFValues4();
744
745        /* Now the selectors. */
746        sendMTFValues5(nGroups, nSelectors);
747
748        /* Now the coding tables. */
749        sendMTFValues6(nGroups, alphaSize);
750
751        /* And finally, the block data proper */
752        sendMTFValues7();
753    }
754
755    private void sendMTFValues0(final int nGroups, final int alphaSize) {
756        final byte[][] len = this.data.sendMTFValues_len;
757        final int[] mtfFreq = this.data.mtfFreq;
758
759        int remF = this.nMTF;
760        int gs = 0;
761
762        for (int nPart = nGroups; nPart > 0; nPart--) {
763            final int tFreq = remF / nPart;
764            int ge = gs - 1;
765            int aFreq = 0;
766
767            for (final int a = alphaSize - 1; (aFreq < tFreq) && (ge < a);) {
768                aFreq += mtfFreq[++ge];
769            }
770
771            if ((ge > gs) && (nPart != nGroups) && (nPart != 1)
772                && (((nGroups - nPart) & 1) != 0)) {
773                aFreq -= mtfFreq[ge--];
774            }
775
776            final byte[] len_np = len[nPart - 1];
777            for (int v = alphaSize; --v >= 0;) {
778                if ((v >= gs) && (v <= ge)) {
779                    len_np[v] = LESSER_ICOST;
780                } else {
781                    len_np[v] = GREATER_ICOST;
782                }
783            }
784
785            gs = ge + 1;
786            remF -= aFreq;
787        }
788    }
789
790    private int sendMTFValues1(final int nGroups, final int alphaSize) {
791        final Data dataShadow = this.data;
792        final int[][] rfreq = dataShadow.sendMTFValues_rfreq;
793        final int[] fave = dataShadow.sendMTFValues_fave;
794        final short[] cost = dataShadow.sendMTFValues_cost;
795        final char[] sfmap = dataShadow.sfmap;
796        final byte[] selector = dataShadow.selector;
797        final byte[][] len = dataShadow.sendMTFValues_len;
798        final byte[] len_0 = len[0];
799        final byte[] len_1 = len[1];
800        final byte[] len_2 = len[2];
801        final byte[] len_3 = len[3];
802        final byte[] len_4 = len[4];
803        final byte[] len_5 = len[5];
804        final int nMTFShadow = this.nMTF;
805
806        int nSelectors = 0;
807
808        for (int iter = 0; iter < N_ITERS; iter++) {
809            for (int t = nGroups; --t >= 0;) {
810                fave[t] = 0;
811                int[] rfreqt = rfreq[t];
812                for (int i = alphaSize; --i >= 0;) {
813                    rfreqt[i] = 0;
814                }
815            }
816
817            nSelectors = 0;
818
819            for (int gs = 0; gs < this.nMTF;) {
820                /* Set group start & end marks. */
821
822                /*
823                 * Calculate the cost of this group as coded by each of the
824                 * coding tables.
825                 */
826
827                final int ge = Math.min(gs + G_SIZE - 1, nMTFShadow - 1);
828
829                if (nGroups == N_GROUPS) {
830                    // unrolled version of the else-block
831
832                    short cost0 = 0;
833                    short cost1 = 0;
834                    short cost2 = 0;
835                    short cost3 = 0;
836                    short cost4 = 0;
837                    short cost5 = 0;
838
839                    for (int i = gs; i <= ge; i++) {
840                        final int icv = sfmap[i];
841                        cost0 += len_0[icv] & 0xff;
842                        cost1 += len_1[icv] & 0xff;
843                        cost2 += len_2[icv] & 0xff;
844                        cost3 += len_3[icv] & 0xff;
845                        cost4 += len_4[icv] & 0xff;
846                        cost5 += len_5[icv] & 0xff;
847                    }
848
849                    cost[0] = cost0;
850                    cost[1] = cost1;
851                    cost[2] = cost2;
852                    cost[3] = cost3;
853                    cost[4] = cost4;
854                    cost[5] = cost5;
855
856                } else {
857                    for (int t = nGroups; --t >= 0;) {
858                        cost[t] = 0;
859                    }
860
861                    for (int i = gs; i <= ge; i++) {
862                        final int icv = sfmap[i];
863                        for (int t = nGroups; --t >= 0;) {
864                            cost[t] += len[t][icv] & 0xff;
865                        }
866                    }
867                }
868
869                /*
870                 * Find the coding table which is best for this group, and
871                 * record its identity in the selector table.
872                 */
873                int bt = -1;
874                for (int t = nGroups, bc = 999999999; --t >= 0;) {
875                    final int cost_t = cost[t];
876                    if (cost_t < bc) {
877                        bc = cost_t;
878                        bt = t;
879                    }
880                }
881
882                fave[bt]++;
883                selector[nSelectors] = (byte) bt;
884                nSelectors++;
885
886                /*
887                 * Increment the symbol frequencies for the selected table.
888                 */
889                final int[] rfreq_bt = rfreq[bt];
890                for (int i = gs; i <= ge; i++) {
891                    rfreq_bt[sfmap[i]]++;
892                }
893
894                gs = ge + 1;
895            }
896
897            /*
898             * Recompute the tables based on the accumulated frequencies.
899             */
900            for (int t = 0; t < nGroups; t++) {
901                hbMakeCodeLengths(len[t], rfreq[t], this.data, alphaSize, 20);
902            }
903        }
904
905        return nSelectors;
906    }
907
908    private void sendMTFValues2(final int nGroups, final int nSelectors) {
909        // assert (nGroups < 8) : nGroups;
910
911        final Data dataShadow = this.data;
912        byte[] pos = dataShadow.sendMTFValues2_pos;
913
914        for (int i = nGroups; --i >= 0;) {
915            pos[i] = (byte) i;
916        }
917
918        for (int i = 0; i < nSelectors; i++) {
919            final byte ll_i = dataShadow.selector[i];
920            byte tmp = pos[0];
921            int j = 0;
922
923            while (ll_i != tmp) {
924                j++;
925                byte tmp2 = tmp;
926                tmp = pos[j];
927                pos[j] = tmp2;
928            }
929
930            pos[0] = tmp;
931            dataShadow.selectorMtf[i] = (byte) j;
932        }
933    }
934
935    private void sendMTFValues3(final int nGroups, final int alphaSize) {
936        int[][] code = this.data.sendMTFValues_code;
937        byte[][] len = this.data.sendMTFValues_len;
938
939        for (int t = 0; t < nGroups; t++) {
940            int minLen = 32;
941            int maxLen = 0;
942            final byte[] len_t = len[t];
943            for (int i = alphaSize; --i >= 0;) {
944                final int l = len_t[i] & 0xff;
945                if (l > maxLen) {
946                    maxLen = l;
947                }
948                if (l < minLen) {
949                    minLen = l;
950                }
951            }
952
953            // assert (maxLen <= 20) : maxLen;
954            // assert (minLen >= 1) : minLen;
955
956            hbAssignCodes(code[t], len[t], minLen, maxLen, alphaSize);
957        }
958    }
959
960    private void sendMTFValues4() throws IOException {
961        final boolean[] inUse = this.data.inUse;
962        final boolean[] inUse16 = this.data.sentMTFValues4_inUse16;
963
964        for (int i = 16; --i >= 0;) {
965            inUse16[i] = false;
966            final int i16 = i * 16;
967            for (int j = 16; --j >= 0;) {
968                if (inUse[i16 + j]) {
969                    inUse16[i] = true;
970                }
971            }
972        }
973
974        for (int i = 0; i < 16; i++) {
975            bsW(1, inUse16[i] ? 1 : 0);
976        }
977
978        final OutputStream outShadow = this.out;
979        int bsLiveShadow = this.bsLive;
980        int bsBuffShadow = this.bsBuff;
981
982        for (int i = 0; i < 16; i++) {
983            if (inUse16[i]) {
984                final int i16 = i * 16;
985                for (int j = 0; j < 16; j++) {
986                    // inlined: bsW(1, inUse[i16 + j] ? 1 : 0);
987                    while (bsLiveShadow >= 8) {
988                        outShadow.write(bsBuffShadow >> 24); // write 8-bit
989                        bsBuffShadow <<= 8;
990                        bsLiveShadow -= 8;
991                    }
992                    if (inUse[i16 + j]) {
993                        bsBuffShadow |= 1 << (32 - bsLiveShadow - 1);
994                    }
995                    bsLiveShadow++;
996                }
997            }
998        }
999
1000        this.bsBuff = bsBuffShadow;
1001        this.bsLive = bsLiveShadow;
1002    }
1003
1004    private void sendMTFValues5(final int nGroups, final int nSelectors)
1005        throws IOException {
1006        bsW(3, nGroups);
1007        bsW(15, nSelectors);
1008
1009        final OutputStream outShadow = this.out;
1010        final byte[] selectorMtf = this.data.selectorMtf;
1011
1012        int bsLiveShadow = this.bsLive;
1013        int bsBuffShadow = this.bsBuff;
1014
1015        for (int i = 0; i < nSelectors; i++) {
1016            for (int j = 0, hj = selectorMtf[i] & 0xff; j < hj; j++) {
1017                // inlined: bsW(1, 1);
1018                while (bsLiveShadow >= 8) {
1019                    outShadow.write(bsBuffShadow >> 24);
1020                    bsBuffShadow <<= 8;
1021                    bsLiveShadow -= 8;
1022                }
1023                bsBuffShadow |= 1 << (32 - bsLiveShadow - 1);
1024                bsLiveShadow++;
1025            }
1026
1027            // inlined: bsW(1, 0);
1028            while (bsLiveShadow >= 8) {
1029                outShadow.write(bsBuffShadow >> 24);
1030                bsBuffShadow <<= 8;
1031                bsLiveShadow -= 8;
1032            }
1033            // bsBuffShadow |= 0 << (32 - bsLiveShadow - 1);
1034            bsLiveShadow++;
1035        }
1036
1037        this.bsBuff = bsBuffShadow;
1038        this.bsLive = bsLiveShadow;
1039    }
1040
1041    private void sendMTFValues6(final int nGroups, final int alphaSize)
1042        throws IOException {
1043        final byte[][] len = this.data.sendMTFValues_len;
1044        final OutputStream outShadow = this.out;
1045
1046        int bsLiveShadow = this.bsLive;
1047        int bsBuffShadow = this.bsBuff;
1048
1049        for (int t = 0; t < nGroups; t++) {
1050            byte[] len_t = len[t];
1051            int curr = len_t[0] & 0xff;
1052
1053            // inlined: bsW(5, curr);
1054            while (bsLiveShadow >= 8) {
1055                outShadow.write(bsBuffShadow >> 24); // write 8-bit
1056                bsBuffShadow <<= 8;
1057                bsLiveShadow -= 8;
1058            }
1059            bsBuffShadow |= curr << (32 - bsLiveShadow - 5);
1060            bsLiveShadow += 5;
1061
1062            for (int i = 0; i < alphaSize; i++) {
1063                int lti = len_t[i] & 0xff;
1064                while (curr < lti) {
1065                    // inlined: bsW(2, 2);
1066                    while (bsLiveShadow >= 8) {
1067                        outShadow.write(bsBuffShadow >> 24); // write 8-bit
1068                        bsBuffShadow <<= 8;
1069                        bsLiveShadow -= 8;
1070                    }
1071                    bsBuffShadow |= 2 << (32 - bsLiveShadow - 2);
1072                    bsLiveShadow += 2;
1073
1074                    curr++; /* 10 */
1075                }
1076
1077                while (curr > lti) {
1078                    // inlined: bsW(2, 3);
1079                    while (bsLiveShadow >= 8) {
1080                        outShadow.write(bsBuffShadow >> 24); // write 8-bit
1081                        bsBuffShadow <<= 8;
1082                        bsLiveShadow -= 8;
1083                    }
1084                    bsBuffShadow |= 3 << (32 - bsLiveShadow - 2);
1085                    bsLiveShadow += 2;
1086
1087                    curr--; /* 11 */
1088                }
1089
1090                // inlined: bsW(1, 0);
1091                while (bsLiveShadow >= 8) {
1092                    outShadow.write(bsBuffShadow >> 24); // write 8-bit
1093                    bsBuffShadow <<= 8;
1094                    bsLiveShadow -= 8;
1095                }
1096                // bsBuffShadow |= 0 << (32 - bsLiveShadow - 1);
1097                bsLiveShadow++;
1098            }
1099        }
1100
1101        this.bsBuff = bsBuffShadow;
1102        this.bsLive = bsLiveShadow;
1103    }
1104
1105    private void sendMTFValues7() throws IOException {
1106        final Data dataShadow = this.data;
1107        final byte[][] len = dataShadow.sendMTFValues_len;
1108        final int[][] code = dataShadow.sendMTFValues_code;
1109        final OutputStream outShadow = this.out;
1110        final byte[] selector = dataShadow.selector;
1111        final char[] sfmap = dataShadow.sfmap;
1112        final int nMTFShadow = this.nMTF;
1113
1114        int selCtr = 0;
1115
1116        int bsLiveShadow = this.bsLive;
1117        int bsBuffShadow = this.bsBuff;
1118
1119        for (int gs = 0; gs < nMTFShadow;) {
1120            final int ge = Math.min(gs + G_SIZE - 1, nMTFShadow - 1);
1121            final int selector_selCtr = selector[selCtr] & 0xff;
1122            final int[] code_selCtr = code[selector_selCtr];
1123            final byte[] len_selCtr = len[selector_selCtr];
1124
1125            while (gs <= ge) {
1126                final int sfmap_i = sfmap[gs];
1127
1128                //
1129                // inlined: bsW(len_selCtr[sfmap_i] & 0xff,
1130                // code_selCtr[sfmap_i]);
1131                //
1132                while (bsLiveShadow >= 8) {
1133                    outShadow.write(bsBuffShadow >> 24);
1134                    bsBuffShadow <<= 8;
1135                    bsLiveShadow -= 8;
1136                }
1137                final int n = len_selCtr[sfmap_i] & 0xFF;
1138                bsBuffShadow |= code_selCtr[sfmap_i] << (32 - bsLiveShadow - n);
1139                bsLiveShadow += n;
1140
1141                gs++;
1142            }
1143
1144            gs = ge + 1;
1145            selCtr++;
1146        }
1147
1148        this.bsBuff = bsBuffShadow;
1149        this.bsLive = bsLiveShadow;
1150    }
1151
1152    private void moveToFrontCodeAndSend() throws IOException {
1153        bsW(24, this.data.origPtr);
1154        generateMTFValues();
1155        sendMTFValues();
1156    }
1157
1158    private void blockSort() {
1159        blockSorter.blockSort(data, last);
1160    }
1161
1162    /*
1163     * Performs Move-To-Front on the Burrows-Wheeler transformed
1164     * buffer, storing the MTFed data in data.sfmap in RUNA/RUNB
1165     * run-length-encoded form.
1166     *
1167     * <p>Keeps track of byte frequencies in data.mtfFreq at the same time.</p>
1168     */
1169    private void generateMTFValues() {
1170        final int lastShadow = this.last;
1171        final Data dataShadow = this.data;
1172        final boolean[] inUse = dataShadow.inUse;
1173        final byte[] block = dataShadow.block;
1174        final int[] fmap = dataShadow.fmap;
1175        final char[] sfmap = dataShadow.sfmap;
1176        final int[] mtfFreq = dataShadow.mtfFreq;
1177        final byte[] unseqToSeq = dataShadow.unseqToSeq;
1178        final byte[] yy = dataShadow.generateMTFValues_yy;
1179
1180        // make maps
1181        int nInUseShadow = 0;
1182        for (int i = 0; i < 256; i++) {
1183            if (inUse[i]) {
1184                unseqToSeq[i] = (byte) nInUseShadow;
1185                nInUseShadow++;
1186            }
1187        }
1188        this.nInUse = nInUseShadow;
1189
1190        final int eob = nInUseShadow + 1;
1191
1192        for (int i = eob; i >= 0; i--) {
1193            mtfFreq[i] = 0;
1194        }
1195
1196        for (int i = nInUseShadow; --i >= 0;) {
1197            yy[i] = (byte) i;
1198        }
1199
1200        int wr = 0;
1201        int zPend = 0;
1202
1203        for (int i = 0; i <= lastShadow; i++) {
1204            final byte ll_i = unseqToSeq[block[fmap[i]] & 0xff];
1205            byte tmp = yy[0];
1206            int j = 0;
1207
1208            while (ll_i != tmp) {
1209                j++;
1210                byte tmp2 = tmp;
1211                tmp = yy[j];
1212                yy[j] = tmp2;
1213            }
1214            yy[0] = tmp;
1215
1216            if (j == 0) {
1217                zPend++;
1218            } else {
1219                if (zPend > 0) {
1220                    zPend--;
1221                    while (true) {
1222                        if ((zPend & 1) == 0) {
1223                            sfmap[wr] = RUNA;
1224                            wr++;
1225                            mtfFreq[RUNA]++;
1226                        } else {
1227                            sfmap[wr] = RUNB;
1228                            wr++;
1229                            mtfFreq[RUNB]++;
1230                        }
1231
1232                        if (zPend >= 2) {
1233                            zPend = (zPend - 2) >> 1;
1234                        } else {
1235                            break;
1236                        }
1237                    }
1238                    zPend = 0;
1239                }
1240                sfmap[wr] = (char) (j + 1);
1241                wr++;
1242                mtfFreq[j + 1]++;
1243            }
1244        }
1245
1246        if (zPend > 0) {
1247            zPend--;
1248            while (true) {
1249                if ((zPend & 1) == 0) {
1250                    sfmap[wr] = RUNA;
1251                    wr++;
1252                    mtfFreq[RUNA]++;
1253                } else {
1254                    sfmap[wr] = RUNB;
1255                    wr++;
1256                    mtfFreq[RUNB]++;
1257                }
1258
1259                if (zPend >= 2) {
1260                    zPend = (zPend - 2) >> 1;
1261                } else {
1262                    break;
1263                }
1264            }
1265        }
1266
1267        sfmap[wr] = (char) eob;
1268        mtfFreq[eob]++;
1269        this.nMTF = wr + 1;
1270    }
1271
1272    static final class Data {
1273
1274        // with blockSize 900k
1275        /* maps unsigned byte => "does it occur in block" */
1276        final boolean[] inUse = new boolean[256]; // 256 byte
1277        final byte[] unseqToSeq = new byte[256]; // 256 byte
1278        final int[] mtfFreq = new int[MAX_ALPHA_SIZE]; // 1032 byte
1279        final byte[] selector = new byte[MAX_SELECTORS]; // 18002 byte
1280        final byte[] selectorMtf = new byte[MAX_SELECTORS]; // 18002 byte
1281
1282        final byte[] generateMTFValues_yy = new byte[256]; // 256 byte
1283        final byte[][] sendMTFValues_len = new byte[N_GROUPS][MAX_ALPHA_SIZE]; // 1548
1284        // byte
1285        final int[][] sendMTFValues_rfreq = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192
1286        // byte
1287        final int[] sendMTFValues_fave = new int[N_GROUPS]; // 24 byte
1288        final short[] sendMTFValues_cost = new short[N_GROUPS]; // 12 byte
1289        final int[][] sendMTFValues_code = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192
1290        // byte
1291        final byte[] sendMTFValues2_pos = new byte[N_GROUPS]; // 6 byte
1292        final boolean[] sentMTFValues4_inUse16 = new boolean[16]; // 16 byte
1293
1294        final int[] heap = new int[MAX_ALPHA_SIZE + 2]; // 1040 byte
1295        final int[] weight = new int[MAX_ALPHA_SIZE * 2]; // 2064 byte
1296        final int[] parent = new int[MAX_ALPHA_SIZE * 2]; // 2064 byte
1297
1298        // ------------
1299        // 333408 byte
1300
1301        /* holds the RLEd block of original data starting at index 1.
1302         * After sorting the last byte added to the buffer is at index
1303         * 0. */
1304        final byte[] block; // 900021 byte
1305        /* maps index in Burrows-Wheeler transformed block => index of
1306         * byte in original block */
1307        final int[] fmap; // 3600000 byte
1308        final char[] sfmap; // 3600000 byte
1309        // ------------
1310        // 8433529 byte
1311        // ============
1312
1313        /**
1314         * Index of original line in Burrows-Wheeler table.
1315         *
1316         * <p>This is the index in fmap that points to the last byte
1317         * of the original data.</p>
1318         */
1319        int origPtr;
1320
1321        Data(int blockSize100k) {
1322            final int n = blockSize100k * BZip2Constants.BASEBLOCKSIZE;
1323            this.block = new byte[(n + 1 + NUM_OVERSHOOT_BYTES)];
1324            this.fmap = new int[n];
1325            this.sfmap = new char[2 * n];
1326        }
1327
1328    }
1329
1330}