com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException Java Examples

The following examples show how to use com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompatListenerAssistTest.java    From okdownload with Apache License 2.0 6 votes vote down vote up
@Test
public void handleError_withPreAllocateException() {
    final DownloadTaskAdapter mockTaskAdapter = mock(DownloadTaskAdapter.class);
    final ProgressAssist mockProgressAssist = mock(ProgressAssist.class);
    when(mockTaskAdapter.getProgressAssist()).thenReturn(mockProgressAssist);
    when(mockTaskAdapter.getRetryAssist()).thenReturn(null);
    when(mockProgressAssist.getSofarBytes()).thenReturn(1L);
    final Exception mockException = mock(PreAllocateException.class);

    compatListenerAssist.handleError(mockTaskAdapter, mockException);

    verify(callback, never()).retry(
            any(DownloadTaskAdapter.class), any(Throwable.class), anyInt(), anyLong());
    final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
    final ArgumentCaptor<DownloadTaskAdapter> taskCaptor = ArgumentCaptor
            .forClass(DownloadTaskAdapter.class);
    verify(callback).error(taskCaptor.capture(), throwableCaptor.capture());
    assertThat(taskCaptor.getValue()).isEqualTo(mockTaskAdapter);
    assertThat(throwableCaptor.getValue())
            .isExactlyInstanceOf(FileDownloadOutOfSpaceException.class);
}
 
Example #2
Source File: DownloadLaunchRunnable.java    From FileDownloader with Apache License 2.0 6 votes vote down vote up
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();
    }
}