Skip to content

Commit

Permalink
if hashes match don't override file
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jul 13, 2024
1 parent 9b35b61 commit 4d3e569
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
}
}

0 comments on commit 4d3e569

Please sign in to comment.