Path: blob/master/test/jdk/java/io/InputStream/OpsAfterClose.java
41152 views
/*1* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 418148326* @summary Test if InputStream methods will check if the stream27* has been closed.28*/2930import java.io.*;3132public enum OpsAfterClose {3334READ { boolean check(InputStream is) {35try {36int read = is.read();37System.out.println("read returns: " + read);38} catch (IOException io) {39System.out.print("Excep Msg: "+ io.getMessage() + ", ");40return true;41}42return false;43} },4445READ_BUF { boolean check(InputStream is) {46try {47byte buf[] = new byte[2];48int read = is.read(buf);49System.out.println("read(buf) returns: " + read);50} catch (IOException io) {51System.out.print("Excep Msg: "+ io.getMessage() + ", ");52return true;53}54return false;55} },56READ_BUF_OFF { boolean check(InputStream is) {57try {58byte buf[] = new byte[2];59int len = 1;60int read = is.read(buf, 0, len);61System.out.println("read(buf, 0, len) returns: " + read);62} catch (IOException io) {63System.out.print("Excep Msg: "+ io.getMessage() + ", ");64return true;65}66return false;67} },68AVAILABLE { boolean check(InputStream is) {69try {70int avail = is.available();71System.out.println("available() returns: " + avail);72return false;73} catch (IOException io) {74System.out.print("Excep Msg: "+ io.getMessage() + ", ");75return true;76}77} },78SKIP { boolean check(InputStream is) {79try {80long skipped = is.skip(1);81System.out.println("skip() returns: " + skipped);82} catch (IOException io) {83System.out.print("Excep Msg: "+ io.getMessage() + ", ");84return true;85}86return false;87} },88MARK { boolean check(InputStream is) {89is.mark(20);90return true;91} },92RESET { boolean check(InputStream is) {93try {94is.reset();95} catch (IOException io) {96System.out.print("Excep Msg: "+ io.getMessage() + ", ");97return true;98}99return false;100} },101MARK_SUPPORTED { boolean check(InputStream is) {102is.markSupported();103return true;104} },105CLOSE { boolean check(InputStream is) {106try {107is.close();108return true; // No Exception thrown on windows for FileInputStream109} catch (IOException io) {110System.out.print("Excep Msg: "+ io.getMessage() + ", ");111return true; // Exception thrown on solaris and linux for FileInputStream112}113} };114115abstract boolean check(InputStream is);116117public static void main(String args[]) throws Exception {118119boolean failed = false;120121File f = new File(System.getProperty("test.dir", "."),122"f.txt");123f.createNewFile();124f.deleteOnExit();125126FileInputStream fis = new FileInputStream(f);127try {128if (testInputStream(fis)) {129failed = true;130}131if (testFileInputStream(fis)) {132failed = true;133}134} finally {135fis.close();136}137138BufferedInputStream bs = new BufferedInputStream(139new FileInputStream(f));140try {141if (testInputStream(bs)) {142failed = true;143}144} finally {145bs.close();146}147148DataInputStream dis = new DataInputStream(149new FileInputStream(f));150try {151if (testInputStream(dis)) {152failed = true;153}154} finally {155dis.close();156}157158PushbackInputStream pbis = new PushbackInputStream(159new ByteArrayInputStream(new byte[20]));160if (testInputStream(pbis)) {161failed = true;162}163164if (testPushbackInputStream(pbis)) {165failed = true;166}167168PipedInputStream pis = new PipedInputStream(new PipedOutputStream());169if (testInputStream(pis)) {170failed = true;171}172173/**174* The SequenceInputStream and ObjectInputStream does not throw IOException175176SequenceInputStream sqis = new SequenceInputStream(177new FileInputStream(f),178new PipedInputStream(new PipedOutputStream())179);180if (testInputStream(sqis)) {181failed = true;182}183184String serStr = "abc";185ObjectOutputStream oos = new ObjectOutputStream(186new FileOutputStream(f));187oos.writeObject(serStr);188oos.close();189190ObjectInputStream ois = new ObjectInputStream(191new FileInputStream(f));192if (testInputStream(ois)) {193failed = true;194}195196*/197198if (failed) {199throw new Exception(200"Some Op for some Stream failed, check the failed status above");201}202}203204private static boolean testInputStream(InputStream is)205throws Exception {206is.close();207boolean failed = false;208boolean result;209System.out.println("Testing :" + is);210for (OpsAfterClose op : OpsAfterClose.values()) {211212if (op.equals(AVAILABLE) && (is instanceof PipedInputStream)) {213// skip the test as available() returns 0214continue;215}216217result = op.check(is);218if (!result) {219failed = true;220}221System.out.println(op + ":" + result);222}223if (failed) {224System.out.println("Test failed for the failed operation{s}" +225" above for :" + is);226}227return failed;228}229230private static boolean testPushbackInputStream(PushbackInputStream pis)231throws Exception {232boolean failed = false;233try {234pis.unread(1);235System.out.println("Test failed for unread(int):" + pis);236failed = true;237} catch (IOException io) {238System.out.println("UNREAD(int):true");239}240241byte buf[] = new byte[2];242try {243pis.unread(buf, 0, 2);244System.out.println("Test failed for unread(buf, offset, len):" +245pis);246failed = true;247} catch (IOException io) {248System.out.println("UNREAD(buf, offset, len):true");249}250try {251pis.unread(buf);252System.out.println("Test failed for unread(char[] buf):" + pis);253failed = true;254} catch (IOException io) {255System.out.println("UNREAD(buf):true");256}257return failed;258}259260private static boolean testFileInputStream(FileInputStream fis)261throws Exception {262boolean failed = false;263try {264fis.getFD();265System.out.println("GetFD: true");266} catch (IOException io) {267System.out.println("GetFD: false");268failed = true;269}270fis.getChannel();271System.out.println("GetChannel: true");272return failed;273}274}275276277