Code example for Deflater

Methods: deflate, end, getAdler, getTotalIn, getTotalOut, needsInput, reset, setDictionary, setInput, setLevel

0
        defl.setInput(byteArray);
        while (!defl.needsInput()) {
            defl.deflate(outPutBuf);
        } 
        defl.finish();
        while (!defl.finished()) {
            defl.deflate(outPutBuf);
        } 
        assertEquals(totalOut, defl.getTotalOut());
        defl.end();
 
        outPutBuf = new byte[500];
        defl = new Deflater(2, true);
        defl.setInput(byteArray);
        while (!defl.needsInput()) {
            defl.deflate(outPutBuf);
        } 
        defl.finish();
        while (!defl.finished()) {
            defl.deflate(outPutBuf);
        } 
        assertTrue(
                "getTotalOut() should not be equal comparing two Deflaters with different header options.", 
                defl.getTotalOut() != totalOut);
        defl.end();
 
        byte outPutInf[] = new byte[500];
        Inflater infl = new Inflater(true);
        while (!infl.finished()) {
            if (infl.needsInput()) {
                infl.setInput(outPutBuf);
            } 
            infl.inflate(outPutInf);
        } 
        for (int i = 0; i < byteArray.length; i++) {
            assertEquals(byteArray[i], outPutInf[i]);
        } 
        assertEquals(
                "final decompressed data contained more bytes than original - constructorIZ", 
                0, outPutInf[byteArray.length]);
        infl.end();
 
        infl = new Inflater(false);
        outPutInf = new byte[500];
        int r = 0;
        try { 
            while (!infl.finished()) {
                if (infl.needsInput()) {
                    infl.setInput(outPutBuf);
                } 
                infl.inflate(outPutInf);
            } 
        } catch (DataFormatException e) {
            r = 1;
        } 
        assertEquals("header option did not correspond", 1, r);
 
        // testing boundaries 
        try { 
            Deflater boundDefl = new Deflater();
            // Level must be between 0-9 
            boundDefl.setLevel(-2);
            fail("IllegalArgumentException not thrown when setting level to a number < 0.");
        } catch (IllegalArgumentException e) {
        } 
        try { 
            Deflater boundDefl = new Deflater();
            boundDefl.setLevel(10);
            fail("IllegalArgumentException not thrown when setting level to a number > 9.");
        } catch (IllegalArgumentException e) {
        } 
    } 
 
    /** 
     * @tests java.util.zip.Deflater#Deflater(int) 
     */ 
    @TestTargetNew( 
        level = TestLevel.COMPLETE, 
        notes = "",