Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.

Commit ddd755c

Browse files
committed
Use tempfiles and atomic move when downloading boot.jar
1 parent 2d3774f commit ddd755c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/boot/Loader.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import java.util.*;
88
import java.util.jar.*;
99
import java.util.zip.*;
10+
import java.nio.file.Files;
1011
import java.util.regex.Pattern;
1112
import java.lang.reflect.Method;
13+
import static java.nio.file.StandardCopyOption.*;
1214

1315
@SuppressWarnings("unchecked")
1416
public 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

Comments
 (0)