Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/SocketChannel/AsyncCloseChannel.java
41154 views
1
/*
2
* Copyright (c) 2006, 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 6285901 6501089
26
* @summary Check no data is written to wrong socket channel during async closing.
27
*/
28
29
import java.io.IOException;
30
import java.net.InetAddress;
31
import java.net.InetSocketAddress;
32
import java.net.ServerSocket;
33
import java.net.Socket;
34
import java.nio.ByteBuffer;
35
import java.nio.channels.ClosedChannelException;
36
import java.nio.channels.SocketChannel;
37
38
public class AsyncCloseChannel {
39
static volatile boolean failed = false;
40
static volatile boolean keepGoing = true;
41
static int maxAcceptCount = 100;
42
static volatile int acceptCount = 0;
43
static int sensorPort;
44
static int targetPort;
45
46
public static void main(String args[]) throws Exception {
47
if (System.getProperty("os.name").startsWith("Windows")) {
48
System.err.println("WARNING: Still does not work on Windows!");
49
return;
50
}
51
Thread ss = new SensorServer(); ss.start();
52
Thread ts = new TargetServer(); ts.start();
53
54
sensorPort = ((ServerThread)ss).server.getLocalPort();
55
targetPort = ((ServerThread)ts).server.getLocalPort();
56
57
Thread sc = new SensorClient(); sc.start();
58
Thread tc = new TargetClient(); tc.start();
59
60
while(acceptCount < maxAcceptCount && !failed) {
61
Thread.sleep(10);
62
}
63
keepGoing = false;
64
try {
65
ss.interrupt();
66
ts.interrupt();
67
sc.interrupt();
68
tc.interrupt();
69
} catch (Exception e) {}
70
if (failed)
71
throw new RuntimeException("AsyncCloseChannel2 failed after <"
72
+ acceptCount + "> times of accept!");
73
}
74
75
static class SensorServer extends ServerThread {
76
public void runEx() throws Exception {
77
while(keepGoing) {
78
try {
79
final Socket s = server.accept();
80
new Thread() {
81
public void run() {
82
try {
83
int c = s.getInputStream().read();
84
if(c != -1) {
85
// No data is ever written to the peer's socket!
86
System.err.println("Oops: read a character: "
87
+ (char) c);
88
failed = true;
89
}
90
} catch (IOException ex) {
91
ex.printStackTrace();
92
} finally {
93
closeIt(s);
94
}
95
}
96
}.start();
97
} catch (IOException ex) {
98
System.err.println("Exception on sensor server " + ex.getMessage());
99
}
100
}
101
}
102
}
103
104
static class TargetServer extends ServerThread {
105
public void runEx() throws Exception {
106
while (keepGoing) {
107
try {
108
final Socket s = server.accept();
109
acceptCount++;
110
new Thread() {
111
public void run() {
112
boolean empty = true;
113
try {
114
while (keepGoing) {
115
int c = s.getInputStream().read();
116
if(c == -1) {
117
if(!empty)
118
break;
119
}
120
empty = false;
121
}
122
} catch (IOException ex) {
123
ex.printStackTrace();
124
} finally {
125
closeIt(s);
126
}
127
}
128
}.start();
129
} catch (IOException ex) {
130
System.err.println("Exception on target server " + ex.getMessage());
131
}
132
}
133
}
134
}
135
136
static class SensorClient extends Thread {
137
private static boolean wake;
138
private static SensorClient theClient;
139
public void run() {
140
while (keepGoing) {
141
Socket s = null;
142
try {
143
s = new Socket();
144
synchronized(this) {
145
while(!wake && keepGoing) {
146
try {
147
wait();
148
} catch (InterruptedException ex) { }
149
}
150
wake = false;
151
}
152
s.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), sensorPort));
153
try {
154
Thread.sleep(10);
155
} catch (InterruptedException ex) { }
156
} catch (IOException ex) {
157
System.err.println("Exception on sensor client " + ex.getMessage());
158
} finally {
159
if(s != null) {
160
try {
161
s.close();
162
} catch(IOException ex) { ex.printStackTrace();}
163
}
164
}
165
}
166
}
167
168
public SensorClient() {
169
theClient = this;
170
}
171
172
public static void wakeMe() {
173
synchronized(theClient) {
174
wake = true;
175
theClient.notify();
176
}
177
}
178
}
179
180
static class TargetClient extends Thread {
181
volatile boolean ready = false;
182
public void run() {
183
while(keepGoing) {
184
try {
185
final SocketChannel s = SocketChannel.open(
186
new InetSocketAddress(InetAddress.getLoopbackAddress(), targetPort));
187
s.finishConnect();
188
s.socket().setSoLinger(false, 0);
189
ready = false;
190
Thread t = new Thread() {
191
public void run() {
192
ByteBuffer b = ByteBuffer.allocate(1);
193
try {
194
for(;;) {
195
b.clear();
196
b.put((byte) 'A');
197
b.flip();
198
s.write(b);
199
ready = true;
200
}
201
} catch (IOException ex) {
202
if(!(ex instanceof ClosedChannelException))
203
System.err.println("Exception in target client child "
204
+ ex.toString());
205
}
206
}
207
};
208
t.start();
209
while(!ready && keepGoing) {
210
try {
211
Thread.sleep(10);
212
} catch (InterruptedException ex) {}
213
}
214
s.close();
215
SensorClient.wakeMe();
216
t.join();
217
} catch (IOException ex) {
218
System.err.println("Exception in target client parent "
219
+ ex.getMessage());
220
} catch (InterruptedException ex) {}
221
}
222
}
223
}
224
225
static abstract class ServerThread extends Thread {
226
ServerSocket server;
227
public ServerThread() {
228
super();
229
try {
230
server = new ServerSocket(0);
231
} catch (IOException ex) {
232
ex.printStackTrace();
233
}
234
}
235
236
public void interrupt() {
237
super.interrupt();
238
if (server != null) {
239
try {
240
server.close();
241
} catch (IOException ex) {
242
ex.printStackTrace();
243
}
244
}
245
}
246
public void run() {
247
try {
248
runEx();
249
} catch (Exception ex) {
250
ex.printStackTrace();
251
}
252
}
253
254
abstract void runEx() throws Exception;
255
}
256
257
public static void closeIt(Socket s) {
258
try {
259
if(s != null)
260
s.close();
261
} catch (IOException ex) { }
262
}
263
}
264
265