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