Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/jar/B4957695.java
41159 views
1
/*
2
* Copyright (c) 2003, 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 4957695
27
* @run main/othervm -Djava.io.tmpdir=. B4957695
28
* @summary URLJarFile.retrieve does not delete tmpFile on IOException
29
*/
30
31
import java.io.*;
32
import java.net.*;
33
34
public class B4957695 {
35
36
static Server server;
37
38
static class Server extends Thread {
39
final ServerSocket srv;
40
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n'};
41
42
Server(ServerSocket s) {
43
srv = s;
44
}
45
46
void readOneRequest(InputStream is) throws IOException {
47
int requestEndCount = 0, r;
48
while ((r = is.read()) != -1) {
49
if (r == requestEnd[requestEndCount]) {
50
requestEndCount++;
51
if (requestEndCount == 4) {
52
break;
53
}
54
} else {
55
requestEndCount = 0;
56
}
57
}
58
}
59
60
public void run() {
61
try (Socket s = srv.accept()) {
62
// read HTTP request from client
63
readOneRequest(s.getInputStream());
64
try (OutputStreamWriter ow =
65
new OutputStreamWriter((s.getOutputStream()))) {
66
FileInputStream fin = new FileInputStream(new File(
67
System.getProperty("test.src", "."), "foo1.jar"));
68
int length = fin.available();
69
byte[] b = new byte[length-10];
70
fin.read(b, 0, length-10);
71
ow.write("HTTP/1.0 200 OK\r\n");
72
73
// Note: The client expects length bytes.
74
ow.write("Content-Length: " + length + "\r\n");
75
ow.write("Content-Type: text/html\r\n");
76
ow.write("\r\n");
77
78
// Note: The (buggy) server only sends length-10 bytes.
79
ow.write(new String(b));
80
ow.flush();
81
}
82
} catch (IOException e) {
83
e.printStackTrace();
84
}
85
}
86
}
87
88
static void read (InputStream is) throws IOException {
89
int c,len=0;
90
while ((c=is.read()) != -1) {
91
len += c;
92
}
93
System.out.println ("read " + len + " bytes");
94
}
95
96
public static void main (String[] args) throws Exception {
97
String tmpdir = System.getProperty("java.io.tmpdir");
98
String[] list1 = listTmpFiles(tmpdir);
99
InetAddress localHost = InetAddress.getByName("localhost");
100
InetSocketAddress address = new InetSocketAddress(localHost, 0);
101
ServerSocket serverSocket = new ServerSocket();
102
serverSocket.bind(address);
103
server = new Server(serverSocket);
104
server.start();
105
int port = serverSocket.getLocalPort();
106
System.out.println ("Server: listening on port: " + port);
107
URL url = new URL ("jar:http://localhost:"+port+"!/COPYRIGHT");
108
try {
109
URLConnection urlc = url.openConnection ();
110
InputStream is = urlc.getInputStream();
111
read (is);
112
is.close();
113
} catch (IOException e) {
114
System.out.println ("Received IOException as expected: " + e);
115
} finally {
116
try {serverSocket.close();} catch (IOException x) {}
117
}
118
String[] list2 = listTmpFiles(tmpdir);
119
if (!sameList (list1, list2)) {
120
throw new RuntimeException ("some jar_cache files left behind");
121
}
122
}
123
124
static String[] listTmpFiles (String d) {
125
File dir = new File (d);
126
return dir.list (new FilenameFilter () {
127
public boolean accept (File dr, String name) {
128
return (name.startsWith ("jar_cache"));
129
}
130
});
131
}
132
133
static boolean sameList (String[] list1, String[] list2) {
134
if (list1.length != list2.length) {
135
return false;
136
}
137
for (int i=0; i<list1.length; i++) {
138
String s1 = list1[i];
139
String s2 = list2[i];
140
if ((s1 == null && s2 != null)) {
141
return false;
142
} else if ((s2 == null && s1 != null)) {
143
return false;
144
} else if (s1 == null) {
145
return true;
146
} else if (!s1.equals(s2)) {
147
return false;
148
}
149
}
150
return true;
151
}
152
}
153
154