|
1 | 1 | /* |
2 | | - * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
@@ -47,11 +47,12 @@ static void checkStream(Deflater def, byte[] in, int len, |
47 | 47 | Arrays.fill(out1, (byte)0); |
48 | 48 | Arrays.fill(out2, (byte)0); |
49 | 49 |
|
50 | | - ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
51 | | - try (DeflaterOutputStream defos = new DeflaterOutputStream(baos, def)) { |
52 | | - defos.write(in, 0, len); |
| 50 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 51 | + try (DeflaterOutputStream defos = new DeflaterOutputStream(baos, def)) { |
| 52 | + defos.write(in, 0, len); |
| 53 | + } |
| 54 | + out1 = baos.toByteArray(); |
53 | 55 | } |
54 | | - out1 = baos.toByteArray(); |
55 | 56 | int m = out1.length; |
56 | 57 |
|
57 | 58 | Inflater inf = new Inflater(nowrap); |
@@ -118,27 +119,55 @@ static void checkByteBufferReadonly(Deflater def, Inflater inf, |
118 | 119 | } catch (ReadOnlyBufferException robe) {} |
119 | 120 | } |
120 | 121 |
|
121 | | - static void check(Deflater def, byte[] in, int len, |
122 | | - byte[] out1, byte[] out2, boolean nowrap) |
| 122 | + /** |
| 123 | + * Uses the {@code def} deflater to deflate the input data {@code in} of length {@code len}. |
| 124 | + * A new {@link Inflater} is then created within this method to inflate the deflated data. The |
| 125 | + * inflated data is then compared with the {@code in} to assert that it matches the original |
| 126 | + * input data. |
| 127 | + * This method repeats these checks for the different overloaded methods of |
| 128 | + * {@code Deflater.deflate(...)} and {@code Inflater.inflate(...)} |
| 129 | + * |
| 130 | + * @param def the deflater to use for deflating the contents in {@code in} |
| 131 | + * @param in the input content |
| 132 | + * @param len the length of the input content to use |
| 133 | + * @param nowrap will be passed to the constructor of the {@code Inflater} used in this |
| 134 | + * method |
| 135 | + * @throws Throwable if any error occurs during the check |
| 136 | + */ |
| 137 | + static void check(Deflater def, byte[] in, int len, boolean nowrap) |
123 | 138 | throws Throwable |
124 | 139 | { |
125 | | - Arrays.fill(out1, (byte)0); |
126 | | - Arrays.fill(out2, (byte)0); |
127 | | - |
| 140 | + byte[] tempBuffer = new byte[len]; |
| 141 | + byte[] out1, out2; |
| 142 | + int m = 0, n = 0; |
| 143 | + Inflater inf = new Inflater(nowrap); |
128 | 144 | def.setInput(in, 0, len); |
129 | 145 | def.finish(); |
130 | | - int m = def.deflate(out1); |
131 | 146 |
|
132 | | - Inflater inf = new Inflater(nowrap); |
133 | | - inf.setInput(out1, 0, m); |
134 | | - int n = inf.inflate(out2); |
| 147 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(len)) { |
| 148 | + while (!def.finished()) { |
| 149 | + int temp_counter = def.deflate(tempBuffer); |
| 150 | + m += temp_counter; |
| 151 | + baos.write(tempBuffer, 0, temp_counter); |
| 152 | + } |
| 153 | + out1 = baos.toByteArray(); |
| 154 | + baos.reset(); |
135 | 155 |
|
136 | | - if (n != len || |
137 | | - !Arrays.equals(Arrays.copyOf(in, len), Arrays.copyOf(out2, len)) || |
138 | | - inf.inflate(out2) != 0) { |
139 | | - System.out.printf("m=%d, n=%d, len=%d, eq=%b%n", |
140 | | - m, n, len, Arrays.equals(in, out2)); |
141 | | - throw new RuntimeException("De/inflater failed:" + def); |
| 156 | + inf.setInput(out1, 0, m); |
| 157 | + |
| 158 | + while (!inf.finished()) { |
| 159 | + int temp_counter = inf.inflate(tempBuffer); |
| 160 | + n += temp_counter; |
| 161 | + baos.write(tempBuffer, 0, temp_counter); |
| 162 | + } |
| 163 | + out2 = baos.toByteArray(); |
| 164 | + if (n != len || |
| 165 | + !Arrays.equals(in, 0, len, out2, 0, len) || |
| 166 | + inf.inflate(out2) != 0) { |
| 167 | + System.out.printf("m=%d, n=%d, len=%d, eq=%b%n", |
| 168 | + m, n, len, Arrays.equals(in, out2)); |
| 169 | + throw new RuntimeException("De/inflater failed:" + def); |
| 170 | + } |
142 | 171 | } |
143 | 172 |
|
144 | 173 | // readable |
@@ -287,7 +316,7 @@ public static void main(String[] args) throws Throwable { |
287 | 316 | : new Random().nextInt(dataIn.length); |
288 | 317 | // use a new deflater |
289 | 318 | Deflater def = newDeflater(level, strategy, dowrap, dataOut2); |
290 | | - check(def, dataIn, len, dataOut1, dataOut2, dowrap); |
| 319 | + check(def, dataIn, len, dowrap); |
291 | 320 | def.end(); |
292 | 321 |
|
293 | 322 | // reuse the deflater (with reset) and test on stream, which |
|
0 commit comments