From cbac88b2b5da8c143bedd5706f4590bae18eb0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ho=C3=A0ng=20Gia=20B=E1=BA=A3o?= <70064328+YT-Advanced@users.noreply.github.com> Date: Sun, 8 Dec 2024 08:06:19 +0700 Subject: [PATCH] fix(YouTube - Spoof Streaming Data): Fix memory leak in `ByteArrayOutputStream` (#102) --- .../misc/requests/StreamingDataRequest.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/misc/requests/StreamingDataRequest.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/misc/requests/StreamingDataRequest.java index 6d09ccc05..73d28f339 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/misc/requests/StreamingDataRequest.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/misc/requests/StreamingDataRequest.java @@ -169,26 +169,26 @@ public class StreamingDataRequest { ); private static ByteBuffer fetch(@NonNull String videoId, Map playerHeaders) { - try { - lastSpoofedClientType = null; + lastSpoofedClientType = null; - // Retry with different client if empty response body is received. - for (ClientType clientType : CLIENT_ORDER_TO_USE) { - HttpURLConnection connection = send(clientType, videoId, playerHeaders); + // Retry with different client if empty response body is received. + for (ClientType clientType : CLIENT_ORDER_TO_USE) { + HttpURLConnection connection = send(clientType, videoId, playerHeaders); - // gzip encoding doesn't response with content length (-1), - // but empty response body does. - if (connection == null || connection.getContentLength() == 0) { - continue; - } + // gzip encoding doesn't response with content length (-1), + // but empty response body does. + if (connection == null || connection.getContentLength() == 0) + continue; + + try ( InputStream inputStream = new BufferedInputStream(connection.getInputStream()); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream() + ) { byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) >= 0) { baos.write(buffer, 0, bytesRead); } - inputStream.close(); if (clientType == ClientType.IOS && liveStreams.check(buffer).isFiltered()) { Logger.printDebug(() -> "Ignore IOS spoofing as it is a livestream (video: " + videoId + ")"); continue; @@ -196,9 +196,9 @@ public class StreamingDataRequest { lastSpoofedClientType = clientType; return ByteBuffer.wrap(baos.toByteArray()); + } catch (IOException ex) { + Logger.printException(() -> "Fetch failed while processing response data", ex); } - } catch (IOException ex) { - Logger.printException(() -> "Fetch failed while processing response data", ex); } handleConnectionError("Could not fetch any client streams", null);