forked from j62/ctbrec
1
0
Fork 0

Made sure to close okhttp responses properly to avoid resource leaks

This commit is contained in:
0xboobface 2018-07-25 14:06:30 +02:00
parent c3d916e033
commit 6136f095ec
2 changed files with 71 additions and 54 deletions

View File

@ -38,6 +38,7 @@ public class Chaturbate {
.addHeader("X-Requested-With", "XMLHttpRequest")
.build();
Response response = client.execute(req);
try {
if(response.isSuccessful()) {
String content = response.body().string();
LOG.debug("Raw stream info: {}", content);
@ -48,9 +49,11 @@ public class Chaturbate {
} else {
int code = response.code();
String message = response.message();
response.close();
throw new IOException("Server responded with " + code + " - " + message + " headers: [" + response.headers() + "]");
}
} finally {
response.close();
}
}
public static int[] getResolution(Model model, HttpClient client) throws IOException, ParseException, PlaylistException {
@ -83,10 +86,14 @@ public class Chaturbate {
LOG.trace("Loading master playlist {}", streamInfo.url);
Request req = new Request.Builder().url(streamInfo.url).build();
Response response = client.execute(req);
try {
InputStream inputStream = response.body().byteStream();
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8);
Playlist playlist = parser.parse();
MasterPlaylist master = playlist.getMasterPlaylist();
return master;
} finally {
response.close();
}
}
}

View File

@ -146,6 +146,7 @@ public class HlsDownload implements Download {
URL segmentsUrl = new URL(segments);
Request request = new Request.Builder().url(segmentsUrl).addHeader("connection", "keep-alive").build();
Response response = client.execute(request);
try {
InputStream inputStream = response.body().byteStream();
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8);
Playlist playlist = parser.parse();
@ -169,11 +170,15 @@ public class HlsDownload implements Download {
return lsp;
}
return null;
} finally {
response.close();
}
}
private String parseMaster(String url, int streamUrlIndex) throws IOException, ParseException, PlaylistException {
Request request = new Request.Builder().url(url).addHeader("connection", "keep-alive").build();
Response response = client.execute(request);
try {
InputStream inputStream = response.body().byteStream();
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8);
@ -195,6 +200,9 @@ public class HlsDownload implements Download {
}
}
return null;
} finally {
response.close();
}
}
public static class LiveStreamingPlaylist {
@ -238,6 +246,8 @@ public class HlsDownload implements Download {
break;
} catch(Exception e) {
LOG.error("Error while downloading segment. Retrying " + i, e);
} finally {
response.close();
}
}
return false;