Path: blob/master/test/jdk/java/io/DataInputStream/OpsAfterClose.java
41149 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 DataInputStream methods will check if the stream27* has been closed.28*/2930import java.io.*;3132public enum OpsAfterClose {3334READ { boolean check(DataInputStream 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(DataInputStream 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(DataInputStream 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(DataInputStream 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(DataInputStream 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(DataInputStream is) {89is.mark(20);90return true;91} },92RESET { boolean check(DataInputStream 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(DataInputStream is) {102is.markSupported();103return true;104} },105CLOSE { boolean check(DataInputStream is) {106try {107is.close();108return true; // No Exception thrown on windows109} catch (IOException io) {110System.out.print("Excep Msg: "+ io.getMessage() + ", ");111return true; // Exception thrown on solaris and linux112}113}},114READ_BYTE { boolean check(DataInputStream is) {115try {116is.readByte();117} catch (IOException io) {118System.out.print("Excep Msg: "+ io.getMessage() + ", ");119return true;120}121return false;122} },123READ_CHAR { boolean check(DataInputStream is) {124try {125is.readChar();126} catch (IOException io) {127System.out.print("Excep Msg: "+ io.getMessage() + ", ");128return true;129}130return false;131} },132READ_DOUBLE { boolean check(DataInputStream is) {133try {134is.readDouble();135} catch (IOException io) {136System.out.print("Excep Msg: "+ io.getMessage() + ", ");137return true;138}139return false;140} },141142READ_FLOAT { boolean check(DataInputStream is) {143try {144is.readFloat();145} catch (IOException io) {146System.out.print("Excep Msg: "+ io.getMessage() + ", ");147return true;148}149return false;150} },151READ_INT { boolean check(DataInputStream is) {152try {153is.readInt();154} catch (IOException io) {155System.out.print("Excep Msg: "+ io.getMessage() + ", ");156return true;157}158return false;159} },160READ_LONG { boolean check(DataInputStream is) {161try {162is.readLong();163} catch (IOException io) {164System.out.print("Excep Msg: "+ io.getMessage() + ", ");165return true;166}167return false;168} },169READ_SHORT { boolean check(DataInputStream is) {170try {171is.readShort();172} catch (IOException io) {173System.out.print("Excep Msg: "+ io.getMessage() + ", ");174return true;175}176return false;177} },178READ_UnsignedByte { boolean check(DataInputStream is) {179try {180is.readUnsignedByte();181} catch (IOException io) {182System.out.print("Excep Msg: "+ io.getMessage() + ", ");183return true;184}185return false;186} },187READ_UnsignedShort { boolean check(DataInputStream is) {188try {189is.readUnsignedShort();190} catch (IOException io) {191System.out.print("Excep Msg: "+ io.getMessage() + ", ");192return true;193}194return false;195} },196READ_UTF { boolean check(DataInputStream is) {197try {198is.readUTF();199} catch (IOException io) {200System.out.print("Excep Msg: "+ io.getMessage() + ", ");201return true;202}203return false;204} },205SKIP_BYTES { boolean check(DataInputStream is) {206try {207is.skipBytes(1);208} catch (IOException io) {209System.out.print("Excep Msg: "+ io.getMessage() + ", ");210return true;211}212return false;213} },214READ_FULLY { boolean check(DataInputStream is) {215try {216is.readFully(new byte[1]);217} catch (IOException io) {218System.out.print("Excep Msg: "+ io.getMessage() + ", ");219return true;220}221return false;222} },223READ_FULLY_BUF { boolean check(DataInputStream is) {224try {225is.readFully(new byte[1], 0, 1);226} catch (IOException io) {227System.out.print("Excep Msg: "+ io.getMessage() + ", ");228return true;229}230return false;231} };232233234abstract boolean check(DataInputStream is);235236public static void main(String args[]) throws Exception {237238boolean failed = false;239240File f = new File(System.getProperty("test.dir", "."),241"f.txt");242f.createNewFile();243f.deleteOnExit();244245FileInputStream fis = new FileInputStream(f);246try {247DataInputStream dis = new DataInputStream(248new FileInputStream(f));249try {250if (testDataInputStream(dis)) {251failed = true;252}253} finally {254dis.close();255}256} finally {257fis.close();258}259}260261private static boolean testDataInputStream(DataInputStream is)262throws Exception {263is.close();264boolean failed = false;265boolean result;266System.out.println("Testing :" + is);267for (OpsAfterClose op : OpsAfterClose.values()) {268269result = op.check(is);270if (!result) {271failed = true;272}273System.out.println(op + ":" + result);274}275if (failed) {276System.out.println("Test failed for the failed operation{s}" +277" above for :" + is);278}279return failed;280}281}282283284