4817
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
4817
|
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.
|
4817
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @bug 4521942
|
|
26 |
* @summary Ensure that InputStreamReaders work properly
|
|
27 |
* when the underlying byte stream times out
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.net.*;
|
|
31 |
import java.io.*;
|
|
32 |
|
|
33 |
|
|
34 |
public class StreamTimeout {
|
|
35 |
|
|
36 |
private static PrintStream log = System.err;
|
|
37 |
|
|
38 |
private static String charset = "US-ASCII";
|
|
39 |
|
|
40 |
private static Object lock = new Object();
|
|
41 |
private static synchronized void waitABit(int millisec) {
|
|
42 |
synchronized(lock) {
|
|
43 |
try {
|
|
44 |
lock.wait(millisec);
|
|
45 |
} catch (InterruptedException e) {
|
|
46 |
//ignore
|
|
47 |
}
|
|
48 |
}
|
|
49 |
}
|
|
50 |
|
|
51 |
private static class Client extends Thread {
|
|
52 |
public void run() {
|
|
53 |
try {
|
|
54 |
Socket so = new Socket("127.0.0.1", 22222);
|
|
55 |
Writer wr = new OutputStreamWriter(so.getOutputStream(),
|
|
56 |
charset);
|
|
57 |
wr.write("ab");
|
|
58 |
wr.flush();
|
|
59 |
} catch (IOException x) {
|
|
60 |
log.print("Unexpected exception in writer: ");
|
|
61 |
x.printStackTrace();
|
|
62 |
System.exit(1);
|
|
63 |
}
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
private static void gobble(InputStream is, Reader rd,
|
|
68 |
int ec, boolean force)
|
|
69 |
throws Exception
|
|
70 |
{
|
|
71 |
int a = is.available();
|
|
72 |
boolean r = rd.ready();
|
|
73 |
log.print("" + a + " bytes available, "
|
|
74 |
+ "reader " + (r ? "" : "not ") + "ready");
|
|
75 |
if (!r && !force) {
|
|
76 |
log.println();
|
|
77 |
return;
|
|
78 |
}
|
|
79 |
int c;
|
|
80 |
try {
|
|
81 |
c = rd.read();
|
|
82 |
} catch (InterruptedIOException x) {
|
|
83 |
log.println();
|
|
84 |
throw x;
|
|
85 |
}
|
|
86 |
log.println(", read() ==> "
|
|
87 |
+ (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
|
|
88 |
if (c != ec)
|
|
89 |
throw new Exception("Incorrect value read: Expected "
|
|
90 |
+ ec + ", read " + (char)c);
|
|
91 |
}
|
|
92 |
|
|
93 |
public static void main(String[] args) throws Exception {
|
|
94 |
|
|
95 |
if (args.length > 0)
|
|
96 |
charset = args[0];
|
|
97 |
|
|
98 |
ServerSocket ss = new ServerSocket(22222);
|
|
99 |
Thread cl = new Client();
|
|
100 |
cl.start();
|
|
101 |
Socket s = ss.accept();
|
|
102 |
s.setSoTimeout(150);
|
|
103 |
InputStream is = s.getInputStream();
|
|
104 |
Reader rd = new InputStreamReader(is, charset);
|
|
105 |
|
|
106 |
while (is.available() <= 0)
|
|
107 |
Thread.yield();
|
|
108 |
|
|
109 |
gobble(is, rd, 'a', false);
|
|
110 |
gobble(is, rd, 'b', false);
|
|
111 |
gobble(is, rd, -1, false);
|
|
112 |
|
|
113 |
boolean caught = false;
|
|
114 |
try {
|
|
115 |
gobble(is, rd, -1, true);
|
|
116 |
} catch (InterruptedIOException e) {
|
|
117 |
log.println("Read timed out, as expected");
|
|
118 |
caught = true;
|
|
119 |
}
|
|
120 |
if (!caught) {
|
|
121 |
log.println("Read did not time out, test inapplicable");
|
|
122 |
return;
|
|
123 |
}
|
|
124 |
|
|
125 |
caught = false;
|
|
126 |
try {
|
|
127 |
gobble(is, rd, -1, true);
|
|
128 |
} catch (InterruptedIOException x) {
|
|
129 |
log.println("Second read timed out, as expected");
|
|
130 |
caught = true;
|
|
131 |
}
|
|
132 |
if (!caught)
|
|
133 |
throw new Exception("Second read completed");
|
|
134 |
|
|
135 |
}
|
|
136 |
|
|
137 |
}
|