diff --git a/src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java b/src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java index 0cfec9b3..d2f9c9fc 100644 --- a/src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java +++ b/src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java @@ -127,26 +127,32 @@ public File findJavaApi() { } synchronized (Flags.class) { Constants.DIR.mkdirs(); - Path tmp = Constants.DIR.toPath().resolve("jvmdg-api.jar"); + Path tmp = Constants.DIR.toPath().resolve("jvmdg-api- " + jvmdgVersion + " .jar"); File prop = getJavaApiFromSystemProperty(); if (prop != null) { return prop; } URL url = getJavaApiFromShade(); + if (Files.exists(tmp)) { + if (url == null) { + foundApi = tmp.toFile(); + return tmp.toFile(); + } else { + try (InputStream in = url.openStream()) { + try (InputStream in2 = Files.newInputStream(tmp)) { + if (hash(in).equals(hash(in2))) { + foundApi = tmp.toFile(); + return tmp.toFile(); + } + } + } + + } + } if (url == null && allowMaven) { 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))) { -// foundApi = tmp.toFile(); -// return tmp.toFile(); -// } -// } -// } -// } try (InputStream in = url.openStream()) { Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING); foundApi = tmp.toFile(); @@ -160,23 +166,24 @@ public File findJavaApi() { } } -// 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); -// } -// } + 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); + } + } + }