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) {
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);