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 4206909 4813885
27 * @summary Test basic functionality of DeflaterOutputStream/InflaterInputStream and GZIPOutputStream/GZIPInputStream, including flush
28 */
29
30 import java.io.*;
31 import java.util.*;
32 import java.util.zip.*;
33
34 public class InflateIn_DeflateOut {
35
36 private static class PairedInputStream extends ByteArrayInputStream {
37 private PairedOutputStream out = null;
38 private Random random;
39
40 public PairedInputStream() {
41 // The ByteArrayInputStream needs to start with a buffer, but we
42 // need to set it to have no data
43 super(new byte[1]);
44 count = 0;
45 pos = 0;
46 random = new Random(new Date().getTime());
129 private static void WriteCloseRead() throws Throwable {
130 Random random = new Random(new Date().getTime());
131
132 PairedInputStream pis = new PairedInputStream();
133 InflaterInputStream iis = new InflaterInputStream(pis);
134
135 PairedOutputStream pos = new PairedOutputStream(pis);
136 pis.setPairedOutputStream(pos);
137
138 byte[] data = new byte[random.nextInt(1024 * 1024)];
139 byte[] buf = new byte[data.length];
140 random.nextBytes(data);
141
142 try (DeflaterOutputStream dos = new DeflaterOutputStream(pos, true)) {
143 dos.write(data);
144 }
145 check(readFully(iis, buf, buf.length));
146 check(Arrays.equals(data, buf));
147 }
148
149 private static void check(InputStream is, OutputStream os)
150 throws Throwable
151 {
152 Random random = new Random(new Date().getTime());
153 // Large writes
154 for (int x = 0; x < 200 ; x++) {
155 // byte[] data = new byte[random.nextInt(1024 * 1024)];
156 byte[] data = new byte[1024];
157 byte[] buf = new byte[data.length];
158 random.nextBytes(data);
159
160 os.write(data);
161 os.flush();
162 check(readFully(is, buf, buf.length));
163 check(Arrays.equals(data, buf));
164 }
165
166 // Small writes
167 for (int x = 0; x < 2000 ; x++) {
168 byte[] data = new byte[random.nextInt(20) + 10];
250 }
251
252 private static void GZLineOrientedProtocol() throws Throwable {
253 PairedInputStream pis = new PairedInputStream();
254 PairedOutputStream pos = new PairedOutputStream(pis);
255 pis.setPairedOutputStream(pos);
256
257 GZIPOutputStream gos = new GZIPOutputStream(pos, true);
258 gos.flush(); // flush the head out, so gis can read
259 GZIPInputStream gis = new GZIPInputStream(pis);
260
261 checkLOP(gis, gos);
262 }
263
264 public static void realMain(String[] args) throws Throwable {
265 WriteCloseRead();
266 WriteFlushRead();
267 LineOrientedProtocol();
268 GZWriteFlushRead();
269 GZLineOrientedProtocol();
270 }
271
272 //--------------------- Infrastructure ---------------------------
273 static volatile int passed = 0, failed = 0;
274 static void pass() {passed++;}
275 static void fail() {failed++; Thread.dumpStack();}
276 static void fail(String msg) {System.out.println(msg); fail();}
277 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
278 static void check(boolean cond) {if (cond) pass(); else fail();}
279 static void equal(Object x, Object y) {
280 if (x == null ? y == null : x.equals(y)) pass();
281 else fail(x + " not equal to " + y);}
282 public static void main(String[] args) throws Throwable {
283 try {realMain(args);} catch (Throwable t) {unexpected(t);}
284 System.out.println("\nPassed = " + passed + " failed = " + failed);
285 if (failed > 0) throw new AssertionError("Some tests failed");}
286 }
|
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 4206909 4813885 8189789
27 * @summary Test basic functionality of DeflaterOutputStream/InflaterInputStream and GZIPOutputStream/GZIPInputStream, including flush
28 */
29
30 import java.io.*;
31 import java.util.*;
32 import java.util.zip.*;
33
34 public class InflateIn_DeflateOut {
35
36 private static class PairedInputStream extends ByteArrayInputStream {
37 private PairedOutputStream out = null;
38 private Random random;
39
40 public PairedInputStream() {
41 // The ByteArrayInputStream needs to start with a buffer, but we
42 // need to set it to have no data
43 super(new byte[1]);
44 count = 0;
45 pos = 0;
46 random = new Random(new Date().getTime());
129 private static void WriteCloseRead() throws Throwable {
130 Random random = new Random(new Date().getTime());
131
132 PairedInputStream pis = new PairedInputStream();
133 InflaterInputStream iis = new InflaterInputStream(pis);
134
135 PairedOutputStream pos = new PairedOutputStream(pis);
136 pis.setPairedOutputStream(pos);
137
138 byte[] data = new byte[random.nextInt(1024 * 1024)];
139 byte[] buf = new byte[data.length];
140 random.nextBytes(data);
141
142 try (DeflaterOutputStream dos = new DeflaterOutputStream(pos, true)) {
143 dos.write(data);
144 }
145 check(readFully(iis, buf, buf.length));
146 check(Arrays.equals(data, buf));
147 }
148
149 private static void TestFlushableGZIPOutputStream() throws Throwable {
150 Random random = new Random(new Date().getTime());
151
152 ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
153 OutputStream output = new FlushableGZIPOutputStream(byteOutStream);
154
155 byte[] data = new byte[random.nextInt(1024 * 1024)];
156 byte[] buf = new byte[data.length];
157 random.nextBytes(data);
158
159 output.write(data);
160 for (int i=0; i<data.length; i++) {
161 output.write(data[i]);
162 }
163 output.flush();
164 for (int i=0; i<data.length; i++) {
165 output.write(data[i]);
166 }
167 output.write(data);
168 output.close();
169
170 ByteArrayInputStream byteInStream =
171 new ByteArrayInputStream(byteOutStream.toByteArray());
172
173 GZIPInputStream gzis = new GZIPInputStream(byteInStream);
174 ByteArrayOutputStream baos = new ByteArrayOutputStream();
175 int numRead;
176 byte[] b = new byte[4 * 1024];
177 try {
178 while ((numRead = gzis.read(buf)) >= 0) {
179 baos.write(b, 0, numRead);
180 }
181 } finally {
182 baos.close();
183 }
184
185 byte[] decompressedBytes = baos.toByteArray();
186
187 check(decompressedBytes.length == data.length * 4);
188 }
189
190 private static void check(InputStream is, OutputStream os)
191 throws Throwable
192 {
193 Random random = new Random(new Date().getTime());
194 // Large writes
195 for (int x = 0; x < 200 ; x++) {
196 // byte[] data = new byte[random.nextInt(1024 * 1024)];
197 byte[] data = new byte[1024];
198 byte[] buf = new byte[data.length];
199 random.nextBytes(data);
200
201 os.write(data);
202 os.flush();
203 check(readFully(is, buf, buf.length));
204 check(Arrays.equals(data, buf));
205 }
206
207 // Small writes
208 for (int x = 0; x < 2000 ; x++) {
209 byte[] data = new byte[random.nextInt(20) + 10];
291 }
292
293 private static void GZLineOrientedProtocol() throws Throwable {
294 PairedInputStream pis = new PairedInputStream();
295 PairedOutputStream pos = new PairedOutputStream(pis);
296 pis.setPairedOutputStream(pos);
297
298 GZIPOutputStream gos = new GZIPOutputStream(pos, true);
299 gos.flush(); // flush the head out, so gis can read
300 GZIPInputStream gis = new GZIPInputStream(pis);
301
302 checkLOP(gis, gos);
303 }
304
305 public static void realMain(String[] args) throws Throwable {
306 WriteCloseRead();
307 WriteFlushRead();
308 LineOrientedProtocol();
309 GZWriteFlushRead();
310 GZLineOrientedProtocol();
311 TestFlushableGZIPOutputStream();
312 }
313
314 //--------------------- Infrastructure ---------------------------
315 static volatile int passed = 0, failed = 0;
316 static void pass() {passed++;}
317 static void fail() {failed++; Thread.dumpStack();}
318 static void fail(String msg) {System.out.println(msg); fail();}
319 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
320 static void check(boolean cond) {if (cond) pass(); else fail();}
321 static void equal(Object x, Object y) {
322 if (x == null ? y == null : x.equals(y)) pass();
323 else fail(x + " not equal to " + y);}
324 public static void main(String[] args) throws Throwable {
325 try {realMain(args);} catch (Throwable t) {unexpected(t);}
326 System.out.println("\nPassed = " + passed + " failed = " + failed);
327 if (failed > 0) throw new AssertionError("Some tests failed");}
328 }
329
330 class FlushableGZIPOutputStream extends GZIPOutputStream {
331 public FlushableGZIPOutputStream(OutputStream os) throws IOException {
332 super(os);
333 }
334
335 private static final byte[] EMPTYBYTEARRAY = new byte[0];
336 private boolean hasData = false;
337
338 /**
339 * Here we make sure we have received data, so that the header has been for
340 * sure written to the output stream already.
341 */
342 @Override
343 public synchronized void write(byte[] bytes, int i, int i1)
344 throws IOException {
345 super.write(bytes, i, i1);
346 hasData = true;
347 }
348
349 @Override
350 public synchronized void write(int i) throws IOException {
351 super.write(i);
352 hasData = true;
353 }
354
355 @Override
356 public synchronized void write(byte[] bytes) throws IOException {
357 super.write(bytes);
358 hasData = true;
359 }
360
361 @Override
362 public synchronized void flush() throws IOException {
363 if (!hasData) {
364 return; // do not allow the gzip header to be flushed on its own
365 }
366
367 // trick the deflater to flush
368 /**
369 * Now this is tricky: We force the Deflater to flush its data by
370 * switching compression level. As yet, a perplexingly simple workaround
371 * for
372 * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html
373 */
374 if (!def.finished()) {
375 def.setInput(EMPTYBYTEARRAY, 0, 0);
376
377 def.setLevel(Deflater.NO_COMPRESSION);
378 deflate();
379
380 def.setLevel(Deflater.DEFAULT_COMPRESSION);
381 deflate();
382
383 out.flush();
384 }
385
386 hasData = false; // no more data to flush
387 }
388
389 /*
390 * Keep on calling deflate until it runs dry. The default implementation
391 * only does it once and can therefore hold onto data when they need to be
392 * flushed out.
393 */
394 @Override
395 protected void deflate() throws IOException {
396 int len;
397 do {
398 len = def.deflate(buf, 0, buf.length);
399 if (len > 0) {
400 out.write(buf, 0, len);
401 }
402 } while (len != 0);
403 }
404
405 }
|