author | iklam |
Tue, 12 Aug 2014 17:29:01 -0700 | |
changeset 26212 | 7585e2404b2a |
parent 12441 | dc1628ee8f30 |
permissions | -rw-r--r-- |
4817 | 1 |
/* |
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2010, 2012, 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 |
||
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
30 |
import java.io.Closeable; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
31 |
import java.io.IOException; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
32 |
import java.io.InputStream; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
33 |
import java.io.InputStreamReader; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
34 |
import java.io.InterruptedIOException; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
35 |
import java.io.OutputStreamWriter; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
36 |
import java.io.PrintStream; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
37 |
import java.io.Reader; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
38 |
import java.io.Writer; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
39 |
import java.net.ServerSocket; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
40 |
import java.net.Socket; |
4817 | 41 |
|
42 |
public class StreamTimeout { |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
43 |
static final PrintStream log = System.err; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
44 |
static String charset = "US-ASCII"; |
4817 | 45 |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
46 |
private static class Client extends Thread implements Closeable { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
47 |
private final Socket so; |
4817 | 48 |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
49 |
Client(int port) throws IOException { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
50 |
so = new Socket("127.0.0.1", port); |
4817 | 51 |
} |
52 |
||
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
53 |
@Override |
4817 | 54 |
public void run() { |
55 |
try { |
|
56 |
Writer wr = new OutputStreamWriter(so.getOutputStream(), |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
57 |
charset); |
4817 | 58 |
wr.write("ab"); |
59 |
wr.flush(); |
|
60 |
} catch (IOException x) { |
|
61 |
log.print("Unexpected exception in writer: "); |
|
62 |
x.printStackTrace(); |
|
63 |
} |
|
64 |
} |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
65 |
|
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
66 |
@Override |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
67 |
public void close() throws IOException { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
68 |
so.close(); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
69 |
} |
4817 | 70 |
} |
71 |
||
72 |
private static void gobble(InputStream is, Reader rd, |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
73 |
int ec, boolean force) |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
74 |
throws Exception |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
75 |
{ |
4817 | 76 |
int a = is.available(); |
77 |
boolean r = rd.ready(); |
|
78 |
log.print("" + a + " bytes available, " |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
79 |
+ "reader " + (r ? "" : "not ") + "ready"); |
4817 | 80 |
if (!r && !force) { |
81 |
log.println(); |
|
82 |
return; |
|
83 |
} |
|
84 |
int c; |
|
85 |
try { |
|
86 |
c = rd.read(); |
|
87 |
} catch (InterruptedIOException x) { |
|
88 |
log.println(); |
|
89 |
throw x; |
|
90 |
} |
|
91 |
log.println(", read() ==> " |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
92 |
+ (c >= 0 ? ("'" + (char)c + "'" ): "EOF")); |
4817 | 93 |
if (c != ec) |
94 |
throw new Exception("Incorrect value read: Expected " |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
95 |
+ ec + ", read " + (char)c); |
4817 | 96 |
} |
97 |
||
98 |
public static void main(String[] args) throws Exception { |
|
99 |
||
100 |
if (args.length > 0) |
|
101 |
charset = args[0]; |
|
102 |
||
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
103 |
try(ServerSocket ss = new ServerSocket(0); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
104 |
Client cl = new Client(ss.getLocalPort())) { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
105 |
|
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
106 |
cl.start(); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
107 |
|
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
108 |
try(Socket s = ss.accept()) { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
109 |
s.setSoTimeout(150); |
4817 | 110 |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
111 |
try(InputStream is = s.getInputStream(); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
112 |
Reader rd = new InputStreamReader(is, charset)) { |
4817 | 113 |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
114 |
while (is.available() <= 0) |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
115 |
Thread.yield(); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
116 |
|
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
117 |
gobble(is, rd, 'a', false); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
118 |
gobble(is, rd, 'b', false); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
119 |
gobble(is, rd, -1, false); |
4817 | 120 |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
121 |
boolean caught = false; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
122 |
try { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
123 |
gobble(is, rd, -1, true); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
124 |
} catch (InterruptedIOException e) { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
125 |
log.println("Read timed out, as expected"); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
126 |
caught = true; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
127 |
} |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
128 |
if (!caught) { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
129 |
log.println("Read did not time out, test inapplicable"); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
130 |
return; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
131 |
} |
4817 | 132 |
|
12441
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
133 |
caught = false; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
134 |
try { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
135 |
gobble(is, rd, -1, true); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
136 |
} catch (InterruptedIOException x) { |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
137 |
log.println("Second read timed out, as expected"); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
138 |
caught = true; |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
139 |
} |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
140 |
if (!caught) |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
141 |
throw new Exception("Second read completed"); |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
142 |
} |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
143 |
} |
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
144 |
|
dc1628ee8f30
7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents:
5506
diff
changeset
|
145 |
cl.join(); |
4817 | 146 |
} |
147 |
} |
|
148 |
} |