1 /*
2  * Copyright (c) 1998, 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 4110528 4112112 4112103
26    @summary Test if zip related in/output streams will
27             prevent i/o after stream has been closed.
28    */
29 import java.util.zip.*;
30 import java.util.jar.*;
31 import java.io.*;
32 
33 public class StreamIOAfterClose {
34     // compressed stub data
35     private static byte[] compressed = {
36         31,-117,8,0,0,0,0,0,0,0,-85,-107,-79,74,85,97,117,48,
37         -9,117,-47,114,15,-87,-27,-9,-54,-48,49,-108,17,19,20,
38         118,-87,-84,78,-15,-10,-87,-112,51,115,16,85,81,54,11,
39         114,44,11,98,116,17,102,-10,72,-10,80,-79,101,14,47,-50,
40         16,117,-9,-83,16,13,-55,83,83,103,-30,-117,-42,-82,-105,
41         -119,46,-16,20,-111,-85,-16,-48,54,79,-53,-76,116,
42         -80,-78,77,-88,-85,50,113,-54,15,-74,-28,-44,101,-43,47,
43         85,54,-74,1,0,85,69,28,117,100,0,0,0
44     };
45 
testRead(InputStream in)46     private static void testRead(InputStream in) throws Exception {
47         in.close();
48         try {
49             in.read();
50             throw new Exception("read allowed after stream is closed");
51         } catch (IOException e) {
52         }
53     }
54 
testWrite(ZipOutputStream out)55     private static void testWrite(ZipOutputStream out) throws Exception {
56         out.close();
57         try {
58             out.putNextEntry(new ZipEntry(""));
59             throw new Exception("write allowed after stream is closed");
60         } catch (IOException e) {
61         }
62     }
63 
main(String argv[])64     public static void main(String argv[]) throws Exception {
65         ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream());
66         zos.putNextEntry(new ZipEntry("1"));
67         testWrite(zos);
68 
69         JarOutputStream jos = new JarOutputStream(new ByteArrayOutputStream());
70         jos.putNextEntry(new ZipEntry("1"));
71         testWrite(jos);
72 
73         InputStream bis = new ByteArrayInputStream(new byte[10]);
74         InputStream bis1 = new ByteArrayInputStream(compressed);
75         testRead(new ZipInputStream(bis));
76         testRead(new GZIPInputStream(bis1));
77     }
78 }
79