2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4181483
|
|
27 |
* @summary Test if DataInputStream methods will check if the stream
|
|
28 |
* has been closed.
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.*;
|
|
32 |
|
|
33 |
public enum OpsAfterClose {
|
|
34 |
|
|
35 |
READ { boolean check(DataInputStream 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 |
|
|
46 |
READ_BUF { boolean check(DataInputStream 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 |
} },
|
|
57 |
READ_BUF_OFF { boolean check(DataInputStream 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 |
} },
|
|
69 |
AVAILABLE { boolean check(DataInputStream 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 |
} },
|
|
79 |
SKIP { boolean check(DataInputStream 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 |
} },
|
|
89 |
MARK { boolean check(DataInputStream is) {
|
|
90 |
is.mark(20);
|
|
91 |
return true;
|
|
92 |
} },
|
|
93 |
RESET { boolean check(DataInputStream 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 |
} },
|
|
102 |
MARK_SUPPORTED { boolean check(DataInputStream is) {
|
|
103 |
is.markSupported();
|
|
104 |
return true;
|
|
105 |
} },
|
|
106 |
CLOSE { boolean check(DataInputStream is) {
|
|
107 |
try {
|
|
108 |
is.close();
|
|
109 |
return true; // No Exception thrown on windows
|
|
110 |
} catch (IOException io) {
|
|
111 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
112 |
return true; // Exception thrown on solaris and linux
|
|
113 |
}
|
|
114 |
}},
|
|
115 |
READ_BYTE { boolean check(DataInputStream is) {
|
|
116 |
try {
|
|
117 |
is.readByte();
|
|
118 |
} catch (IOException io) {
|
|
119 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
120 |
return true;
|
|
121 |
}
|
|
122 |
return false;
|
|
123 |
} },
|
|
124 |
READ_CHAR { boolean check(DataInputStream is) {
|
|
125 |
try {
|
|
126 |
is.readChar();
|
|
127 |
} catch (IOException io) {
|
|
128 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
129 |
return true;
|
|
130 |
}
|
|
131 |
return false;
|
|
132 |
} },
|
|
133 |
READ_DOUBLE { boolean check(DataInputStream is) {
|
|
134 |
try {
|
|
135 |
is.readDouble();
|
|
136 |
} catch (IOException io) {
|
|
137 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
138 |
return true;
|
|
139 |
}
|
|
140 |
return false;
|
|
141 |
} },
|
|
142 |
|
|
143 |
READ_FLOAT { boolean check(DataInputStream is) {
|
|
144 |
try {
|
|
145 |
is.readFloat();
|
|
146 |
} catch (IOException io) {
|
|
147 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
148 |
return true;
|
|
149 |
}
|
|
150 |
return false;
|
|
151 |
} },
|
|
152 |
READ_INT { boolean check(DataInputStream is) {
|
|
153 |
try {
|
|
154 |
is.readInt();
|
|
155 |
} catch (IOException io) {
|
|
156 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
157 |
return true;
|
|
158 |
}
|
|
159 |
return false;
|
|
160 |
} },
|
|
161 |
READ_LONG { boolean check(DataInputStream is) {
|
|
162 |
try {
|
|
163 |
is.readLong();
|
|
164 |
} catch (IOException io) {
|
|
165 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
166 |
return true;
|
|
167 |
}
|
|
168 |
return false;
|
|
169 |
} },
|
|
170 |
READ_SHORT { boolean check(DataInputStream is) {
|
|
171 |
try {
|
|
172 |
is.readShort();
|
|
173 |
} catch (IOException io) {
|
|
174 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
175 |
return true;
|
|
176 |
}
|
|
177 |
return false;
|
|
178 |
} },
|
|
179 |
READ_UnsignedByte { boolean check(DataInputStream is) {
|
|
180 |
try {
|
|
181 |
is.readUnsignedByte();
|
|
182 |
} catch (IOException io) {
|
|
183 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
184 |
return true;
|
|
185 |
}
|
|
186 |
return false;
|
|
187 |
} },
|
|
188 |
READ_UnsignedShort { boolean check(DataInputStream is) {
|
|
189 |
try {
|
|
190 |
is.readUnsignedShort();
|
|
191 |
} catch (IOException io) {
|
|
192 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
193 |
return true;
|
|
194 |
}
|
|
195 |
return false;
|
|
196 |
} },
|
|
197 |
READ_UTF { boolean check(DataInputStream is) {
|
|
198 |
try {
|
|
199 |
is.readUTF();
|
|
200 |
} catch (IOException io) {
|
|
201 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
202 |
return true;
|
|
203 |
}
|
|
204 |
return false;
|
|
205 |
} },
|
|
206 |
SKIP_BYTES { boolean check(DataInputStream is) {
|
|
207 |
try {
|
|
208 |
is.skipBytes(1);
|
|
209 |
} catch (IOException io) {
|
|
210 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
211 |
return true;
|
|
212 |
}
|
|
213 |
return false;
|
|
214 |
} },
|
|
215 |
READ_FULLY { boolean check(DataInputStream is) {
|
|
216 |
try {
|
|
217 |
is.readFully(new byte[1]);
|
|
218 |
} catch (IOException io) {
|
|
219 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
220 |
return true;
|
|
221 |
}
|
|
222 |
return false;
|
|
223 |
} },
|
|
224 |
READ_FULLY_BUF { boolean check(DataInputStream is) {
|
|
225 |
try {
|
|
226 |
is.readFully(new byte[1], 0, 1);
|
|
227 |
} catch (IOException io) {
|
|
228 |
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
|
229 |
return true;
|
|
230 |
}
|
|
231 |
return false;
|
|
232 |
} };
|
|
233 |
|
|
234 |
|
|
235 |
abstract boolean check(DataInputStream is);
|
|
236 |
|
|
237 |
public static void main(String args[]) throws Exception {
|
|
238 |
|
|
239 |
boolean failed = false;
|
|
240 |
|
|
241 |
File f = new File(System.getProperty("test.dir", "."),
|
|
242 |
"f.txt");
|
|
243 |
f.createNewFile();
|
|
244 |
f.deleteOnExit();
|
|
245 |
|
|
246 |
FileInputStream fis = new FileInputStream(f);
|
|
247 |
|
|
248 |
DataInputStream dis = new DataInputStream(
|
|
249 |
new FileInputStream(f));
|
|
250 |
if (testDataInputStream(dis)) {
|
|
251 |
failed = true;
|
|
252 |
}
|
|
253 |
|
|
254 |
}
|
|
255 |
|
|
256 |
private static boolean testDataInputStream(DataInputStream is)
|
|
257 |
throws Exception {
|
|
258 |
is.close();
|
|
259 |
boolean failed = false;
|
|
260 |
boolean result;
|
|
261 |
System.out.println("Testing :" + is);
|
|
262 |
for (OpsAfterClose op : OpsAfterClose.values()) {
|
|
263 |
|
|
264 |
result = op.check(is);
|
|
265 |
if (!result) {
|
|
266 |
failed = true;
|
|
267 |
}
|
|
268 |
System.out.println(op + ":" + result);
|
|
269 |
}
|
|
270 |
if (failed) {
|
|
271 |
System.out.println("Test failed for the failed operation{s}" +
|
|
272 |
" above for :" + is);
|
|
273 |
}
|
|
274 |
return failed;
|
|
275 |
}
|
|
276 |
}
|