Improve recording download from server

This commit is contained in:
0xboobface 2019-12-29 15:39:44 +01:00
parent 77e0a0737a
commit e0f494d6da
1 changed files with 16 additions and 14 deletions

View File

@ -22,6 +22,7 @@ import ctbrec.Hmac;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.OS; import ctbrec.OS;
import ctbrec.io.HttpClient; import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import ctbrec.io.StreamRedirectThread; import ctbrec.io.StreamRedirectThread;
import ctbrec.recorder.ProgressListener; import ctbrec.recorder.ProgressListener;
import ctbrec.recorder.RecordingManager; import ctbrec.recorder.RecordingManager;
@ -150,21 +151,22 @@ public class MergedHlsDownload extends HlsDownload {
private void downloadFile(String fileUri, File tempDir) throws IOException { private void downloadFile(String fileUri, File tempDir) throws IOException {
LOG.trace("Downloading file {} to {}", fileUri, tempDir); LOG.trace("Downloading file {} to {}", fileUri, tempDir);
Request request = new Request.Builder().url(fileUri).addHeader("connection", "keep-alive").build(); Request request = new Request.Builder().url(fileUri).addHeader("connection", "keep-alive").build();
Response response = client.execute(request); try (Response response = client.execute(request)) {
InputStream in = null; if (response.isSuccessful()) {
File file = new File(request.url().encodedPath()); InputStream in = null;
try (FileOutputStream fos = new FileOutputStream(new File(tempDir, file.getName()))) { File file = new File(request.url().encodedPath());
in = response.body().byteStream(); File downloadedSegment = new File(tempDir, file.getName());
byte[] b = new byte[1024 * 100]; try (FileOutputStream fos = new FileOutputStream(downloadedSegment)) {
int length = -1; in = response.body().byteStream();
while ((length = in.read(b)) >= 0) { byte[] b = new byte[1024 * 100];
fos.write(b, 0, length); int length = -1;
while ((length = in.read(b)) >= 0) {
fos.write(b, 0, length);
}
}
} else {
throw new HttpException(response.code(), response.message());
} }
} finally {
if (in != null) {
in.close();
}
response.close();
} }
} }