Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ResponseCache/Test.java
38811 views
1
/*
2
* Copyright (c) 2012, 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
* @summary Fixed a potential NullPointerException when setting a ResponseCache that returns a null CacheRequest
26
* @bug 4837267
27
* @author Michael McMahon
28
*/
29
30
import com.sun.net.httpserver.*;
31
import java.net.*;
32
import java.io.*;
33
import java.util.*;
34
35
public class Test
36
{
37
38
static class MyHandler implements HttpHandler {
39
public void handle(HttpExchange t) throws IOException {
40
byte[] b = new byte[1024];
41
int r = 0;
42
InputStream is = t.getRequestBody();
43
while (is.read(b) != -1) ;
44
String response = "This is the response";
45
t.sendResponseHeaders(200, response.length());
46
OutputStream os = t.getResponseBody();
47
os.write(response.getBytes());
48
os.close();
49
}
50
}
51
52
public static void main(String args[]) throws Exception {
53
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
54
server.createContext("/", new MyHandler());
55
server.start();
56
ResponseCache bak = ResponseCache.getDefault();
57
58
try {
59
ResponseCache.setDefault (new ResponseCache() {
60
public CacheResponse get (URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)
61
throws IOException {
62
return null;
63
}
64
public CacheRequest put(URI uri, URLConnection conn) throws IOException
65
{
66
return null;
67
}
68
});
69
70
URL url = new URL ("http://localhost:" + server.getAddress().getPort() + "/");
71
URLConnection urlc = url.openConnection ();
72
InputStream is = urlc.getInputStream();
73
while (is.read() != -1) ;
74
is.close();
75
} finally {
76
ResponseCache.setDefault(bak);
77
server.stop(0);
78
}
79
}
80
}
81
82