Merge pull request #42 from neziw/fix-resources-leak

Ensure InputStream and BufferedReader are closed properly
This commit is contained in:
Tofaa 2025-08-06 06:19:47 +04:00 committed by GitHub
commit 8ac2119c67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -57,19 +57,20 @@ public final class GithubUpdater {
URL url = new URL("https://api.github.com/repos/" + org + "/" + repo + "/releases/latest"); URL url = new URL("https://api.github.com/repos/" + org + "/" + repo + "/releases/latest");
URLConnection connection = url.openConnection(); URLConnection connection = url.openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/5.0"); connection.addRequestProperty("User-Agent", "Mozilla/5.0");
try (
InputStreamReader isr = new InputStreamReader(connection.getInputStream()); InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr); BufferedReader reader = new BufferedReader(isr)
) {
String response = reader.readLine(); String response = reader.readLine();
JsonObject json = AdventureSerializer.getGsonSerializer().serializer().fromJson(response, JsonObject.class); JsonObject json = AdventureSerializer.getGsonSerializer().serializer().fromJson(response, JsonObject.class);
reader.close();
isr.close();
if (json.has("tag_name")) { if (json.has("tag_name")) {
return PEVersion.fromString(json.get("tag_name").getAsString().replaceFirst("^[vV]", "")); 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 @Deprecated
public String getCurrentVersion() { public String getCurrentVersion() {