1 /*
   2  * Copyright (c) 2010, 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  */
  29 
  30 import java.io.*;
  31 import java.util.*;
  32 import java.util.zip.*;
  33 
  34 public class GZIPInputStreamRead {
  35     public static void main(String[] args) throws Throwable {
  36         Random rnd = new Random();
  37         for (int i = 1; i < 100; i++) {
  38             int members = rnd.nextInt(10) + 1;
  39 
  40             ByteArrayOutputStream srcBAOS = new ByteArrayOutputStream();
  41             ByteArrayOutputStream dstBAOS = new ByteArrayOutputStream();
  42             for (int j = 0; j < members; j++) {
  43                 byte[] src = new byte[rnd.nextInt(8192) + 1];
  44                 rnd.nextBytes(src);
  45                 srcBAOS.write(src);
  46 
  47                 try (GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS)) {
  48                     gzos.write(src);
  49                 }
  50             }
  51             byte[] srcBytes = srcBAOS.toByteArray();
  52             byte[] dstBytes = dstBAOS.toByteArray();
  53             // try different size of buffer to read the
  54             // GZIPInputStream
  55             /* just for fun when running manually
  56             for (int j = 1; j < 10; j++) {
  57                 test(srcBytes, dstBytes, j);
  58             }
  59             */
  60             for (int j = 0; j < 10; j++) {
  61                 int readBufSZ = rnd.nextInt(2048) + 1;
  62                 test(srcBytes,
  63                      dstBytes,
  64                      readBufSZ,
  65                      512);    // the defualt buffer size
  66                 test(srcBytes,
  67                      dstBytes,
  68                      readBufSZ,
  69                      rnd.nextInt(4096) + 1);
  70             }
  71         }
  72     }
  73 
  74     private static void test(byte[] src, byte[] dst,
  75                              int readBufSize, int gzisBufSize)
  76         throws Throwable
  77     {
  78         try (GZIPInputStream gzis = new GZIPInputStream(
  79                                         new ByteArrayInputStream(dst),
  80                                         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 }