Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/ChunkedInputStream/TestAvailable.java
41154 views
1
/*
2
* Copyright (c) 2006, 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 6446990
27
* @modules jdk.httpserver
28
* @library /test/lib
29
* @run main/othervm TestAvailable
30
* @summary HttpURLConnection#available() reads more and more data into memory
31
*/
32
33
import java.net.*;
34
import java.util.*;
35
import java.io.*;
36
import com.sun.net.httpserver.*;
37
import java.util.concurrent.Executors;
38
import java.util.concurrent.ExecutorService;
39
import jdk.test.lib.net.URIBuilder;
40
41
public class TestAvailable
42
{
43
com.sun.net.httpserver.HttpServer httpServer;
44
ExecutorService executorService;
45
46
public static void main(String[] args)
47
{
48
new TestAvailable();
49
}
50
51
public TestAvailable()
52
{
53
try {
54
startHttpServer();
55
doClient();
56
} catch (IOException ioe) {
57
System.err.println(ioe);
58
}
59
}
60
61
void doClient() {
62
try {
63
InetSocketAddress address = httpServer.getAddress();
64
65
URL url = URIBuilder.newBuilder()
66
.scheme("http")
67
.host(address.getAddress())
68
.port(address.getPort())
69
.path("/testAvailable/")
70
.toURLUnchecked();
71
72
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
73
74
uc.setDoOutput(true);
75
uc.setRequestMethod("POST");
76
uc.setChunkedStreamingMode(0);
77
OutputStream os = uc.getOutputStream();
78
for (int i=0; i< (128 * 1024); i++)
79
os.write('X');
80
os.close();
81
82
InputStream is = uc.getInputStream();
83
int avail = 0;
84
while (avail == 0) {
85
try { Thread.sleep(2000); } catch (Exception e) {}
86
avail = is.available();
87
}
88
89
try { Thread.sleep(2000); } catch (Exception e) {}
90
int nextAvail = is.available();
91
92
is.close();
93
94
if (nextAvail > avail) {
95
throw new RuntimeException
96
("Failed: calling available multiple times should not return more data");
97
}
98
99
} catch (IOException e) {
100
throw new RuntimeException(e);
101
} finally {
102
httpServer.stop(1);
103
executorService.shutdown();
104
}
105
106
107
}
108
109
/**
110
* Http Server
111
*/
112
public void startHttpServer() throws IOException {
113
InetAddress loopback = InetAddress.getLoopbackAddress();
114
InetSocketAddress sockaddr = new InetSocketAddress(loopback, 0);
115
httpServer = com.sun.net.httpserver.HttpServer.create(sockaddr, 0);
116
117
// create HttpServer context
118
HttpContext ctx = httpServer.createContext("/testAvailable/", new MyHandler());
119
120
executorService = Executors.newCachedThreadPool();
121
httpServer.setExecutor(executorService);
122
httpServer.start();
123
}
124
125
class MyHandler implements HttpHandler {
126
public void handle(HttpExchange t) throws IOException {
127
InputStream is = t.getRequestBody();
128
byte[] ba = new byte[1024];
129
while (is.read(ba) != -1);
130
is.close();
131
132
t.sendResponseHeaders(200, 0);
133
134
OutputStream os = t.getResponseBody();
135
for (int i=0; i< (128 * 1024); i++)
136
os.write('X');
137
os.close();
138
139
t.close();
140
}
141
}
142
}
143
144