77import java .util .*;
88import java .util .jar .*;
99import java .util .zip .*;
10+ import java .nio .file .Files ;
1011import java .util .regex .Pattern ;
1112import java .lang .reflect .Method ;
13+ import static java .nio .file .StandardCopyOption .*;
1214
1315@ SuppressWarnings ("unchecked" )
1416public class Loader {
@@ -163,16 +165,18 @@ public class Loader {
163165
164166 public static File
165167 download (String url , File f ) throws Exception {
166- mkParents (f );
167- if (f .exists ()) f .delete ();
168+ File tmp = File .createTempFile ("boot" , ".jar" );
168169 int n = -1 ;
169170 byte [] buf = new byte [4096 ];
171+
170172 System .err .print ("Downloading " + url + "..." );
171173 try (InputStream is = (new URL (url )).openStream ();
172- OutputStream os = new FileOutputStream (f )) {
173- while (-1 != (n = is .read (buf )))
174- os .write (buf , 0 , n ); }
174+ OutputStream os = new FileOutputStream (tmp )) {
175+ while (-1 != (n = is .read (buf ))) os .write (buf , 0 , n ); }
175176 System .err .println ("done." );
177+
178+ mkParents (f );
179+ Files .move (tmp .toPath (), f .toPath (), REPLACE_EXISTING , ATOMIC_MOVE );
176180 return f ; }
177181
178182 public static String
@@ -182,9 +186,7 @@ public class Loader {
182186 public static File
183187 validateBinaryFile (File f ) throws Exception {
184188 if (f == null ) return null ;
185- try (FileInputStream fis = new FileInputStream (f );
186- JarInputStream jis = new JarInputStream (fis )) {
187- for (ZipEntry e = jis .getNextEntry (); e != null ; e = jis .getNextEntry ()); }
189+ try (ZipFile z = new ZipFile (f )) { }
188190 catch (IOException ie ) { f = null ; }
189191 return f ; }
190192
0 commit comments