FileDocCategorySizeDatePackage
DeflateTest.javaAPI DocAndroid 1.5 API6183Wed May 06 22:42:02 BST 2009android.core

DeflateTest

public class DeflateTest extends TestCase

Fields Summary
private static final int
LOCAL_BUF_SIZE
Constructors Summary
Methods Summary
private voidbigTest(int step, int expectedAdler)

        byte[] input = new byte[128 * 1024];
        byte[] comp = new byte[128 * 1024 + 512];
        byte[] output = new byte[128 * 1024 + 512];
        Inflater inflater = new Inflater(false);
        Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, false);

        createSample(input, step);

        compress(deflater, input, comp);
        expand(inflater, comp, (int) deflater.getBytesWritten(), output);

        assertEquals(inflater.getBytesWritten(), input.length);
        assertEquals(deflater.getAdler(), inflater.getAdler());
        assertEquals(deflater.getAdler(), expectedAdler);
    
private voidcompress(java.util.zip.Deflater deflater, byte[] inBuf, byte[] outBuf)


    /*
     * Compress all data in "in" to "out".  We use a small window on input
     * and output to exercise that part of the code.
     *
     * It's the caller's responsibility to ensure that "out" has enough
     * space.
     */
            
        int inCount = inBuf.length;        // use all
        int inPosn;
        int outPosn;

        inPosn = outPosn = 0;

        //System.out.println("### starting compress");

        while (!deflater.finished()) {
            int want = -1, got;

            // only read if the input buffer is empty
            if (deflater.needsInput() && inCount != 0) {
                want = (inCount < LOCAL_BUF_SIZE) ? inCount : LOCAL_BUF_SIZE;

                deflater.setInput(inBuf, inPosn, want);

                inCount -= want;
                inPosn += want;
                if (inCount == 0) {
                    deflater.finish();
                }
            }

            // deflate to current position in output buffer
            int compCount;

            compCount = deflater.deflate(outBuf, outPosn, LOCAL_BUF_SIZE);
            outPosn += compCount;

            //System.out.println("Compressed " + want + ", output " + compCount);
        }
    
private voidcreateSample(byte[] sample, int stepStep)

        byte val, step;
        int i, j, offset;

        assertTrue(sample.length >= 128 * 1024);

        val = 0;
        step = 1;
        offset = 0;
        for (i = 0; i < (128 * 1024) / 256; i++) {
            for (j = 0; j < 256; j++) {
                sample[offset++] = val;
                val += step;
            }

            step += stepStep;
        }
    
private voidexpand(java.util.zip.Inflater inflater, byte[] inBuf, int inCount, byte[] outBuf)

        int inPosn;
        int outPosn;

        inPosn = outPosn = 0;

        //System.out.println("### starting expand, inCount is " + inCount);

        while (!inflater.finished()) {
            int want = -1, got;

            // only read if the input buffer is empty
            if (inflater.needsInput() && inCount != 0) {
                want = (inCount < LOCAL_BUF_SIZE) ? inCount : LOCAL_BUF_SIZE;

                inflater.setInput(inBuf, inPosn, want);

                inCount -= want;
                inPosn += want;
            }

            // inflate to current position in output buffer
            int compCount;

            compCount = inflater.inflate(outBuf, outPosn, LOCAL_BUF_SIZE);
            outPosn += compCount;

            //System.out.println("Expanded " + want + ", output " + compCount);
        }
    
private voidsimpleTest()

        // Encode a String into bytes
        String inputString = "blahblahblah??";
        byte[] input = inputString.getBytes("UTF-8");

        // Compress the bytes
        byte[] output = new byte[100];
        Deflater compresser = new Deflater();
        compresser.setInput(input);
        compresser.finish();
        int compressedDataLength = compresser.deflate(output);

        // Decompress the bytes
        Inflater decompresser = new Inflater();
        decompresser.setInput(output, 0, compressedDataLength);
        byte[] result = new byte[100];
        int resultLength = decompresser.inflate(result);

        // Decode the bytes into a String
        String outputString = new String(result, 0, resultLength, "UTF-8");

        assertEquals(inputString, outputString);
        assertEquals(compresser.getAdler(), decompresser.getAdler());

        decompresser.end();
    
public voidtestDeflate()

        simpleTest();

        bigTest(0, 1738149618);
        bigTest(1, 934350518);
        bigTest(2, -532869390);