Skip to content

Commit

Permalink
just statically cache the found api
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jul 13, 2024
1 parent 4d3e569 commit a74658c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
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.3
version=0.8.4

asm_version=9.7

Expand Down
93 changes: 49 additions & 44 deletions src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,39 +114,44 @@ private URL getJavaApiFromMaven() throws IOException {
}
}

private static File foundApi = null;

public File findJavaApi() {
try {
if (api != null) {
return api;
}
Constants.DIR.mkdirs();
Path tmp = Constants.DIR.toPath().resolve("jvmdg-api.jar");
File prop = getJavaApiFromSystemProperty();
if (prop != null) {
return prop;
}
URL url = getJavaApiFromShade();
if (url == null && allowMaven) {
url = getJavaApiFromMaven();
if (foundApi != null) {
api = foundApi;
return foundApi;
}
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();
}
}
}
synchronized (Flags.class) {
Constants.DIR.mkdirs();
Path tmp = Constants.DIR.toPath().resolve("jvmdg-api.jar");
File prop = getJavaApiFromSystemProperty();
if (prop != null) {
return prop;
}
try (InputStream in = url.openStream()) {
Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING);
URL url = getJavaApiFromShade();
if (url == null && allowMaven) {
url = getJavaApiFromMaven();
}
return tmp.toFile();
} else {
// failed to find java api
if (!quiet) {
System.err.println("[JvmDowngrader] Failed to find java api jar!");
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();
return tmp.toFile();
}
}
return null;
}
Expand All @@ -155,23 +160,23 @@ 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);
// }
// }
}

0 comments on commit a74658c

Please sign in to comment.