use of com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException in project FileDownloader by lingochamp.
the class DownloadLaunchRunnable method isRetry.
@Override
public boolean isRetry(Exception exception) {
if (exception instanceof FileDownloadHttpException) {
final FileDownloadHttpException httpException = (FileDownloadHttpException) exception;
final int code = httpException.getCode();
if (isSingleConnection && code == HTTP_REQUESTED_RANGE_NOT_SATISFIABLE) {
if (!isTriedFixRangeNotSatisfiable) {
FileDownloadUtils.deleteTaskFiles(model.getTargetFilePath(), model.getTempFilePath());
isTriedFixRangeNotSatisfiable = true;
return true;
}
}
}
return validRetryTimes > 0 && !(exception instanceof FileDownloadGiveUpRetryException);
}
use of com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException in project FileDownloader by lingochamp.
the class DownloadRunnable method run.
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
FileDownloadConnection connection = null;
final long beginOffset = connectTask.getProfile().currentOffset;
boolean isConnected = false;
do {
try {
if (paused) {
return;
}
isConnected = false;
connection = connectTask.connect();
final int code = connection.getResponseCode();
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "the connection[%d] for %d, is connected %s with code[%d]", connectionIndex, downloadId, connectTask.getProfile(), code);
}
if (code != HttpURLConnection.HTTP_PARTIAL && code != HttpURLConnection.HTTP_OK) {
throw new SocketException(FileDownloadUtils.formatString("Connection failed with request[%s] response[%s] " + "http-state[%d] on task[%d-%d], which is changed" + " after verify connection, so please try again.", connectTask.getRequestHeader(), connection.getResponseHeaderFields(), code, downloadId, connectionIndex));
}
isConnected = true;
final FetchDataTask.Builder builder = new FetchDataTask.Builder();
if (paused)
return;
fetchDataTask = builder.setDownloadId(downloadId).setConnectionIndex(connectionIndex).setCallback(callback).setHost(this).setWifiRequired(isWifiRequired).setConnection(connection).setConnectionProfile(this.connectTask.getProfile()).setPath(path).build();
fetchDataTask.run();
if (paused)
fetchDataTask.pause();
break;
} catch (IllegalAccessException | IOException | FileDownloadGiveUpRetryException | IllegalArgumentException e) {
if (callback.isRetry(e)) {
if (isConnected && fetchDataTask == null) {
// connected but create fetch data task failed, give up directly.
FileDownloadLog.w(this, "it is valid to retry and connection is valid but" + " create fetch-data-task failed, so give up directly with %s", e);
callback.onError(e);
break;
} else {
if (fetchDataTask != null) {
// update currentOffset in ConnectionProfile
final long downloadedOffset = getDownloadedOffset();
if (downloadedOffset > 0) {
connectTask.updateConnectionProfile(downloadedOffset);
}
}
callback.onRetry(e);
}
} else {
callback.onError(e);
break;
}
} finally {
if (connection != null)
connection.ending();
}
} while (true);
}
use of com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException in project FileDownloader by lingochamp.
the class FileDownloadRunnable method run.
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
isPending = false;
isRunning = true;
try {
// Step 1, check model
if (model == null) {
FileDownloadLog.e(this, "start runnable but model == null?? %s", mId);
this.model = helper.find(mId);
if (this.model == null) {
FileDownloadLog.e(this, "start runnable but downloadMode == null?? %s", mId);
return;
}
}
// Step 2, check status
if (model.getStatus() != FileDownloadStatus.pending) {
if (model.getStatus() == FileDownloadStatus.paused) {
if (FileDownloadLog.NEED_LOG) {
/**
* @see FileDownloadThreadPool#cancel(int), the invoking simultaneously
* with here. And this area is invoking before there, so, {@code cancel(int)}
* is fail.
*
* High concurrent cause.
*/
FileDownloadLog.d(this, "High concurrent cause, start runnable but " + "already paused %d", mId);
}
} else {
onError(new RuntimeException(FileDownloadUtils.formatString("Task[%d] can't start the download" + " runnable, because its status is %d not %d", mId, model.getStatus(), FileDownloadStatus.pending)));
}
return;
}
if (mIsWifiRequired && !FileDownloadUtils.checkPermission(Manifest.permission.ACCESS_NETWORK_STATE)) {
onError(new FileDownloadGiveUpRetryException(FileDownloadUtils.formatString("Task[%d] can't start the download runnable," + " because this task require wifi, but user application " + "nor current process has %s, so we can't check whether " + "the network type connection.", mId, Manifest.permission.ACCESS_NETWORK_STATE)));
return;
}
onStarted();
// Step 3, start download
loop(model);
} finally {
isRunning = false;
}
}
use of com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException in project FileDownloader by lingochamp.
the class FileDownloadRunnable method loop.
@SuppressWarnings("ConstantConditions")
private void loop(FileDownloadModel model) {
int retryingTimes = 0;
boolean revisedInterval = false;
FileDownloadConnection connection = null;
do {
// loop for retry
long soFar = 0;
final int id = mId;
try {
// Step 1, check state
if (checkState()) {
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "already canceled %d %d", id, model.getStatus());
}
onPause();
break;
}
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(FileDownloadRunnable.class, "start download %s %s", id, model.getUrl());
}
// Step 2, handle resume from breakpoint
checkIsResumeAvailable();
connection = mConnectionCreator.create(model.getUrl());
addHeader(connection);
// start download----------------
// Step 3, init request
// get the request header in here, because of there are many connection
// component(such as HttpsURLConnectionImpl, HttpURLConnectionImpl in okhttp3) don't
// allow access to the request header after it connected.
final Map<String, List<String>> requestHeader = connection.getRequestHeaderFields();
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "%s request header %s", id, requestHeader);
}
// Step 4, build connect
connection.execute();
final int code = connection.getResponseCode();
final boolean isSucceedStart = code == HttpURLConnection.HTTP_OK || code == FileDownloadConnection.NO_RESPONSE_CODE;
// if the response status code isn't point to PARTIAL/OFFSET, isSucceedResume will
// be assigned to false, so filedownloader will download the file from very beginning.
final boolean isSucceedResume = ((code == HttpURLConnection.HTTP_PARTIAL) || (code == FileDownloadConnection.RESPONSE_CODE_FROM_OFFSET)) && isResumeDownloadAvailable;
if (isResumeDownloadAvailable && !isSucceedResume) {
FileDownloadLog.d(this, "want to resume from the breakpoint[%d], but the " + "response status code is[%d]", model.getSoFar(), code);
}
if (isSucceedStart || isSucceedResume) {
long total = model.getTotal();
final String transferEncoding = connection.getResponseHeaderField("Transfer-Encoding");
// Step 5, check response's header
if (isSucceedStart || total <= 0) {
if (transferEncoding == null) {
total = FileDownloadUtils.convertContentLengthString(connection.getResponseHeaderField("Content-Length"));
} else {
// if transfer not nil, ignore content-length
total = TOTAL_VALUE_IN_CHUNKED_RESOURCE;
}
}
// TODO consider if not is chunked & http 1.0/(>=http1.1 & connect not be keep live) may not give content-length
if (total < 0) {
// invalid total length
final boolean isEncodingChunked = transferEncoding != null && transferEncoding.equals("chunked");
if (!isEncodingChunked) {
// not chunked transfer encoding data
if (FileDownloadProperties.getImpl().HTTP_LENIENT) {
// do not response content-length either not chunk transfer encoding,
// but HTTP lenient is true, so handle as the case of transfer encoding chunk
total = TOTAL_VALUE_IN_CHUNKED_RESOURCE;
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "%d response header is not legal but " + "HTTP lenient is true, so handle as the case of " + "transfer encoding chunk", id);
}
} else {
throw new FileDownloadGiveUpRetryException("can't know the size of the " + "download file, and its Transfer-Encoding is not Chunked " + "either.\nyou can ignore such exception by add " + "http.lenient=true to the filedownloader.properties");
}
}
}
if (isSucceedResume) {
soFar = model.getSoFar();
}
// Step 6, callback on connected, and update header to db. for save etag.
onConnected(isSucceedResume, total, findEtag(connection), findFilename(connection));
// Step 7, check whether has same task running after got filename from server/local generate.
if (model.isPathAsDirectory()) {
// this scope for caring about the case of there is another task is provided
// the same path to store file and the same url.
final String targetFilePath = model.getTargetFilePath();
// get the ID after got the filename.
final int fileCaseId = FileDownloadUtils.generateId(model.getUrl(), targetFilePath);
// whether the file with the filename has been existed.
if (FileDownloadHelper.inspectAndInflowDownloaded(id, targetFilePath, isForceReDownload, false)) {
helper.remove(id);
break;
}
final FileDownloadModel fileCaseModel = helper.find(fileCaseId);
if (fileCaseModel != null) {
// whether the another task with the same file and url is downloading.
if (FileDownloadHelper.inspectAndInflowDownloading(id, fileCaseModel, threadPoolMonitor, false)) {
//it has been post to upper layer the 'warn' message, so the current
// task no need to continue download.
helper.remove(id);
break;
}
// the another task with the same file name and url is paused
helper.remove(fileCaseId);
deleteTargetFile();
if (FileDownloadMgr.isBreakpointAvailable(fileCaseId, fileCaseModel)) {
model.setSoFar(fileCaseModel.getSoFar());
model.setTotal(fileCaseModel.getTotal());
model.setETag(fileCaseModel.getETag());
helper.update(model);
// re connect to resume from breakpoint.
continue;
}
}
// whether there is an another running task with the same target-file-path.
if (FileDownloadHelper.inspectAndInflowConflictPath(id, model.getSoFar(), getTempFilePath(), targetFilePath, threadPoolMonitor)) {
helper.remove(id);
break;
}
}
// Step 8, start fetch datum from input stream & write to file
if (fetch(connection, isSucceedResume, soFar, total)) {
break;
}
} else {
final FileDownloadHttpException httpException = new FileDownloadHttpException(code, requestHeader, connection.getResponseHeaderFields());
if (revisedInterval) {
throw httpException;
}
revisedInterval = true;
switch(code) {
case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
deleteTaskFiles();
FileDownloadLog.w(FileDownloadRunnable.class, "%d response code %d, " + "range[%d] isn't make sense, so delete the dirty file[%s]" + ", and try to redownload it from byte-0.", id, code, model.getSoFar(), model.getTempFilePath());
onRetry(httpException, retryingTimes++);
break;
default:
throw httpException;
}
}
} catch (Throwable ex) {
// TODO 决策是否需要重试,是否是用户决定,或者根据错误码处理
if (autoRetryTimes > retryingTimes++ && !(ex instanceof FileDownloadGiveUpRetryException)) {
// retry
onRetry(ex, retryingTimes);
} else {
// error
onError(ex);
break;
}
} finally {
if (connection != null) {
connection.ending();
}
}
} while (true);
}
use of com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException in project FileDownloader by lingochamp.
the class FetchDataTask method run.
public void run() throws IOException, IllegalAccessException, IllegalArgumentException, FileDownloadGiveUpRetryException {
if (paused)
return;
long contentLength = FileDownloadUtils.findContentLength(connectionIndex, connection);
if (contentLength == TOTAL_VALUE_IN_CHUNKED_RESOURCE) {
contentLength = FileDownloadUtils.findContentLengthFromContentRange(connection);
}
if (contentLength == 0) {
throw new FileDownloadGiveUpRetryException(FileDownloadUtils.formatString("there isn't any content need to download on %d-%d with the " + "content-length is 0", downloadId, connectionIndex));
}
if (this.contentLength > 0 && contentLength != this.contentLength) {
final String range;
if (endOffset == ConnectionProfile.RANGE_INFINITE) {
range = FileDownloadUtils.formatString("range[%d-)", currentOffset);
} else {
range = FileDownloadUtils.formatString("range[%d-%d)", currentOffset, endOffset);
}
throw new FileDownloadGiveUpRetryException(FileDownloadUtils.formatString("require %s with contentLength(%d), but the " + "backend response contentLength is %d on " + "downloadId[%d]-connectionIndex[%d], please ask your backend " + "dev to fix such problem.", range, this.contentLength, contentLength, downloadId, connectionIndex));
}
final long fetchBeginOffset = currentOffset;
// start fetch
InputStream inputStream = null;
FileDownloadOutputStream outputStream = null;
try {
final boolean isSupportSeek = CustomComponentHolder.getImpl().isSupportSeek();
if (hostRunnable != null && !isSupportSeek) {
throw new IllegalAccessException("can't using multi-download when the output stream can't support seek");
}
this.outputStream = outputStream = FileDownloadUtils.createOutputStream(path);
if (isSupportSeek) {
outputStream.seek(currentOffset);
}
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "start fetch(%d): range [%d, %d), seek to[%d]", connectionIndex, startOffset, endOffset, currentOffset);
}
inputStream = connection.getInputStream();
byte[] buff = new byte[BUFFER_SIZE];
if (paused)
return;
do {
int byteCount = inputStream.read(buff);
if (byteCount == -1) {
break;
}
outputStream.write(buff, 0, byteCount);
currentOffset += byteCount;
// callback progress
callback.onProgress(byteCount);
checkAndSync();
// check status
if (paused)
return;
if (isWifiRequired && FileDownloadUtils.isNetworkNotOnWifiType()) {
throw new FileDownloadNetworkPolicyException();
}
} while (true);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if (outputStream != null)
sync();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
final long fetchedLength = currentOffset - fetchBeginOffset;
if (contentLength != TOTAL_VALUE_IN_CHUNKED_RESOURCE && contentLength != fetchedLength) {
throw new FileDownloadGiveUpRetryException(FileDownloadUtils.formatString("fetched length[%d] != content length[%d]," + " range[%d, %d) offset[%d] fetch begin offset[%d]", fetchedLength, contentLength, startOffset, endOffset, currentOffset, fetchBeginOffset));
}
// callback completed
callback.onCompleted(hostRunnable, startOffset, endOffset);
}
Aggregations