Skip to content

Commit 8ac2119

Browse files
authored
Merge pull request #42 from neziw/fix-resources-leak
Ensure InputStream and BufferedReader are closed properly
2 parents f9b2afd + d706e28 commit 8ac2119

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,19 @@ private PEVersion getLatestVersion() throws IOException {
5757
URL url = new URL("https://api.github.com/repos/" + org + "/" + repo + "/releases/latest");
5858
URLConnection connection = url.openConnection();
5959
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
60-
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
61-
BufferedReader reader = new BufferedReader(isr);
62-
String response = reader.readLine();
63-
JsonObject json = AdventureSerializer.getGsonSerializer().serializer().fromJson(response, JsonObject.class);
6460

65-
reader.close();
66-
isr.close();
61+
try (
62+
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
63+
BufferedReader reader = new BufferedReader(isr)
64+
) {
65+
String response = reader.readLine();
66+
JsonObject json = AdventureSerializer.getGsonSerializer().serializer().fromJson(response, JsonObject.class);
6767

68-
if (json.has("tag_name")) {
69-
return PEVersion.fromString(json.get("tag_name").getAsString().replaceFirst("^[vV]", ""));
68+
if (json.has("tag_name")) {
69+
return PEVersion.fromString(json.get("tag_name").getAsString().replaceFirst("^[vV]", ""));
70+
}
71+
throw new IOException("Could not find name attribute in github api fetch");
7072
}
71-
throw new IOException("Could not find name attribute in github api fetch");
7273
}
7374

7475
@Deprecated

0 commit comments

Comments
 (0)