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