Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/SocketInputStream/SocketClosedException.java
41149 views
1
/*
2
* Copyright (c) 2002, 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 4681556
27
* @library /test/lib
28
* @summary Wrong text if a read is performed on a socket after it
29
* has been closed
30
* @run main SocketClosedException
31
* @run main/othervm -Djava.net.preferIPv4Stack=true SocketClosedException
32
*/
33
34
import java.io.*;
35
import java.net.*;
36
import jdk.test.lib.net.IPSupport;
37
38
public class SocketClosedException {
39
static void doServerSide() throws Exception {
40
try {
41
Socket socket = serverSocket.accept();
42
43
OutputStream os = socket.getOutputStream();
44
45
os.write(85);
46
os.flush();
47
socket.close();
48
} finally {
49
serverSocket.close();
50
}
51
}
52
53
static void doClientSide(int port) throws Exception {
54
InetAddress loopback = InetAddress.getLoopbackAddress();
55
Socket socket = new Socket(loopback, port);
56
InputStream is = socket.getInputStream();
57
58
is.read();
59
socket.close();
60
is.read();
61
}
62
63
static ServerSocket serverSocket;
64
static Exception serverException = null;
65
66
public static void main(String[] args) throws Exception {
67
IPSupport.throwSkippedExceptionIfNonOperational();
68
InetAddress loopback = InetAddress.getLoopbackAddress();
69
serverSocket = new ServerSocket();
70
serverSocket.bind(new InetSocketAddress(loopback, 0));
71
startServer();
72
try {
73
doClientSide(serverSocket.getLocalPort());
74
} catch (SocketException e) {
75
if (!e.getMessage().equalsIgnoreCase("Socket closed")) {
76
throw new Exception("Received a wrong exception message: " +
77
e.getMessage());
78
}
79
System.out.println("PASSED: received the right exception message: "
80
+ e.getMessage());
81
}
82
if (serverException != null) {
83
throw serverException;
84
}
85
}
86
87
static void startServer() {
88
(new Thread() {
89
public void run() {
90
try {
91
doServerSide();
92
} catch (Exception e) {
93
e.printStackTrace();
94
}
95
}
96
}).start();
97
}
98
}
99
100