From f3042b6b4569de8c787533b0bf5ea35114510ff5 Mon Sep 17 00:00:00 2001 From: xjoao Date: Fri, 22 May 2020 13:58:45 -0400 Subject: [PATCH] Update close for try witch resources --- .../habbohotel/items/YoutubeManager.java | 52 +++++++++---------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/items/YoutubeManager.java b/src/main/java/com/eu/habbo/habbohotel/items/YoutubeManager.java index d2f3d3bf..3be71726 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/YoutubeManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/YoutubeManager.java @@ -133,37 +133,35 @@ public class YoutubeManager { InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); - - YoutubePlaylist playlist = null; - - String inputLine; - while ((inputLine = br.readLine()) != null) { - if (inputLine.contains("window[\"ytInitialData\"]")) { - JsonObject obj = new JsonParser().parse(inputLine.substring(inputLine.indexOf("{")).replace(";", "")).getAsJsonObject(); - - JsonObject meta = obj.get("microformat").getAsJsonObject().get("microformatDataRenderer").getAsJsonObject(); - String name = meta.get("title").getAsString(); - String description = meta.get("description").getAsString(); - - ArrayList videos = new ArrayList<>(); - - JsonArray rawVideos = obj.get("contents").getAsJsonObject().get("twoColumnBrowseResultsRenderer").getAsJsonObject().get("tabs").getAsJsonArray().get(0).getAsJsonObject().get("tabRenderer").getAsJsonObject().get("content").getAsJsonObject().get("sectionListRenderer").getAsJsonObject().get("contents").getAsJsonArray().get(0).getAsJsonObject().get("itemSectionRenderer").getAsJsonObject().get("contents").getAsJsonArray().get(0).getAsJsonObject().get("playlistVideoListRenderer").getAsJsonObject().get("contents").getAsJsonArray(); - - for (JsonElement rawVideo : rawVideos) { - JsonObject videoData = rawVideo.getAsJsonObject().get("playlistVideoRenderer").getAsJsonObject(); - if (!videoData.has("lengthSeconds")) continue; // removed videos - videos.add(new YoutubeVideo(videoData.get("videoId").getAsString(), Integer.valueOf(videoData.get("lengthSeconds").getAsString()))); + YoutubePlaylist playlist; + try (BufferedReader br = new BufferedReader(isr)) { + playlist = null; + String inputLine; + while ((inputLine = br.readLine()) != null) { + if (inputLine.contains("window[\"ytInitialData\"]")) { + JsonObject obj = new JsonParser().parse(inputLine.substring(inputLine.indexOf("{")).replace(";", "")).getAsJsonObject(); + + JsonObject meta = obj.get("microformat").getAsJsonObject().get("microformatDataRenderer").getAsJsonObject(); + String name = meta.get("title").getAsString(); + String description = meta.get("description").getAsString(); + + ArrayList videos = new ArrayList<>(); + + JsonArray rawVideos = obj.get("contents").getAsJsonObject().get("twoColumnBrowseResultsRenderer").getAsJsonObject().get("tabs").getAsJsonArray().get(0).getAsJsonObject().get("tabRenderer").getAsJsonObject().get("content").getAsJsonObject().get("sectionListRenderer").getAsJsonObject().get("contents").getAsJsonArray().get(0).getAsJsonObject().get("itemSectionRenderer").getAsJsonObject().get("contents").getAsJsonArray().get(0).getAsJsonObject().get("playlistVideoListRenderer").getAsJsonObject().get("contents").getAsJsonArray(); + + for (JsonElement rawVideo : rawVideos) { + JsonObject videoData = rawVideo.getAsJsonObject().get("playlistVideoRenderer").getAsJsonObject(); + if (!videoData.has("lengthSeconds")) continue; // removed videos + videos.add(new YoutubeVideo(videoData.get("videoId").getAsString(), Integer.valueOf(videoData.get("lengthSeconds").getAsString()))); + } + + playlist = new YoutubePlaylist(playlistId, name, description, videos); + + break; } - - playlist = new YoutubePlaylist(playlistId, name, description, videos); - - break; } } - br.close(); - this.playlistCache.put(playlistId, playlist); return playlist;