fix(YouTube - Spoof Streaming Data): Fix memory leak in ByteArrayOutputStream (#102)

This commit is contained in:
Hoàng Gia Bảo 2024-12-08 08:06:19 +07:00 committed by GitHub
parent b31865afbe
commit cbac88b2b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -169,26 +169,26 @@ public class StreamingDataRequest {
); );
private static ByteBuffer fetch(@NonNull String videoId, Map<String, String> playerHeaders) { private static ByteBuffer fetch(@NonNull String videoId, Map<String, String> playerHeaders) {
try { lastSpoofedClientType = null;
lastSpoofedClientType = null;
// Retry with different client if empty response body is received. // Retry with different client if empty response body is received.
for (ClientType clientType : CLIENT_ORDER_TO_USE) { for (ClientType clientType : CLIENT_ORDER_TO_USE) {
HttpURLConnection connection = send(clientType, videoId, playerHeaders); HttpURLConnection connection = send(clientType, videoId, playerHeaders);
// gzip encoding doesn't response with content length (-1), // gzip encoding doesn't response with content length (-1),
// but empty response body does. // but empty response body does.
if (connection == null || connection.getContentLength() == 0) { if (connection == null || connection.getContentLength() == 0)
continue; continue;
}
try (
InputStream inputStream = new BufferedInputStream(connection.getInputStream()); InputStream inputStream = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream()
) {
byte[] buffer = new byte[2048]; byte[] buffer = new byte[2048];
int bytesRead; int bytesRead;
while ((bytesRead = inputStream.read(buffer)) >= 0) { while ((bytesRead = inputStream.read(buffer)) >= 0) {
baos.write(buffer, 0, bytesRead); baos.write(buffer, 0, bytesRead);
} }
inputStream.close();
if (clientType == ClientType.IOS && liveStreams.check(buffer).isFiltered()) { if (clientType == ClientType.IOS && liveStreams.check(buffer).isFiltered()) {
Logger.printDebug(() -> "Ignore IOS spoofing as it is a livestream (video: " + videoId + ")"); Logger.printDebug(() -> "Ignore IOS spoofing as it is a livestream (video: " + videoId + ")");
continue; continue;
@ -196,9 +196,9 @@ public class StreamingDataRequest {
lastSpoofedClientType = clientType; lastSpoofedClientType = clientType;
return ByteBuffer.wrap(baos.toByteArray()); 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); handleConnectionError("Could not fetch any client streams", null);