From d706e2888f8de590bedac9ebcc11dbfd878e1002 Mon Sep 17 00:00:00 2001 From: neziw Date: Mon, 4 Aug 2025 21:37:41 +0200 Subject: [PATCH] Ensure InputStream and BufferedReader are closed properly Replaces manual closing of InputStreamReader and BufferedReader with try-with-resources for safer and cleaner resource management in the getLatestReleaseVersion method. --- .../tofaa/entitylib/utils/GithubUpdater.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java b/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java index a753dec..95bea5a 100644 --- a/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java +++ b/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java @@ -57,18 +57,19 @@ public final class GithubUpdater { URL url = new URL("https://api.github.com/repos/" + org + "/" + repo + "/releases/latest"); URLConnection connection = url.openConnection(); connection.addRequestProperty("User-Agent", "Mozilla/5.0"); - InputStreamReader isr = new InputStreamReader(connection.getInputStream()); - BufferedReader reader = new BufferedReader(isr); - String response = reader.readLine(); - JsonObject json = AdventureSerializer.getGsonSerializer().serializer().fromJson(response, JsonObject.class); - reader.close(); - isr.close(); + try ( + InputStreamReader isr = new InputStreamReader(connection.getInputStream()); + BufferedReader reader = new BufferedReader(isr) + ) { + String response = reader.readLine(); + JsonObject json = AdventureSerializer.getGsonSerializer().serializer().fromJson(response, JsonObject.class); - if (json.has("tag_name")) { - return PEVersion.fromString(json.get("tag_name").getAsString().replaceFirst("^[vV]", "")); + if (json.has("tag_name")) { + return PEVersion.fromString(json.get("tag_name").getAsString().replaceFirst("^[vV]", "")); + } + throw new IOException("Could not find name attribute in github api fetch"); } - throw new IOException("Could not find name attribute in github api fetch"); } @Deprecated