1 /*
   2  * Copyright (c) 2010, 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 /* @test
  25  * @bug 4691425
  26  * @summary Test the read and write of GZIPInput/OutputStream, including
  27  *          concatenated .gz inputstream
  28  * @key randomness
  29  */
  30 
  31 import java.io.*;
  32 import java.util.*;
  33 import java.util.zip.*;
  34 
  35 public class GZIPInputStreamRead {
  36     public static void main(String[] args) throws Throwable {
  37         Random rnd = new Random();
  38         for (int i = 1; i < 100; i++) {
  39             int members = rnd.nextInt(10) + 1;
  40 
  41             ByteArrayOutputStream srcBAOS = new ByteArrayOutputStream();
  42             ByteArrayOutputStream dstBAOS = new ByteArrayOutputStream();
  43             for (int j = 0; j < members; j++) {
  44                 byte[] src = new byte[rnd.nextInt(8192) + 1];
  45                 rnd.nextBytes(src);
  46                 srcBAOS.write(src);
  47 
  48                 try (GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS)) {
  49                     gzos.write(src);
  50                 }
  51             }
  52             byte[] srcBytes = srcBAOS.toByteArray();
  53             byte[] dstBytes = dstBAOS.toByteArray();
  54             // try different size of buffer to read the
  55             // GZIPInputStream
  56             /* just for fun when running manually
  57             for (int j = 1; j < 10; j++) {
  58                 test(srcBytes, dstBytes, j);
  59             }
  60             */
  61             for (int j = 0; j < 10; j++) {
  62                 int readBufSZ = rnd.nextInt(2048) + 1;
  63                 test(srcBytes,
  64                      dstBytes,
  65                      readBufSZ,
  66                      512);    // the defualt buffer size
  67                 test(srcBytes,
  68                      dstBytes,
  69                      readBufSZ,
  70                      rnd.nextInt(4096) + 1);
  71             }
  72         }
  73     }
  74 
  75     private static void test(byte[] src, byte[] dst,
  76                              int readBufSize, int gzisBufSize)
  77         throws Throwable
  78     {
  79         try (ByteArrayInputStream bais = new ByteArrayInputStream(dst);
  80              GZIPInputStream gzis = new GZIPInputStream(bais, gzisBufSize))
  81         {
  82             byte[] result = new byte[src.length + 10];
  83             byte[] buf = new byte[readBufSize];
  84             int n = 0;
  85             int off = 0;
  86 
  87             while ((n = gzis.read(buf, 0, buf.length)) != -1) {
  88                 System.arraycopy(buf, 0, result, off, n);
  89                 off += n;
  90                 // no range check, if overflow, let it fail
  91             }
  92             if (off != src.length || gzis.available() != 0 ||
  93                 !Arrays.equals(src, Arrays.copyOf(result, off))) {
  94                 throw new RuntimeException(
  95                     "GZIPInputStream reading failed! " +
  96                     ", src.len=" + src.length +
  97                     ", read=" + off);
  98             }
  99         }
 100     }
 101 }