if (directDownloadEnabled) {
updateStateAndNotifyListener(DownloadState.MEDIA_IN_PROGRESS);
HttpRequest request = requestFactory.buildGetRequest(requestUrl);
HttpResponse response = request.execute();
try {
// All required bytes have been downloaded from the server.
mediaContentLength = response.getHeaders().getContentLength();
bytesDownloaded = mediaContentLength;
updateStateAndNotifyListener(DownloadState.MEDIA_COMPLETE);
AbstractInputStreamContent.copy(response.getContent(), outputStream);
} finally {
response.disconnect();
}
return;
}
// Download the media content in chunks.
while (true) {
HttpRequest request = requestFactory.buildGetRequest(requestUrl);
request.getHeaders().setRange(
"bytes=" + bytesDownloaded + "-" + (bytesDownloaded + chunkSize - 1));
if (backOffPolicyEnabled) {
// Set ExponentialBackOffPolicy as the BackOffPolicy of the HTTP Request which will
// retry the same request again if there is a server error.
request.setBackOffPolicy(new ExponentialBackOffPolicy());
}
HttpResponse response = request.execute();
AbstractInputStreamContent.copy(response.getContent(), outputStream);
String contentRange = response.getHeaders().getContentRange();
long nextByteIndex = getNextByteIndex(contentRange);
setMediaContentLength(contentRange);
if (mediaContentLength <= nextByteIndex) {
// All required bytes have been downloaded from the server.