Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/SocketInputStream/SocketTimeout.java
41149 views
1
/*
2
* Copyright (c) 2000, 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 4158021
27
* @library /test/lib
28
* @summary cannot distinguish Thread.interrupt and Socket.setSoTimeout exceptions
29
* @run main SocketTimeout
30
* @run main/othervm -Djava.net.preferIPv4Stack=true SocketTimeout
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
import jdk.test.lib.net.IPSupport;
36
37
public class SocketTimeout {
38
static final int TIMEOUT = 1000;
39
40
public static void main(String args[]) throws Exception {
41
IPSupport.throwSkippedExceptionIfNonOperational();
42
InetAddress sin = InetAddress.getLocalHost();
43
Socket soc = null,soc1 = null;
44
InputStream is = null;
45
ServerSocket srv = null;
46
int port = 0;
47
48
srv = new ServerSocket();
49
srv.bind(new InetSocketAddress(sin, 0));
50
port = srv.getLocalPort();
51
soc = new Socket(sin, port);
52
soc1 = srv.accept();
53
soc.setSoTimeout(TIMEOUT);
54
srv.setSoTimeout(TIMEOUT);
55
56
try {
57
is = soc.getInputStream();
58
is.read();
59
} catch(InterruptedIOException e) {
60
try {
61
if (! (e instanceof java.net.SocketTimeoutException))
62
throw new Exception ("Wrong exception class thrown");
63
} catch(NoClassDefFoundError e1) {
64
throw new Exception ("SocketTimeoutException: not found");
65
}
66
} finally {
67
soc.close();
68
soc1.close();
69
}
70
71
// now check accept
72
73
try {
74
srv.accept ();
75
} catch(InterruptedIOException e) {
76
try {
77
if (! (e instanceof java.net.SocketTimeoutException))
78
throw new Exception ("Wrong exception class thrown");
79
} catch(NoClassDefFoundError e1) {
80
throw new Exception ("SocketTimeoutException: not found");
81
}
82
} finally {
83
srv.close();
84
}
85
86
// Now check DatagramSocket.receive()
87
88
DatagramSocket dg = new DatagramSocket ();
89
dg.setSoTimeout (TIMEOUT);
90
91
try {
92
dg.receive (new DatagramPacket (new byte [64], 64));
93
} catch(InterruptedIOException e) {
94
try {
95
if (! (e instanceof java.net.SocketTimeoutException))
96
throw new Exception ("Wrong exception class thrown");
97
} catch(NoClassDefFoundError e1) {
98
throw new Exception ("SocketTimeoutException: not found");
99
}
100
} finally {
101
dg.close();
102
}
103
}
104
}
105
106