Skip to content

Commit

Permalink
fix(YouTube - Spoof streaming data): Fix memory leak in `ByteArrayOut…
Browse files Browse the repository at this point in the history
…putStream`
  • Loading branch information
YT-Advanced authored and anddea committed Dec 11, 2024
1 parent 7dde697 commit 42d7bbe
Showing 1 changed file with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,36 +169,36 @@ private static HttpURLConnection send(ClientType clientType, String videoId,
);

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;
}
InputStream inputStream = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 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()
) {
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;
}
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);
Expand Down Expand Up @@ -239,4 +239,4 @@ public ByteBuffer getStream() {
public String toString() {
return "StreamingDataRequest{" + "videoId='" + videoId + '\'' + '}';
}
}
}

0 comments on commit 42d7bbe

Please sign in to comment.