Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/Finalizer.java
41159 views
1
/*
2
* Copyright (c) 2000, 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
/* @test
25
@bug 4372248
26
@summary Java2D demo throws exceptions on MerlinB32
27
*/
28
import java.io.*;
29
import java.net.*;
30
class XServer extends Thread {
31
ServerSocket srv;
32
Socket s;
33
InputStream is;
34
OutputStream os;
35
36
XServer (ServerSocket s) {
37
srv = s;
38
}
39
40
Socket getSocket () {
41
return (s);
42
}
43
44
public void run() {
45
try {
46
String x;
47
String msg = "Message from the server\r\n";
48
s = srv.accept ();
49
is = s.getInputStream ();
50
BufferedReader r = new BufferedReader(new InputStreamReader(is));
51
os = s.getOutputStream ();
52
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));
53
while ((x=r.readLine()) != null) {
54
if (x.length() == 0)
55
break;
56
}
57
w.write("HTTP/1.1 200\r\n");
58
w.write("Content-Type: text/html\r\n");
59
w.write("Content-Length: "+msg.length()+"\r\n");
60
w.write("\r\n");
61
w.write(msg);
62
w.flush();
63
64
// second round
65
while ((x=r.readLine()) != null) {
66
if (x.length() == 0)
67
break;
68
}
69
w.write("HTTP/1.1 200\r\n");
70
w.write("Content-Type: text/html\r\n");
71
w.write("Content-Length: "+msg.length()+"\r\n");
72
w.write("\r\n");
73
w.write(msg);
74
w.flush();
75
s.close ();
76
srv.close ();
77
} catch (IOException e) {}
78
}
79
}
80
81
public class Finalizer {
82
public static void main (String args[]) {
83
ServerSocket serversocket = null;
84
try {
85
InetAddress loopback = InetAddress.getLoopbackAddress();
86
serversocket = new ServerSocket();
87
serversocket.bind(new InetSocketAddress(loopback, 0));
88
int port = serversocket.getLocalPort ();
89
XServer server = new XServer (serversocket);
90
server.start ();
91
Thread.sleep (2000);
92
URL url = new URL ("http://localhost:"+port+"/index.html");
93
URLConnection urlc = url.openConnection ();
94
urlc.connect ();
95
InputStream is = urlc.getInputStream ();
96
is = urlc.getInputStream ();
97
sink (is);
98
HttpURLConnection ht = (HttpURLConnection) urlc;
99
100
URLConnection url1 = url.openConnection ();
101
is = url1.getInputStream ();
102
103
ht = null; urlc = null;
104
System.gc();
105
System.runFinalization ();
106
107
sink (is);
108
109
System.out.println("Passed!");
110
} catch (IOException e) {
111
throw new RuntimeException("finalize method failure."+e);
112
} catch (InterruptedException ie) {
113
} finally {
114
if (serversocket != null) {
115
try {serversocket.close();} catch (IOException io) {}
116
}
117
}
118
}
119
120
static void sink (InputStream is) {
121
int c;
122
byte [] b = new byte [1024];
123
try {
124
while ((c = is.read (b)) != -1);
125
} catch (Exception e) {
126
throw new RuntimeException("finalize method failure."+e);
127
}
128
}
129
}
130
131