Search in sources :

Example 1 with FileDownloadOutOfSpaceException

use of com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException in project FileDownloader by lingochamp.

the class FileDownloadRunnable method exFiltrate.

private Throwable exFiltrate(Throwable ex) {
    final String tempPath = model.getTempFilePath();
    /**
         * Only handle the case of Chunked resource, if it is not chunked, has already been handled
         * in {@link #getOutputStream(boolean, long)}.
         */
    if ((model.getTotal() == TOTAL_VALUE_IN_CHUNKED_RESOURCE || FileDownloadProperties.getImpl().FILE_NON_PRE_ALLOCATION) && ex instanceof IOException && new File(tempPath).exists()) {
        // chunked
        final long freeSpaceBytes = FileDownloadUtils.getFreeSpaceBytes(tempPath);
        if (freeSpaceBytes <= BUFFER_SIZE) {
            // free space is not enough.
            long downloadedSize = 0;
            final File file = new File(tempPath);
            if (!file.exists()) {
                FileDownloadLog.e(FileDownloadRunnable.class, ex, "Exception with: free " + "space isn't enough, and the target file not exist.");
            } else {
                downloadedSize = file.length();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                ex = new FileDownloadOutOfSpaceException(freeSpaceBytes, BUFFER_SIZE, downloadedSize, ex);
            } else {
                ex = new FileDownloadOutOfSpaceException(freeSpaceBytes, BUFFER_SIZE, downloadedSize);
            }
        }
    }
    return ex;
}
Also used : FileDownloadOutOfSpaceException(com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException) IOException(java.io.IOException) File(java.io.File)

Example 2 with FileDownloadOutOfSpaceException

use of com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException in project FileDownloader by lingochamp.

the class DownloadLaunchRunnable method handlePreAllocate.

private void handlePreAllocate(long totalLength, String path) throws IOException, IllegalAccessException {
    FileDownloadOutputStream outputStream = null;
    try {
        if (totalLength != TOTAL_VALUE_IN_CHUNKED_RESOURCE) {
            outputStream = FileDownloadUtils.createOutputStream(model.getTempFilePath());
            final long breakpointBytes = new File(path).length();
            final long requiredSpaceBytes = totalLength - breakpointBytes;
            final long freeSpaceBytes = FileDownloadUtils.getFreeSpaceBytes(path);
            if (freeSpaceBytes < requiredSpaceBytes) {
                // throw a out of space exception.
                throw new FileDownloadOutOfSpaceException(freeSpaceBytes, requiredSpaceBytes, breakpointBytes);
            } else if (!FileDownloadProperties.getImpl().fileNonPreAllocation) {
                // pre allocate.
                outputStream.setLength(totalLength);
            }
        }
    } finally {
        if (outputStream != null)
            outputStream.close();
    }
}
Also used : FileDownloadOutOfSpaceException(com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException) FileDownloadOutputStream(com.liulishuo.filedownloader.stream.FileDownloadOutputStream) File(java.io.File)

Example 3 with FileDownloadOutOfSpaceException

use of com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException in project FileDownloader by lingochamp.

the class FileDownloadRunnable method getOutputStream.

// ----------------------------------
private FileDownloadOutputStream getOutputStream(final boolean append, final long totalBytes) throws IOException, IllegalAccessException {
    final String tempPath = model.getTempFilePath();
    if (TextUtils.isEmpty(tempPath)) {
        throw new RuntimeException("found invalid internal destination path, empty");
    }
    //noinspection ConstantConditions
    if (!FileDownloadUtils.isFilenameValid(tempPath)) {
        throw new RuntimeException(FileDownloadUtils.formatString("found invalid internal destination filename" + " %s", tempPath));
    }
    File file = new File(tempPath);
    if (file.exists() && file.isDirectory()) {
        throw new RuntimeException(FileDownloadUtils.formatString("found invalid internal destination path[%s]," + " & path is directory[%B]", tempPath, file.isDirectory()));
    }
    if (!file.exists()) {
        if (!file.createNewFile()) {
            throw new IOException(FileDownloadUtils.formatString("create new file error  %s", file.getAbsolutePath()));
        }
    }
    FileDownloadOutputStream outputStream = mOutputStreamCreator.create(file);
    // check the available space bytes whether enough or not.
    if (totalBytes > 0) {
        final long breakpointBytes = file.length();
        final long requiredSpaceBytes = totalBytes - breakpointBytes;
        final long freeSpaceBytes = FileDownloadUtils.getFreeSpaceBytes(tempPath);
        if (freeSpaceBytes < requiredSpaceBytes) {
            outputStream.close();
            // throw a out of space exception.
            throw new FileDownloadOutOfSpaceException(freeSpaceBytes, requiredSpaceBytes, breakpointBytes);
        } else if (!FileDownloadProperties.getImpl().FILE_NON_PRE_ALLOCATION) {
            // pre allocate.
            outputStream.setLength(totalBytes);
        }
    }
    if (append && mOutputStreamCreator.supportSeek()) {
        outputStream.seek(model.getSoFar());
    }
    return outputStream;
}
Also used : FileDownloadOutOfSpaceException(com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException) IOException(java.io.IOException) FileDownloadOutputStream(com.liulishuo.filedownloader.stream.FileDownloadOutputStream) File(java.io.File)

Example 4 with FileDownloadOutOfSpaceException

use of com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException in project FileDownloader by lingochamp.

the class DownloadStatusCallback method exFiltrate.

private Exception exFiltrate(Exception ex) {
    final String tempPath = model.getTempFilePath();
    /**
     * Only handle the case of Chunked resource, if it is not chunked, has already been handled
     * in {@link #getOutputStream(boolean, long)}.
     */
    if ((model.isChunked() || FileDownloadProperties.getImpl().fileNonPreAllocation) && ex instanceof IOException && new File(tempPath).exists()) {
        // chunked
        final long freeSpaceBytes = FileDownloadUtils.getFreeSpaceBytes(tempPath);
        if (freeSpaceBytes <= BUFFER_SIZE) {
            // free space is not enough.
            long downloadedSize = 0;
            final File file = new File(tempPath);
            if (!file.exists()) {
                FileDownloadLog.e(this, ex, "Exception with: free " + "space isn't enough, and the target file not exist.");
            } else {
                downloadedSize = file.length();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                ex = new FileDownloadOutOfSpaceException(freeSpaceBytes, BUFFER_SIZE, downloadedSize, ex);
            } else {
                ex = new FileDownloadOutOfSpaceException(freeSpaceBytes, BUFFER_SIZE, downloadedSize);
            }
        }
    }
    return ex;
}
Also used : FileDownloadOutOfSpaceException(com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException) IOException(java.io.IOException) File(java.io.File)

Aggregations

FileDownloadOutOfSpaceException (com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException)4 File (java.io.File)4 IOException (java.io.IOException)3 FileDownloadOutputStream (com.liulishuo.filedownloader.stream.FileDownloadOutputStream)2