/jEdit/tags/jedit-4-5-pre1/installer/CBZip2OutputStream.java
Java | 2021 lines | 1598 code | 194 blank | 229 comment | 221 complexity | a6ae056b76b39de736a770479bb8c89b MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
Large files files are truncated, but you can click here to view the full file
- /*
- * Copyright (C) The Apache Software Foundation. All rights reserved.
- *
- * This software is published under the terms of the Apache Software License
- * version 1.1, a copy of which has been included with this distribution in
- * the LICENSE.txt file.
- */
- package installer;
- import java.io.IOException;
- import java.io.OutputStream;
- /**
- * An output stream that compresses into the BZip2 format (without the file
- * header chars) into another stream. TODO: Update to BZip2 1.0.1
- *
- * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
- */
- public class CBZip2OutputStream
- extends OutputStream
- implements BZip2Constants
- {
- private static final int LOWER_BYTE_MASK = 0x000000ff;
- private static final int UPPER_BYTE_MASK = 0xffffff00;
- private static final int SETMASK = ( 1 << 21 );
- private static final int CLEARMASK = ( ~SETMASK );
- private static final int GREATER_ICOST = 15;
- private static final int LESSER_ICOST = 0;
- private static final int SMALL_THRESH = 20;
- private static final int DEPTH_THRESH = 10;
- /*
- * If you are ever unlucky/improbable enough
- * to get a stack overflow whilst sorting,
- * increase the following constant and try
- * again. In practice I have never seen the
- * stack go above 27 elems, so the following
- * limit seems very generous.
- */
- private static final int QSORT_STACK_SIZE = 1000;
- private CRC m_crc = new CRC();
- private boolean[] m_inUse = new boolean[ 256 ];
- private char[] m_seqToUnseq = new char[ 256 ];
- private char[] m_unseqToSeq = new char[ 256 ];
- private char[] m_selector = new char[ MAX_SELECTORS ];
- private char[] m_selectorMtf = new char[ MAX_SELECTORS ];
- private int[] m_mtfFreq = new int[ MAX_ALPHA_SIZE ];
- private int m_currentChar = -1;
- private int m_runLength;
- private boolean m_closed;
- /*
- * 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[] m_incs = new int[]
- {
- 1, 4, 13, 40, 121, 364, 1093, 3280,
- 9841, 29524, 88573, 265720,
- 797161, 2391484
- };
- private boolean m_blockRandomised;
- /*
- * always: in the range 0 .. 9.
- * The current block size is 100000 * this number.
- */
- private int m_blockSize100k;
- private int m_bsBuff;
- private int m_bsLive;
- /*
- * index of the last char in the block, so
- * the block size == last + 1.
- */
- private int m_last;
- /*
- * index in zptr[] of original string after sorting.
- */
- private int m_origPtr;
- private int m_allowableBlockSize;
- private char[] m_block;
- private int m_blockCRC;
- private int m_combinedCRC;
- private OutputStream m_bsStream;
- private boolean m_firstAttempt;
- private int[] m_ftab;
- private int m_nInUse;
- private int m_nMTF;
- private int[] m_quadrant;
- private short[] m_szptr;
- private int m_workDone;
- /*
- * Used when sorting. If too many long comparisons
- * happen, we stop sorting, randomise the block
- * slightly, and try again.
- */
- private int m_workFactor;
- private int m_workLimit;
- private int[] m_zptr;
- public CBZip2OutputStream( final OutputStream output )
- throws IOException
- {
- this( output, 9 );
- }
- public CBZip2OutputStream( final OutputStream output, final int blockSize )
- throws IOException
- {
- bsSetStream( output );
- m_workFactor = 50;
- int outBlockSize = blockSize;
- if( outBlockSize > 9 )
- {
- outBlockSize = 9;
- }
- if( outBlockSize < 1 )
- {
- outBlockSize = 1;
- }
- m_blockSize100k = outBlockSize;
- allocateCompressStructures();
- initialize();
- initBlock();
- }
- private static void hbMakeCodeLengths( char[] len, int[] freq,
- int alphaSize, int maxLen )
- {
- /*
- * Nodes and heap entries run from 1. Entry 0
- * for both the heap and nodes is a sentinel.
- */
- int nNodes;
- /*
- * Nodes and heap entries run from 1. Entry 0
- * for both the heap and nodes is a sentinel.
- */
- int nHeap;
- /*
- * Nodes and heap entries run from 1. Entry 0
- * for both the heap and nodes is a sentinel.
- */
- int n1;
- /*
- * Nodes and heap entries run from 1. Entry 0
- * for both the heap and nodes is a sentinel.
- */
- int n2;
- /*
- * Nodes and heap entries run from 1. Entry 0
- * for both the heap and nodes is a sentinel.
- */
- int i;
- /*
- * Nodes and heap entries run from 1. Entry 0
- * for both the heap and nodes is a sentinel.
- */
- int j;
- /*
- * Nodes and heap entries run from 1. Entry 0
- * for both the heap and nodes is a sentinel.
- */
- int k;
- boolean tooLong;
- int[] heap = new int[ MAX_ALPHA_SIZE + 2 ];
- int[] weights = new int[ MAX_ALPHA_SIZE * 2 ];
- int[] parent = new int[ MAX_ALPHA_SIZE * 2 ];
- for( i = 0; i < alphaSize; i++ )
- {
- weights[ i + 1 ] = ( freq[ i ] == 0 ? 1 : freq[ i ] ) << 8;
- }
- while( true )
- {
- nNodes = alphaSize;
- nHeap = 0;
- heap[ 0 ] = 0;
- weights[ 0 ] = 0;
- parent[ 0 ] = -2;
- for( i = 1; i <= alphaSize; i++ )
- {
- parent[ i ] = -1;
- nHeap++;
- heap[ nHeap ] = i;
- {
- int zz;
- int tmp;
- zz = nHeap;
- tmp = heap[ zz ];
- while( weights[ tmp ] < weights[ heap[ zz >> 1 ] ] )
- {
- heap[ zz ] = heap[ zz >> 1 ];
- zz >>= 1;
- }
- heap[ zz ] = tmp;
- }
- }
- if( !( nHeap < ( MAX_ALPHA_SIZE + 2 ) ) )
- {
- panic();
- }
- while( nHeap > 1 )
- {
- n1 = heap[ 1 ];
- heap[ 1 ] = heap[ nHeap ];
- nHeap--;
- {
- int zz = 0;
- int yy = 0;
- int tmp = 0;
- zz = 1;
- tmp = heap[ zz ];
- while( true )
- {
- yy = zz << 1;
- if( yy > nHeap )
- {
- break;
- }
- if( yy < nHeap &&
- weights[ heap[ yy + 1 ] ] < weights[ heap[ yy ] ] )
- {
-