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