1 /* 2 * Copyright (c) 2006, 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 /** 25 * @test 26 * @bug 4181483 27 * @summary Test if InputStream methods will check if the stream 28 * has been closed. 29 */ 30 31 import java.io.*; 32 33 public enum OpsAfterClose { 34 check(InputStream is)35 READ { boolean check(InputStream is) { 36 try { 37 int read = is.read(); 38 System.out.println("read returns: " + read); 39 } catch (IOException io) { 40 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 41 return true; 42 } 43 return false; 44 } }, 45 check(InputStream is)46 READ_BUF { boolean check(InputStream is) { 47 try { 48 byte buf[] = new byte[2]; 49 int read = is.read(buf); 50 System.out.println("read(buf) returns: " + read); 51 } catch (IOException io) { 52 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 53 return true; 54 } 55 return false; 56 } }, check(InputStream is)57 READ_BUF_OFF { boolean check(InputStream is) { 58 try { 59 byte buf[] = new byte[2]; 60 int len = 1; 61 int read = is.read(buf, 0, len); 62 System.out.println("read(buf, 0, len) returns: " + read); 63 } catch (IOException io) { 64 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 65 return true; 66 } 67 return false; 68 } }, check(InputStream is)69 AVAILABLE { boolean check(InputStream is) { 70 try { 71 int avail = is.available(); 72 System.out.println("available() returns: " + avail); 73 return false; 74 } catch (IOException io) { 75 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 76 return true; 77 } 78 } }, check(InputStream is)79 SKIP { boolean check(InputStream is) { 80 try { 81 long skipped = is.skip(1); 82 System.out.println("skip() returns: " + skipped); 83 } catch (IOException io) { 84 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 85 return true; 86 } 87 return false; 88 } }, check(InputStream is)89 MARK { boolean check(InputStream is) { 90 is.mark(20); 91 return true; 92 } }, check(InputStream is)93 RESET { boolean check(InputStream is) { 94 try { 95 is.reset(); 96 } catch (IOException io) { 97 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 98 return true; 99 } 100 return false; 101 } }, check(InputStream is)102 MARK_SUPPORTED { boolean check(InputStream is) { 103 is.markSupported(); 104 return true; 105 } }, check(InputStream is)106 CLOSE { boolean check(InputStream is) { 107 try { 108 is.close(); 109 return true; // No Exception thrown on windows for FileInputStream 110 } catch (IOException io) { 111 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 112 return true; // Exception thrown on solaris and linux for FileInputStream 113 } 114 } }; 115 check(InputStream is)116 abstract boolean check(InputStream is); 117 main(String args[])118 public static void main(String args[]) throws Exception { 119 120 boolean failed = false; 121 122 File f = new File(System.getProperty("test.dir", "."), 123 "f.txt"); 124 f.createNewFile(); 125 f.deleteOnExit(); 126 127 FileInputStream fis = new FileInputStream(f); 128 try { 129 if (testInputStream(fis)) { 130 failed = true; 131 } 132 if (testFileInputStream(fis)) { 133 failed = true; 134 } 135 } finally { 136 fis.close(); 137 } 138 139 BufferedInputStream bs = new BufferedInputStream( 140 new FileInputStream(f)); 141 try { 142 if (testInputStream(bs)) { 143 failed = true; 144 } 145 } finally { 146 bs.close(); 147 } 148 149 DataInputStream dis = new DataInputStream( 150 new FileInputStream(f)); 151 try { 152 if (testInputStream(dis)) { 153 failed = true; 154 } 155 } finally { 156 dis.close(); 157 } 158 159 PushbackInputStream pbis = new PushbackInputStream( 160 new ByteArrayInputStream(new byte[20])); 161 if (testInputStream(pbis)) { 162 failed = true; 163 } 164 165 if (testPushbackInputStream(pbis)) { 166 failed = true; 167 } 168 169 PipedInputStream pis = new PipedInputStream(new PipedOutputStream()); 170 if (testInputStream(pis)) { 171 failed = true; 172 } 173 174 /** 175 * The SequenceInputStream and ObjectInputStream does not throw IOException 176 177 SequenceInputStream sqis = new SequenceInputStream( 178 new FileInputStream(f), 179 new PipedInputStream(new PipedOutputStream()) 180 ); 181 if (testInputStream(sqis)) { 182 failed = true; 183 } 184 185 String serStr = "abc"; 186 ObjectOutputStream oos = new ObjectOutputStream( 187 new FileOutputStream(f)); 188 oos.writeObject(serStr); 189 oos.close(); 190 191 ObjectInputStream ois = new ObjectInputStream( 192 new FileInputStream(f)); 193 if (testInputStream(ois)) { 194 failed = true; 195 } 196 197 */ 198 199 if (failed) { 200 throw new Exception( 201 "Some Op for some Stream failed, check the failed status above"); 202 } 203 } 204 testInputStream(InputStream is)205 private static boolean testInputStream(InputStream is) 206 throws Exception { 207 is.close(); 208 boolean failed = false; 209 boolean result; 210 System.out.println("Testing :" + is); 211 for (OpsAfterClose op : OpsAfterClose.values()) { 212 213 if (op.equals(AVAILABLE) && (is instanceof PipedInputStream)) { 214 // skip the test as available() returns 0 215 continue; 216 } 217 218 result = op.check(is); 219 if (!result) { 220 failed = true; 221 } 222 System.out.println(op + ":" + result); 223 } 224 if (failed) { 225 System.out.println("Test failed for the failed operation{s}" + 226 " above for :" + is); 227 } 228 return failed; 229 } 230 testPushbackInputStream(PushbackInputStream pis)231 private static boolean testPushbackInputStream(PushbackInputStream pis) 232 throws Exception { 233 boolean failed = false; 234 try { 235 pis.unread(1); 236 System.out.println("Test failed for unread(int):" + pis); 237 failed = true; 238 } catch (IOException io) { 239 System.out.println("UNREAD(int):true"); 240 } 241 242 byte buf[] = new byte[2]; 243 try { 244 pis.unread(buf, 0, 2); 245 System.out.println("Test failed for unread(buf, offset, len):" + 246 pis); 247 failed = true; 248 } catch (IOException io) { 249 System.out.println("UNREAD(buf, offset, len):true"); 250 } 251 try { 252 pis.unread(buf); 253 System.out.println("Test failed for unread(char[] buf):" + pis); 254 failed = true; 255 } catch (IOException io) { 256 System.out.println("UNREAD(buf):true"); 257 } 258 return failed; 259 } 260 testFileInputStream(FileInputStream fis)261 private static boolean testFileInputStream(FileInputStream fis) 262 throws Exception { 263 boolean failed = false; 264 try { 265 fis.getFD(); 266 System.out.println("GetFD: true"); 267 } catch (IOException io) { 268 System.out.println("GetFD: false"); 269 failed = true; 270 } 271 fis.getChannel(); 272 System.out.println("GetChannel: true"); 273 return failed; 274 } 275 } 276