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 8023431 8026756
26 * @summary Reading last gzip chain member must not close the input stream.
27 *          Garbage following gzip entry must be ignored.
28 */
29
30import java.io.ByteArrayInputStream;
31import java.io.ByteArrayOutputStream;
32import java.io.IOException;
33import java.util.zip.GZIPInputStream;
34import java.util.zip.GZIPOutputStream;
35import java.util.zip.ZipEntry;
36import java.util.zip.ZipInputStream;
37import java.util.zip.ZipOutputStream;
38
39
40public class GZIPInZip {
41
42    public static void main(String[] args)
43            throws Throwable {
44
45        doTest(false, false);
46        doTest(false, true);
47        doTest(true, false);
48        doTest(true, true);
49    }
50
51    private static void doTest(final boolean appendGarbage,
52                               final boolean limitGISBuff)
53            throws Throwable {
54
55        byte[] buf;
56
57        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
58             ZipOutputStream zos = new ZipOutputStream(baos)) {
59
60            final byte[] xbuf = { 'x' };
61
62            zos.putNextEntry(new ZipEntry("a.gz"));
63            GZIPOutputStream gos1 = new GZIPOutputStream(zos);
64            gos1.write(xbuf);
65            gos1.finish();
66            if (appendGarbage)
67                zos.write(xbuf);
68            zos.closeEntry();
69
70            zos.putNextEntry(new ZipEntry("b.gz"));
71            GZIPOutputStream gos2 = new GZIPOutputStream(zos);
72            gos2.write(xbuf);
73            gos2.finish();
74            zos.closeEntry();
75
76            zos.flush();
77            buf = baos.toByteArray();
78        }
79
80        try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
81             ZipInputStream zis = new ZipInputStream(bais)) {
82
83            zis.getNextEntry();
84            GZIPInputStream gis1 = limitGISBuff ?
85                    new GZIPInputStream(zis, 4) :
86                    new GZIPInputStream(zis);
87            // try to read more than the entry has
88            gis1.skip(2);
89
90            try {
91                zis.getNextEntry();
92            } catch (IOException e) {
93                throw new RuntimeException("ZIP stream was prematurely closed", e);
94            }
95        }
96    }
97}
98