Search in sources :

Example 1 with FileDownloadNetworkPolicyException

use of com.liulishuo.filedownloader.exception.FileDownloadNetworkPolicyException 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);
}
Also used : FileDownloadGiveUpRetryException(com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException) InputStream(java.io.InputStream) FileDownloadOutputStream(com.liulishuo.filedownloader.stream.FileDownloadOutputStream) IOException(java.io.IOException) FileDownloadNetworkPolicyException(com.liulishuo.filedownloader.exception.FileDownloadNetworkPolicyException)

Aggregations

FileDownloadGiveUpRetryException (com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException)1 FileDownloadNetworkPolicyException (com.liulishuo.filedownloader.exception.FileDownloadNetworkPolicyException)1 FileDownloadOutputStream (com.liulishuo.filedownloader.stream.FileDownloadOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1