Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java
41159 views
1
/*
2
* Copyright (c) 2005, 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 6262486
27
* @library /test/lib
28
* @modules java.base/sun.net.www
29
* @library ../../httptest/
30
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
31
* @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream
32
* @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load
33
*/
34
35
import java.net.*;
36
import java.io.*;
37
import java.util.*;
38
import jdk.test.lib.net.URIBuilder;
39
40
public class ResponseCacheStream implements HttpCallback {
41
42
void okReply (HttpTransaction req) throws IOException {
43
req.setResponseEntityBody ("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");
44
req.sendResponse (200, "Ok");
45
System.out.println ("Server: sent response");
46
req.orderlyClose();
47
}
48
49
public void request (HttpTransaction req) {
50
try {
51
okReply (req);
52
} catch (IOException e) {
53
e.printStackTrace();
54
}
55
}
56
57
static class MyCacheRequest extends CacheRequest {
58
private OutputStream buf = null;
59
60
public MyCacheRequest(OutputStream out) {
61
buf = out;
62
}
63
64
public OutputStream getBody() throws IOException {
65
return buf;
66
}
67
68
/**
69
* Aborts the attempt to cache the response. If an IOException is
70
* encountered while reading the response or writing to the cache,
71
* the current cache store operation will be abandoned.
72
*/
73
public void abort() {
74
}
75
76
}
77
78
static class MyResponseCache extends ResponseCache {
79
private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
80
81
public MyResponseCache() {
82
}
83
84
public CacheRequest put(URI uri, URLConnection conn) throws IOException {
85
return new MyCacheRequest(buf);
86
}
87
88
public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException {
89
return null;
90
}
91
92
public byte[] getBuffer() {
93
return buf.toByteArray();
94
}
95
}
96
97
static TestHttpServer server;
98
99
public static void main(String[] args) throws Exception {
100
MyResponseCache cache = new MyResponseCache();
101
try {
102
InetAddress loopback = InetAddress.getLoopbackAddress();
103
ResponseCache.setDefault(cache);
104
server = new TestHttpServer (new ResponseCacheStream(), loopback, 0);
105
System.out.println ("Server: listening on port: " + server.getLocalPort());
106
URL url = URIBuilder.newBuilder()
107
.scheme("http")
108
.loopback()
109
.port(server.getLocalPort())
110
.path("/")
111
.toURL();
112
System.out.println ("Client: connecting to " + url);
113
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
114
InputStream is = urlc.getInputStream();
115
System.out.println("is is " + is.getClass() + ". And markSupported: " + is.markSupported());
116
if (is.markSupported()) {
117
byte[] b = new byte[1024];
118
byte[] b2 = new byte[32];
119
int len;
120
int count;
121
is.mark(10);
122
len = is.read(b, 0, 10);
123
is.reset();
124
len = 0;
125
count = 0;
126
do {
127
len = is.read(b, count, 40 - count);
128
if (len > 0)
129
count += len;
130
} while (len > 0);
131
is.mark(20);
132
len = is.read(b2, 0, 20);
133
is.reset();
134
len = is.read(b, count, 10);
135
count += len;
136
is.mark(20);
137
len = is.read(b2, 0, 20);
138
is.reset();
139
do {
140
len = is.read(b, count, 1024 - count);
141
if (len > 0)
142
count += len;
143
} while (len > 0);
144
is.close();
145
String s1 = new String(b, 0 , count);
146
String s2 = new String(cache.getBuffer(), 0 , count);
147
if (! s1.equals(s2))
148
throw new RuntimeException("cache got corrupted!");
149
}
150
} catch (Exception e) {
151
if (server != null) {
152
server.terminate();
153
}
154
throw e;
155
}
156
server.terminate();
157
}
158
}
159
160