Ensure JarFile is always closed

dev/0.11
modmuss50 2020-04-22 18:22:22 +01:00
parent f7190b31c0
commit 26755cb82c
1 changed files with 29 additions and 31 deletions

View File

@ -83,31 +83,30 @@ public class ModProcessor {
} }
private static void handleNestedJars(File input, Project project, Configuration config, ResolvedArtifact artifact) throws IOException { private static void handleNestedJars(File input, Project project, Configuration config, ResolvedArtifact artifact) throws IOException {
JarFile jarFile = new JarFile(input); try (JarFile jarFile = new JarFile(input)) {
JarEntry modJsonEntry = jarFile.getJarEntry("fabric.mod.json"); JarEntry modJsonEntry = jarFile.getJarEntry("fabric.mod.json");
if (modJsonEntry == null) { if (modJsonEntry == null) {
return;
}
try (InputStream inputStream = jarFile.getInputStream(modJsonEntry)) {
JsonObject json = GSON.fromJson(new InputStreamReader(inputStream), JsonObject.class);
if (json == null || !json.has("jars")) {
return; return;
} }
JsonArray jsonArray = json.getAsJsonArray("jars"); try (InputStream inputStream = jarFile.getInputStream(modJsonEntry)) {
JsonObject json = GSON.fromJson(new InputStreamReader(inputStream), JsonObject.class);
for (int i = 0; i < jsonArray.size(); i++) { if (json == null || !json.has("jars")) {
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject(); return;
String fileName = jsonObject.get("file").getAsString(); }
project.getLogger().lifecycle(String.format("Found %s nested in %s", fileName, input.getName()));
processNestedJar(jarFile, fileName, project, config, artifact); JsonArray jsonArray = json.getAsJsonArray("jars");
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
String fileName = jsonObject.get("file").getAsString();
project.getLogger().lifecycle(String.format("Found %s nested in %s", fileName, input.getName()));
processNestedJar(jarFile, fileName, project, config, artifact);
}
} }
} }
jarFile.close();
} }
private static void processNestedJar(JarFile parentJar, String fileName, Project project, Configuration config, ResolvedArtifact artifact) throws IOException { private static void processNestedJar(JarFile parentJar, String fileName, Project project, Configuration config, ResolvedArtifact artifact) throws IOException {
@ -150,26 +149,25 @@ public class ModProcessor {
} }
private static void remapaccessWidener(File input, Project project) throws IOException { private static void remapaccessWidener(File input, Project project) throws IOException {
JarFile jarFile = new JarFile(input);
JarEntry modJsonEntry = jarFile.getJarEntry("fabric.mod.json");
if (modJsonEntry == null) {
return;
}
String accessWidenerPath; String accessWidenerPath;
try (InputStream inputStream = jarFile.getInputStream(modJsonEntry)) { try (JarFile jarFile = new JarFile(input)) {
JsonObject json = GSON.fromJson(new InputStreamReader(inputStream), JsonObject.class); JarEntry modJsonEntry = jarFile.getJarEntry("fabric.mod.json");
if (!json.has("accessWidener")) { if (modJsonEntry == null) {
return; return;
} }
accessWidenerPath = json.get("accessWidener").getAsString(); try (InputStream inputStream = jarFile.getInputStream(modJsonEntry)) {
} JsonObject json = GSON.fromJson(new InputStreamReader(inputStream), JsonObject.class);
jarFile.close(); if (!json.has("accessWidener")) {
return;
}
accessWidenerPath = json.get("accessWidener").getAsString();
}
}
if (accessWidenerPath == null) { if (accessWidenerPath == null) {
return; return;