Search in sources :

Example 1 with FileDownloadHttpException

use of com.liulishuo.filedownloader.exception.FileDownloadHttpException 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);
}
Also used : FileDownloadHttpException(com.liulishuo.filedownloader.exception.FileDownloadHttpException) FileDownloadGiveUpRetryException(com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException)

Example 2 with FileDownloadHttpException

use of com.liulishuo.filedownloader.exception.FileDownloadHttpException 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);
}
Also used : FileDownloadHttpException(com.liulishuo.filedownloader.exception.FileDownloadHttpException) FileDownloadConnection(com.liulishuo.filedownloader.connection.FileDownloadConnection) FileDownloadGiveUpRetryException(com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException) FileDownloadModel(com.liulishuo.filedownloader.model.FileDownloadModel) List(java.util.List)

Aggregations

FileDownloadGiveUpRetryException (com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException)2 FileDownloadHttpException (com.liulishuo.filedownloader.exception.FileDownloadHttpException)2 FileDownloadConnection (com.liulishuo.filedownloader.connection.FileDownloadConnection)1 FileDownloadModel (com.liulishuo.filedownloader.model.FileDownloadModel)1 List (java.util.List)1