From 44a2e87f8197797d855672d885f61ab5382b34b5 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Sat, 11 Apr 2020 06:50:09 -0400 Subject: [PATCH] refactor: support more random suffix to prevent multi-thread crash --- brut.j.util/src/main/java/brut/util/Jar.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/brut.j.util/src/main/java/brut/util/Jar.java b/brut.j.util/src/main/java/brut/util/Jar.java index eb0bffcc..96499001 100644 --- a/brut.j.util/src/main/java/brut/util/Jar.java +++ b/brut.j.util/src/main/java/brut/util/Jar.java @@ -85,19 +85,16 @@ abstract public class Jar { if (in == null) { throw new FileNotFoundException(resourcePath); } - long n = ThreadLocalRandom.current().nextLong(); - if (n == Long.MIN_VALUE) { - n = 0; // corner case - } else { - n = Math.abs(n); - } - String suffix = Long.toString(n) + ".tmp"; - File fileOut = File.createTempFile(tmpPrefix, suffix); + long suffix = ThreadLocalRandom.current().nextLong(); + suffix = suffix == Long.MIN_VALUE ? 0 : Math.abs(suffix); + File fileOut = File.createTempFile(tmpPrefix, suffix + ".tmp"); fileOut.deleteOnExit(); + OutputStream out = new FileOutputStream(fileOut); IOUtils.copy(in, out); in.close(); out.close(); + return fileOut; } catch (IOException ex) { throw new BrutException("Could not extract resource: " + resourcePath, ex);