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