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