1 /* 2 * Copyright (c) 2003, 2016, 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 Server(ServerSocket s)42 Server(ServerSocket s) { 43 srv = s; 44 } 45 readOneRequest(InputStream is)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 run()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 read(InputStream is)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 main(String[] args)96 public static void main (String[] args) throws Exception { 97 String tmpdir = System.getProperty("java.io.tmpdir"); 98 String[] list1 = listTmpFiles(tmpdir); 99 ServerSocket serverSocket = new ServerSocket(0); 100 server = new Server(serverSocket); 101 server.start(); 102 int port = serverSocket.getLocalPort(); 103 System.out.println ("Server: listening on port: " + port); 104 URL url = new URL ("jar:http://localhost:"+port+"!/COPYRIGHT"); 105 try { 106 URLConnection urlc = url.openConnection (); 107 InputStream is = urlc.getInputStream(); 108 read (is); 109 is.close(); 110 } catch (IOException e) { 111 System.out.println ("Received IOException as expected"); 112 } 113 String[] list2 = listTmpFiles(tmpdir); 114 if (!sameList (list1, list2)) { 115 throw new RuntimeException ("some jar_cache files left behind"); 116 } 117 } 118 listTmpFiles(String d)119 static String[] listTmpFiles (String d) { 120 File dir = new File (d); 121 return dir.list (new FilenameFilter () { 122 public boolean accept (File dr, String name) { 123 return (name.startsWith ("jar_cache")); 124 } 125 }); 126 } 127 128 static boolean sameList (String[] list1, String[] list2) { 129 if (list1.length != list2.length) { 130 return false; 131 } 132 for (int i=0; i<list1.length; i++) { 133 String s1 = list1[i]; 134 String s2 = list2[i]; 135 if ((s1 == null && s2 != null)) { 136 return false; 137 } else if ((s2 == null && s1 != null)) { 138 return false; 139 } else if (s1 == null) { 140 return true; 141 } else if (!s1.equals(s2)) { 142 return false; 143 } 144 } 145 return true; 146 } 147 } 148