forked from j62/ctbrec
1
0
Fork 0

Add try-with-resources blocks to free up the HLS playlist file handle

This commit is contained in:
0xboobface 2019-12-22 15:01:49 +01:00
parent 9df30b3342
commit 5b9b91d6e8
2 changed files with 29 additions and 25 deletions

View File

@ -135,23 +135,25 @@ public class PlaylistGenerator {
public void validate(File recDir) throws IOException, ParseException, PlaylistException { public void validate(File recDir) throws IOException, ParseException, PlaylistException {
File playlist = new File(recDir, "playlist.m3u8"); File playlist = new File(recDir, "playlist.m3u8");
if(playlist.exists()) { if(playlist.exists()) {
PlaylistParser playlistParser = new PlaylistParser(new FileInputStream(playlist), Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT); try(FileInputStream fin = new FileInputStream(playlist)) {
Playlist m3u = playlistParser.parse(); PlaylistParser playlistParser = new PlaylistParser(fin, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
MediaPlaylist mediaPlaylist = m3u.getMediaPlaylist(); Playlist m3u = playlistParser.parse();
int playlistSize = mediaPlaylist.getTracks().size(); MediaPlaylist mediaPlaylist = m3u.getMediaPlaylist();
File[] segments = recDir.listFiles(new FilenameFilter() { int playlistSize = mediaPlaylist.getTracks().size();
@Override File[] segments = recDir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) { @Override
return name.endsWith(".ts"); public boolean accept(File dir, String name) {
} return name.endsWith(".ts");
}); }
if(segments.length == 0) { });
throw new InvalidPlaylistException("No segments found. Playlist is empty"); if(segments.length == 0) {
} else if(segments.length != playlistSize) { throw new InvalidPlaylistException("No segments found. Playlist is empty");
throw new InvalidPlaylistException("Playlist size and amount of segments differ (" + segments.length + " != " + playlistSize + ")"); } else if(segments.length != playlistSize) {
} else { throw new InvalidPlaylistException("Playlist size and amount of segments differ (" + segments.length + " != " + playlistSize + ")");
LOG.debug("Generated playlist looks good"); } else {
} LOG.debug("Generated playlist looks good");
}
}
} else { } else {
throw new FileNotFoundException(playlist.getAbsolutePath() + " does not exist"); throw new FileNotFoundException(playlist.getAbsolutePath() + " does not exist");
} }

View File

@ -332,14 +332,16 @@ public class HlsDownload extends AbstractHlsDownload {
private double getPlaylistLength(File playlist) throws IOException, ParseException, PlaylistException { private double getPlaylistLength(File playlist) throws IOException, ParseException, PlaylistException {
if (playlist.exists()) { if (playlist.exists()) {
PlaylistParser playlistParser = new PlaylistParser(new FileInputStream(playlist), Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT); try(FileInputStream fin = new FileInputStream(playlist)) {
Playlist m3u = playlistParser.parse(); PlaylistParser playlistParser = new PlaylistParser(fin, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
MediaPlaylist mediaPlaylist = m3u.getMediaPlaylist(); Playlist m3u = playlistParser.parse();
double length = 0; MediaPlaylist mediaPlaylist = m3u.getMediaPlaylist();
for (TrackData trackData : mediaPlaylist.getTracks()) { double length = 0;
length += trackData.getTrackInfo().duration; for (TrackData trackData : mediaPlaylist.getTracks()) {
} length += trackData.getTrackInfo().duration;
return length; }
return length;
}
} else { } else {
throw new FileNotFoundException(playlist.getAbsolutePath() + " does not exist"); throw new FileNotFoundException(playlist.getAbsolutePath() + " does not exist");
} }