Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/GetErrorStream.java
41159 views
1
/*
2
* Copyright (c) 2000, 2018, 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 4160499
27
* @modules jdk.httpserver
28
* @summary sun.net.www.protocol.http.HttpURLConnection.getErrorStream not hooked up
29
*/
30
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
31
32
import java.io.FileNotFoundException;
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.io.OutputStream;
36
import java.net.HttpURLConnection;
37
import java.net.InetAddress;
38
import java.net.InetSocketAddress;
39
import java.net.URL;
40
import java.net.URLConnection;
41
import java.util.concurrent.ExecutorService;
42
import java.util.concurrent.Executors;
43
44
import com.sun.net.httpserver.HttpExchange;
45
import com.sun.net.httpserver.HttpHandler;
46
import com.sun.net.httpserver.HttpServer;
47
48
public class GetErrorStream {
49
public static void main(String[] args) throws Exception {
50
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
51
HttpServer server = HttpServer.create(addr, 10);
52
server.createContext("/" + HTTP_NOT_FOUND, he -> {
53
final String RESPONSE = "Test: File Not Found.";
54
he.sendResponseHeaders(HTTP_NOT_FOUND, RESPONSE.length());
55
OutputStream os = he.getResponseBody();
56
os.write(RESPONSE.getBytes());
57
os.close();
58
});
59
int port = server.getAddress().getPort();
60
System.out.println("Server port = " + port);
61
62
ExecutorService executor = Executors.newCachedThreadPool();
63
server.setExecutor(executor);
64
server.start();
65
66
URL url = new URL("http://localhost:" + port + "/" + HTTP_NOT_FOUND);
67
URLConnection conn = url.openConnection();
68
69
try {
70
InputStream is = conn.getInputStream();
71
throw new RuntimeException("Expect HTTP_NOT_FOUND!");
72
} catch (FileNotFoundException e) {
73
try {
74
int respCode = ((HttpURLConnection) conn).getResponseCode();
75
InputStream es = ((HttpURLConnection) conn).getErrorStream();
76
if (respCode == HTTP_NOT_FOUND && es != null) {
77
System.out.println("Passed!");
78
} else {
79
throw new RuntimeException("getErrorStream failure.");
80
}
81
} catch (Exception ex) {
82
}
83
} finally {
84
server.stop(0);
85
executor.shutdownNow();
86
}
87
88
}
89
}
90
91