1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 4957695
* @run main/othervm -Djava.io.tmpdir=. B4957695
* @summary URLJarFile.retrieve does not delete tmpFile on IOException
*/
import java.io.*;
import java.net.*;
public class B4957695 {
static Server server;
static class Server extends Thread {
final ServerSocket srv;
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n'};
Server(ServerSocket s) {
srv = s;
}
void readOneRequest(InputStream is) throws IOException {
int requestEndCount = 0, r;
while ((r = is.read()) != -1) {
if (r == requestEnd[requestEndCount]) {
requestEndCount++;
if (requestEndCount == 4) {
break;
}
} else {
requestEndCount = 0;
}
}
}
public void run() {
try (Socket s = srv.accept()) {
// read HTTP request from client
readOneRequest(s.getInputStream());
try (OutputStreamWriter ow =
new OutputStreamWriter((s.getOutputStream()))) {
FileInputStream fin = new FileInputStream(new File(
System.getProperty("test.src", "."), "foo1.jar"));
int length = fin.available();
byte[] b = new byte[length-10];
fin.read(b, 0, length-10);
ow.write("HTTP/1.0 200 OK\r\n");
// Note: The client expects length bytes.
ow.write("Content-Length: " + length + "\r\n");
ow.write("Content-Type: text/html\r\n");
ow.write("\r\n");
// Note: The (buggy) server only sends length-10 bytes.
ow.write(new String(b));
ow.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
static void read (InputStream is) throws IOException {
int c,len=0;
while ((c=is.read()) != -1) {
len += c;
}
System.out.println ("read " + len + " bytes");
}
public static void main (String[] args) throws Exception {
String tmpdir = System.getProperty("java.io.tmpdir");
String[] list1 = listTmpFiles(tmpdir);
ServerSocket serverSocket = new ServerSocket(0);
server = new Server(serverSocket);
server.start();
int port = serverSocket.getLocalPort();
System.out.println ("Server: listening on port: " + port);
URL url = new URL ("jar:http://localhost:"+port+"!/COPYRIGHT");
try {
URLConnection urlc = url.openConnection ();
InputStream is = urlc.getInputStream();
read (is);
is.close();
} catch (IOException e) {
System.out.println ("Received IOException as expected");
}
String[] list2 = listTmpFiles(tmpdir);
if (!sameList (list1, list2)) {
throw new RuntimeException ("some jar_cache files left behind");
}
}
static String[] listTmpFiles (String d) {
File dir = new File (d);
return dir.list (new FilenameFilter () {
public boolean accept (File dr, String name) {
return (name.startsWith ("jar_cache"));
}
});
}
static boolean sameList (String[] list1, String[] list2) {
if (list1.length != list2.length) {
return false;
}
for (int i=0; i<list1.length; i++) {
String s1 = list1[i];
String s2 = list2[i];
if ((s1 == null && s2 != null)) {
return false;
} else if ((s2 == null && s1 != null)) {
return false;
} else if (s1 == null) {
return true;
} else if (!s1.equals(s2)) {
return false;
}
}
return true;
}
}
|