Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/charset/coders/StreamTimeout.java
38828 views
1
/*
2
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
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
*
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.
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.io.Closeable;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.InputStreamReader;
34
import java.io.InterruptedIOException;
35
import java.io.OutputStreamWriter;
36
import java.io.PrintStream;
37
import java.io.Reader;
38
import java.io.Writer;
39
import java.net.ServerSocket;
40
import java.net.Socket;
41
42
public class StreamTimeout {
43
static final PrintStream log = System.err;
44
static String charset = "US-ASCII";
45
46
private static class Client extends Thread implements Closeable {
47
private final Socket so;
48
49
Client(int port) throws IOException {
50
so = new Socket("127.0.0.1", port);
51
}
52
53
@Override
54
public void run() {
55
try {
56
Writer wr = new OutputStreamWriter(so.getOutputStream(),
57
charset);
58
wr.write("ab");
59
wr.flush();
60
} catch (IOException x) {
61
log.print("Unexpected exception in writer: ");
62
x.printStackTrace();
63
}
64
}
65
66
@Override
67
public void close() throws IOException {
68
so.close();
69
}
70
}
71
72
private static void gobble(InputStream is, Reader rd,
73
int ec, boolean force)
74
throws Exception
75
{
76
int a = is.available();
77
boolean r = rd.ready();
78
log.print("" + a + " bytes available, "
79
+ "reader " + (r ? "" : "not ") + "ready");
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() ==> "
92
+ (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
93
if (c != ec)
94
throw new Exception("Incorrect value read: Expected "
95
+ ec + ", read " + (char)c);
96
}
97
98
public static void main(String[] args) throws Exception {
99
100
if (args.length > 0)
101
charset = args[0];
102
103
try(ServerSocket ss = new ServerSocket(0);
104
Client cl = new Client(ss.getLocalPort())) {
105
106
cl.start();
107
108
try(Socket s = ss.accept()) {
109
s.setSoTimeout(150);
110
111
try(InputStream is = s.getInputStream();
112
Reader rd = new InputStreamReader(is, charset)) {
113
114
while (is.available() <= 0)
115
Thread.yield();
116
117
gobble(is, rd, 'a', false);
118
gobble(is, rd, 'b', false);
119
gobble(is, rd, -1, false);
120
121
boolean caught = false;
122
try {
123
gobble(is, rd, -1, true);
124
} catch (InterruptedIOException e) {
125
log.println("Read timed out, as expected");
126
caught = true;
127
}
128
if (!caught) {
129
log.println("Read did not time out, test inapplicable");
130
return;
131
}
132
133
caught = false;
134
try {
135
gobble(is, rd, -1, true);
136
} catch (InterruptedIOException x) {
137
log.println("Second read timed out, as expected");
138
caught = true;
139
}
140
if (!caught)
141
throw new Exception("Second read completed");
142
}
143
}
144
145
cl.join();
146
}
147
}
148
}
149
150