Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/Streams.java
41152 views
1
/*
2
* Copyright (c) 2012, 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
/*
25
* @test
26
* @bug 8003833
27
* @summary Spurious NPE from Socket.getIn/OutputStream
28
*/
29
30
import java.io.IOException;
31
import java.lang.reflect.Constructor;
32
import java.net.InetAddress;
33
import java.net.InetSocketAddress;
34
import java.net.ServerSocket;
35
import java.net.Socket;
36
import java.util.concurrent.Phaser;
37
38
// Racey test, will not always fail, but if it does then there is a problem.
39
40
public class Streams {
41
static final int NUM_THREADS = 100;
42
static volatile boolean failed;
43
static final Phaser startingGate = new Phaser(NUM_THREADS + 1);
44
45
public static void main(String[] args) throws Exception {
46
47
try (ServerSocket ss = new ServerSocket()) {
48
InetAddress loopback = InetAddress.getLoopbackAddress();
49
ss.bind(new InetSocketAddress(loopback, 0));
50
runTest(OutputStreamGetter.class, ss);
51
runTest(InputStreamGetter.class, ss);
52
}
53
54
if (failed)
55
throw new RuntimeException("Failed, check output");
56
}
57
58
static void runTest(Class<? extends StreamGetter> klass, ServerSocket ss)
59
throws Exception
60
{
61
final int port = ss.getLocalPort();
62
final InetAddress address = ss.getInetAddress();
63
Socket[] sockets = new Socket[NUM_THREADS];
64
for (int i=0; i<NUM_THREADS; i++) {
65
sockets[i] = address.isAnyLocalAddress()
66
? new Socket("localhost", port)
67
: new Socket(address, port);
68
try (Socket socket = ss.accept()) {}
69
}
70
71
Constructor<? extends StreamGetter> ctr = klass.getConstructor(Socket.class);
72
73
Thread[] threads = new Thread[NUM_THREADS];
74
for (int i=0; i<NUM_THREADS; i++)
75
threads[i] = ctr.newInstance(sockets[i]);
76
for (int i=0; i<NUM_THREADS; i++)
77
threads[i].start();
78
79
startingGate.arriveAndAwaitAdvance();
80
for (int i=0; i<NUM_THREADS; i++)
81
sockets[i].close();
82
83
for (int i=0; i<NUM_THREADS; i++)
84
threads[i].join();
85
}
86
87
static abstract class StreamGetter extends Thread {
88
final Socket socket;
89
StreamGetter(Socket s) { socket = s; }
90
91
@Override
92
public void run() {
93
try {
94
startingGate.arriveAndAwaitAdvance();
95
getStream();
96
} catch (IOException x) {
97
// OK, socket may be closed
98
} catch (NullPointerException x) {
99
x.printStackTrace();
100
failed = true;
101
}
102
}
103
104
abstract void getStream() throws IOException;
105
}
106
107
static class InputStreamGetter extends StreamGetter {
108
public InputStreamGetter(Socket s) { super(s); }
109
void getStream() throws IOException {
110
socket.getInputStream();
111
}
112
}
113
114
static class OutputStreamGetter extends StreamGetter {
115
public OutputStreamGetter(Socket s) { super(s); }
116
void getStream() throws IOException {
117
socket.getOutputStream();
118
}
119
}
120
}
121
122