1 /*
   2  * Copyright (c) 1999, 2018, 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 import java.io.File;
  25 import java.io.FileInputStream;
  26 import java.io.FileOutputStream;
  27 import java.io.OutputStream;
  28 
  29 import java.net.InetAddress;
  30 import java.net.InetSocketAddress;
  31 import java.net.JarURLConnection;
  32 import java.net.URL;
  33 
  34 import java.util.jar.JarEntry;
  35 import java.util.jar.JarFile;
  36 import java.util.jar.JarOutputStream;
  37 
  38 import com.sun.net.httpserver.*;
  39 
  40 
  41 public class DeleteTempJar
  42 {
  43     public static void realMain(String args[]) throws Exception
  44     {
  45         final File zf = File.createTempFile("deletetemp", ".jar");
  46         zf.deleteOnExit();
  47         try (FileOutputStream fos = new FileOutputStream(zf);
  48              JarOutputStream jos = new JarOutputStream(fos))
  49         {
  50             JarEntry je = new JarEntry("entry");
  51             jos.putNextEntry(je);
  52             jos.write("hello, world".getBytes("ASCII"));
  53         }
  54 
  55         HttpServer server = HttpServer.create(
  56                 new InetSocketAddress((InetAddress) null, 0), 0);
  57         HttpContext context = server.createContext("/",
  58             new HttpHandler() {
  59                 public void handle(HttpExchange e) {
  60                     try (FileInputStream fis = new FileInputStream(zf)) {
  61                         e.sendResponseHeaders(200, zf.length());
  62                         OutputStream os = e.getResponseBody();
  63                         byte[] buf = new byte[1024];
  64                         int count = 0;
  65                         while ((count = fis.read(buf)) != -1) {
  66                             os.write(buf, 0, count);
  67                         }
  68                     } catch (Exception ex) {
  69                         unexpected(ex);
  70                     } finally {
  71                         e.close();
  72                     }
  73                 }
  74             });
  75         server.start();
  76 
  77         URL url = new URL("jar:http://localhost:"
  78                           + new Integer(server.getAddress().getPort()).toString()
  79                           + "/deletetemp.jar!/");
  80         JarURLConnection c = (JarURLConnection)url.openConnection();
  81         JarFile f = c.getJarFile();
  82         check(f.getEntry("entry") != null);
  83         System.out.println(f.getName());
  84         server.stop(0);
  85     }
  86 
  87     //--------------------- Infrastructure ---------------------------
  88     static volatile int passed = 0, failed = 0;
  89     static boolean pass() {passed++; return true;}
  90     static boolean fail() {failed++; Thread.dumpStack(); return false;}
  91     static boolean fail(String msg) {System.out.println(msg); return fail();}
  92     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
  93     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
  94     static boolean equal(Object x, Object y) {
  95         if (x == null ? y == null : x.equals(y)) return pass();
  96         else return fail(x + " not equal to " + y);}
  97     public static void main(String[] args) throws Throwable {
  98         try {realMain(args);} catch (Throwable t) {unexpected(t);}
  99         if (failed > 0) throw new AssertionError("Some tests failed");}
 100 }