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 */
29
30 import java.io.*;
31 import java.util.*;
32 import java.util.zip.*;
33
34 public class DeInflate {
35
36 static void check(Deflater compresser, byte[] in, int len,
37 byte[] out1, byte[] out2, boolean nowrap)
38 throws Throwable
39 {
40 Arrays.fill(out1, (byte)0);
41 Arrays.fill(out2, (byte)0);
42
43 compresser.setInput(in, 0, len);
44 compresser.finish();
45 int m = compresser.deflate(out1);
46
47 Inflater decompresser = new Inflater(nowrap);
48 decompresser.setInput(out1, 0, m);
49 int n = decompresser.inflate(out2);
50
51 if (n != len ||
52 !Arrays.equals(Arrays.copyOf(in, len), Arrays.copyOf(out2, len)) ||
53 decompresser.inflate(out2) != 0) {
54 System.out.printf("m=%d, n=%d, len=%d, eq=%b%n",
55 m, n, len, Arrays.equals(in, out2));
56 throw new RuntimeException("De/inflater failed:" + compresser);
57 }
58 }
59
60 public static void main(String[] args) throws Throwable {
61 byte[] dataIn = new byte[1024 * 512];
62 new Random().nextBytes(dataIn);
63 byte[] dataOut1 = new byte[dataIn.length + 1024];
64 byte[] dataOut2 = new byte[dataIn.length];
65 boolean wrap[] = new boolean[] { false, true };
66
67 for (int level = Deflater.DEFAULT_COMPRESSION;
68 level <= Deflater.BEST_COMPRESSION; level++) {
69 System.out.print("level=" + level + ", strategy= ");
70 for (int strategy = Deflater.DEFAULT_STRATEGY;
71 strategy <= Deflater.HUFFMAN_ONLY; strategy++) {
72 System.out.print(" " + strategy + " nowrap[");
73 for (int dowrap = 0; dowrap <= 1; dowrap++) {
74 System.out.print(" " + wrap[dowrap]);
75 for (int i = 0; i < 5; i++) {
76 Deflater def = new Deflater(level, wrap[dowrap]);
77 if (strategy != Deflater.DEFAULT_STRATEGY) {
78 def.setStrategy(strategy);
79 // The first invocation after setLevel/Strategy()
80 // with a different level/stragety returns 0, if
81 // there is no need to flush out anything for the
82 // previous setting/"data", this is tricky and
83 // appears un-documented.
84 def.deflate(dataOut2);
85 }
86 int len = (i == 0)? dataIn.length
87 : new Random().nextInt(dataIn.length);
88 check(def, dataIn, len, dataOut1, dataOut2, wrap[dowrap]);
89 }
90 }
91 System.out.print("] ");
92 }
93 System.out.println();
94 }
95 }
96 }
|
1 /*
2 * Copyright (c) 2011, 2017, 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 8184682 8184306
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 checkStream(Deflater def, 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 ByteArrayOutputStream baos = new ByteArrayOutputStream();
45 try (DeflaterOutputStream defos = new DeflaterOutputStream(baos, def)) {
46 defos.write(in, 0, len);
47 }
48 out1 = baos.toByteArray();
49 int m = out1.length;
50
51 Inflater inf = new Inflater(nowrap);
52 inf.setInput(out1, 0, m);
53 int n = inf.inflate(out2);
54
55 if (n != len ||
56 !Arrays.equals(Arrays.copyOf(in, len), Arrays.copyOf(out2, len)) ||
57 inf.inflate(out2) != 0) {
58 System.out.printf("m=%d, n=%d, len=%d, eq=%b%n",
59 m, n, len, Arrays.equals(in, out2));
60 throw new RuntimeException("De/inflater failed:" + def);
61 }
62 }
63
64 static void check(Deflater def, byte[] in, int len,
65 byte[] out1, byte[] out2, boolean nowrap)
66 throws Throwable
67 {
68 Arrays.fill(out1, (byte)0);
69 Arrays.fill(out2, (byte)0);
70
71 def.setInput(in, 0, len);
72 def.finish();
73 int m = def.deflate(out1);
74
75 Inflater inf = new Inflater(nowrap);
76 inf.setInput(out1, 0, m);
77 int n = inf.inflate(out2);
78
79 if (n != len ||
80 !Arrays.equals(Arrays.copyOf(in, len), Arrays.copyOf(out2, len)) ||
81 inf.inflate(out2) != 0) {
82 System.out.printf("m=%d, n=%d, len=%d, eq=%b%n",
83 m, n, len, Arrays.equals(in, out2));
84 throw new RuntimeException("De/inflater failed:" + def);
85 }
86 }
87
88 private static Deflater newDeflater(int level, int strategy, boolean dowrap, byte[] tmp) {
89 Deflater def = new Deflater(level, dowrap);
90 if (strategy != Deflater.DEFAULT_STRATEGY) {
91 def.setStrategy(strategy);
92 // The first invocation after setLevel/Strategy()
93 // with a different level/stragety returns 0, if
94 // there is no need to flush out anything for the
95 // previous setting/"data", this is tricky and
96 // appears un-documented.
97 def.deflate(tmp);
98 }
99 return def;
100 }
101
102 private static Deflater resetDeflater(Deflater def, int level, int strategy) {
103 def.setLevel(level);
104 def.setStrategy(strategy);
105 def.reset();
106 return def;
107 }
108
109 public static void main(String[] args) throws Throwable {
110
111 byte[] dataIn = new byte[1024 * 512];
112 new Random().nextBytes(dataIn);
113 byte[] dataOut1 = new byte[dataIn.length + 1024];
114 byte[] dataOut2 = new byte[dataIn.length];
115
116 Deflater defNotWrap = new Deflater(Deflater.DEFAULT_COMPRESSION, false);
117 Deflater defWrap = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
118
119 for (int level = Deflater.DEFAULT_COMPRESSION;
120 level <= Deflater.BEST_COMPRESSION; level++) {
121 for (int strategy = Deflater.DEFAULT_STRATEGY;
122 strategy <= Deflater.HUFFMAN_ONLY; strategy++) {
123 for (boolean dowrap : new boolean[] { false, true }) {
124 System.out.println("level:" + level +
125 ", strategy: " + strategy +
126 ", dowrap: " + dowrap);
127 for (int i = 0; i < 5; i++) {
128 int len = (i == 0)? dataIn.length
129 : new Random().nextInt(dataIn.length);
130 // use a new deflater
131 Deflater def = newDeflater(level, strategy, dowrap, dataOut2);
132 check(def, dataIn, len, dataOut1, dataOut2, dowrap);
133
134 // reuse the deflater (with reset) and test on stream, which
135 // uses a "smaller" buffer (smaller than the overall data)
136 def = resetDeflater(dowrap ? defWrap : defNotWrap, level, strategy);
137 checkStream(def, dataIn, len, dataOut1, dataOut2, dowrap);
138 }
139 }
140 }
141 }
142 }
143 }
|