jdk/test/java/util/zip/DeInflate.java
author sherman
Tue, 29 Nov 2011 11:39:59 -0800
changeset 11112 6d340c7b6a32
child 30046 cf2c86e1819e
permissions -rw-r--r--
7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5 Summary: updated to zlib-1.2.5 Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11112
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     1
/*
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     4
 *
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     8
 *
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    13
 * accompanied this code).
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    14
 *
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    18
 *
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    21
 * questions.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    22
 */
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    23
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    24
/**
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    25
 * @test
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    26
 * @bug 7110149
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    27
 * @summary Test basic deflater & inflater functionality
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    28
 */
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    29
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    30
import java.io.*;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    31
import java.util.*;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    32
import java.util.zip.*;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    33
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    34
public class DeInflate {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    35
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    36
    static void check(Deflater compresser, byte[] in, int len,
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    37
                      byte[] out1, byte[] out2, boolean nowrap)
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    38
        throws Throwable
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    39
    {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    40
        Arrays.fill(out1, (byte)0);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    41
        Arrays.fill(out2, (byte)0);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    42
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    43
        compresser.setInput(in, 0, len);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    44
        compresser.finish();
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    45
        int m = compresser.deflate(out1);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    46
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    47
        Inflater decompresser = new Inflater(nowrap);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    48
        decompresser.setInput(out1, 0, m);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    49
        int n = decompresser.inflate(out2);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    50
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    51
        if (n != len ||
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    52
            !Arrays.equals(Arrays.copyOf(in, len), Arrays.copyOf(out2, len)) ||
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    53
            decompresser.inflate(out2) != 0) {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    54
            System.out.printf("m=%d, n=%d, len=%d, eq=%b%n",
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    55
                              m, n, len, Arrays.equals(in, out2));
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    56
            throw new RuntimeException("De/inflater failed:" + compresser);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    57
        }
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    58
    }
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    59
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    60
    public static void main(String[] args) throws Throwable {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    61
        byte[] dataIn = new byte[1024 * 512];
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    62
        new Random().nextBytes(dataIn);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    63
        byte[] dataOut1 = new byte[dataIn.length + 1024];
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    64
        byte[] dataOut2 = new byte[dataIn.length];
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    65
        boolean wrap[] = new boolean[] { false, true };
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    66
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    67
        for (int level = Deflater.DEFAULT_COMPRESSION;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    68
                 level <= Deflater.BEST_COMPRESSION; level++) {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    69
            System.out.print("level=" + level + ", strategy= ");
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    70
            for (int strategy = Deflater.DEFAULT_STRATEGY;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    71
                     strategy <= Deflater.HUFFMAN_ONLY; strategy++) {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    72
                System.out.print(" " + strategy + " nowrap[");
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    73
                for (int dowrap = 0; dowrap <= 1; dowrap++) {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    74
                    System.out.print(" " + wrap[dowrap]);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    75
                    for (int i = 0; i < 5; i++) {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    76
                        Deflater def = new Deflater(level, wrap[dowrap]);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    77
                        if (strategy != Deflater.DEFAULT_STRATEGY) {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    78
                            def.setStrategy(strategy);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    79
                            // The first invocation after setLevel/Strategy()
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    80
                            // with a different level/stragety returns 0, if
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    81
                            // there is no need to flush out anything for the
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    82
                            // previous setting/"data", this is tricky and
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    83
                            // appears un-documented.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    84
                            def.deflate(dataOut2);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    85
                        }
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    86
                        int len = (i == 0)? dataIn.length
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    87
                                          : new Random().nextInt(dataIn.length);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    88
                            check(def, dataIn, len, dataOut1, dataOut2, wrap[dowrap]);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    89
                    }
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    90
                }
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    91
                System.out.print("] ");
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    92
            }
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    93
            System.out.println();
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    94
        }
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    95
    }
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    96
}