1 /*
   2  * Copyright (c) 2013, 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 7021870
  26  * @summary Reading last gzip chain member must not close the input stream
  27  */
  28 
  29 import java.io.*;
  30 import java.util.*;
  31 import java.util.zip.GZIPInputStream;
  32 import java.util.zip.GZIPOutputStream;
  33 import java.util.zip.ZipEntry;
  34 import java.util.zip.ZipInputStream;
  35 import java.util.zip.ZipOutputStream;
  36 
  37 
  38 public class GZIPInZip {
  39 
  40     private static volatile Throwable trouble;
  41 
  42     public static void main(String[] args) throws Throwable {
  43 
  44         final PipedOutputStream pos = new PipedOutputStream();
  45         final PipedInputStream pis = new PipedInputStream(pos);
  46 
  47         Thread compressor = new Thread() {
  48             public void run() {
  49                 final byte[] xbuf = { 'x' };
  50                 try {
  51                     ZipOutputStream zos = new ZipOutputStream(pos);
  52 
  53                     zos.putNextEntry(new ZipEntry("a.gz"));
  54                     GZIPOutputStream gos1 = new GZIPOutputStream(zos);
  55                     gos1.write(xbuf); gos1.finish();
  56                     zos.closeEntry();
  57 
  58                     zos.putNextEntry(new ZipEntry("b.gz"));
  59                     GZIPOutputStream gos2 = new GZIPOutputStream(zos);
  60                     gos2.write(xbuf); gos2.finish();
  61                     zos.closeEntry();
  62 
  63                 } catch (Throwable t) {
  64                     trouble = t;
  65                 }
  66             }
  67         };
  68 
  69         Thread uncompressor = new Thread() {
  70             public void run() {
  71                 try {
  72                     ZipInputStream zis = new ZipInputStream(pis);
  73                     zis.getNextEntry();
  74                     InputStream gis = new GZIPInputStream(zis);
  75                     // try to read more than the entry has
  76                     gis.skip(2);
  77 
  78                     try {
  79                         zis.getNextEntry();
  80                     } catch (IOException e) {
  81                         throw new AssertionError("ZIP stream was prematurely closed");
  82                     }
  83 
  84                 } catch (Throwable t) {
  85                     trouble = t;
  86                 }
  87             }
  88         };
  89 
  90         compressor.start(); uncompressor.start();
  91         compressor.join();  uncompressor.join();
  92 
  93         if (trouble != null)
  94             throw trouble;
  95     }
  96 }