From 4d3e5694388b7b10d15d50efae1052998b0ff9ae Mon Sep 17 00:00:00 2001 From: Wagyourtail Date: Sat, 13 Jul 2024 06:50:48 -0500 Subject: [PATCH] if hashes match don't override file --- gradle.properties | 2 +- .../java/xyz/wagyourtail/jvmdg/cli/Flags.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index a74b5300..e5f284d5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ kotlin.code.style=official org.gradle.jvmargs=-Xmx4G org.gradle.parallel=true -version=0.8.2 +version=0.8.3 asm_version=9.7 diff --git a/src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java b/src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java index e8292087..3bca82f1 100644 --- a/src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java +++ b/src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java @@ -15,6 +15,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.security.MessageDigest; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -129,6 +130,15 @@ public File findJavaApi() { url = getJavaApiFromMaven(); } if (url != null) { + if (Files.exists(tmp)) { + try (InputStream in = url.openStream()) { + try (InputStream in2 = Files.newInputStream(tmp)) { + if (hash(in).equals(hash(in2))) { + return tmp.toFile(); + } + } + } + } try (InputStream in = url.openStream()) { Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING); } @@ -144,4 +154,24 @@ public File findJavaApi() { throw new RuntimeException("Failed to find java api", e); } } + + private String hash(InputStream is) { + MessageDigest digest = null; + try { + digest = MessageDigest.getInstance("SHA-1"); + byte[] buffer = new byte[8192]; + int read = 0; + while ((read = is.read(buffer)) > 0) { + digest.update(buffer, 0, read); + } + byte[] hash = digest.digest(); + StringBuilder sb = new StringBuilder(); + for (byte b : hash) { + sb.append(String.format("%02x", b)); + } + return sb.toString(); + } catch (Exception e) { + throw new RuntimeException("Failed to hash", e); + } + } }