public static void setRemitToDBDelayMillis(int delayMillis) { final BreakpointStore store = OkDownload.with().breakpointStore(); if (!(store instanceof RemitStoreOnSQLite)) { throw new IllegalStateException( "The current store is " + store + " not RemitStoreOnSQLite!"); } delayMillis = Math.max(0, delayMillis); ((RemitStoreOnSQLite) store).remitHelper.delayMillis = delayMillis; } }
public FetchDataInterceptor(int blockIndex, @NonNull InputStream inputStream, @NonNull MultiPointOutputStream outputStream, DownloadTask task) { this.blockIndex = blockIndex; this.inputStream = inputStream; this.readBuffer = new byte[task.getReadBufferSize()]; this.outputStream = outputStream; this.task = task; this.dispatcher = OkDownload.with().callbackDispatcher(); }
void inspectDownloadFromBreakpoint(@NonNull DownloadTask task, @NonNull BreakpointInfo info) { final DownloadMonitor monitor = OkDownload.with().getMonitor(); if (monitor != null) monitor.taskDownloadFromBreakpoint(task, info); }
/** * Cancel the current task, if there is another same id task, it would be canceled too. * <p> * If the task is canceled all resourced about this task will be recycled. * <p> * If there are more than one task need to cancel, please using {@link #cancel(DownloadTask[])} * instead, because the performance is optimized to handle bunch of tasks cancel. */ public void cancel() { OkDownload.with().downloadDispatcher().cancel(this); }
DownloadCache createCache(@NonNull BreakpointInfo info) { final MultiPointOutputStream outputStream = OkDownload.with().processFileStrategy() .createProcessStream(task, info, store); return new DownloadCache(outputStream); }
@Nullable public static BreakpointInfo getCurrentInfo(@NonNull DownloadTask task) { final BreakpointStore store = OkDownload.with().breakpointStore(); final int id = store.findOrCreateId(task); final BreakpointInfo info = store.get(id); return info == null ? null : info.copy(); }
public void pause(final FileDownloadListener listener) { final List<DownloadTaskAdapter> taskAdapters = FileDownloadList.getImpl().getByFileDownloadListener(listener); final DownloadTask[] downloadTasks = new DownloadTask[taskAdapters.size()]; for (int i = 0; i < taskAdapters.size(); i++) { downloadTasks[i] = taskAdapters.get(i).getDownloadTask(); } OkDownload.with().downloadDispatcher().cancel(downloadTasks); }
void assembleBlockAndCallbackFromBeginning(@NonNull BreakpointInfo info, @NonNull BreakpointRemoteCheck remoteCheck, @NonNull ResumeFailedCause failedCause) { Util.assembleBlock(task, info, remoteCheck.getInstanceLength(), remoteCheck.isAcceptRange()); OkDownload.with().callbackDispatcher().dispatch() .downloadFromBeginning(task, info, failedCause); }
/** * @return {@code true} success valid filename from store. */ public boolean validFilenameFromStore(@NonNull DownloadTask task) { final String filename = OkDownload.with().breakpointStore() .getResponseFilename(task.getUrl()); if (filename == null) return false; task.getFilenameHolder().set(filename); return true; }
private void prepareOutputStreamEnv() throws FileNotFoundException, PreAllocateException { when(OkDownload.with().outputStreamFactory().supportSeek()).thenReturn(true); when(OkDownload.with().processFileStrategy().isPreAllocateLength(task)).thenReturn(true); when(OkDownload.with().outputStreamFactory().create(any(Context.class), any(Uri.class), anyInt())).thenReturn(mock(DownloadOutputStream.class)); // recreate for new values of support-seek and pre-allocate-length. multiPointOutputStream = spy(new MultiPointOutputStream(task, info, store)); doNothing().when(multiPointOutputStream).inspectFreeSpace(any(StatFs.class), anyLong()); final Uri uri = mock(Uri.class); when(task.getUri()).thenReturn(uri); when(uri.getScheme()).thenReturn("file"); }
@Test public void monitor_taskEnd() throws IOException { mockOkDownload(); final DownloadMonitor monitor = mock(DownloadMonitor.class); final OkDownload okDownload = OkDownload.with(); when(okDownload.getMonitor()).thenReturn(monitor); final DownloadTask task = mock(DownloadTask.class); final EndCause endCause = mock(EndCause.class); final Exception exception = mock(Exception.class); transmit.inspectTaskEnd(task, endCause, exception); verify(monitor).taskEnd(eq(task), eq(endCause), eq(exception)); }
private void setupConnection() throws IOException { mockOkDownload(); final DownloadConnection.Factory factory = OkDownload.with().connectionFactory(); when(factory.create(url)).thenReturn(connection); }
@Test public void check_assembleBasicData() throws IOException { mockOkDownload(); check.check(); final DownloadStrategy strategy = OkDownload.with().downloadStrategy(); verify(strategy).validFilenameFromResponse(eq(responseFilename), eq(task), eq(info)); verify(info).setChunked(eq(connectTrial.isChunked())); verify(info).setEtag(eq(responseEtag)); }
@Test public void getCurrentInfo() { final BreakpointStore store = OkDownload.with().breakpointStore(); final BreakpointInfo origin = mock(BreakpointInfo.class); doReturn(origin).when(store).get(anyInt()); StatusUtil.getCurrentInfo(mock(DownloadTask.class)); verify(origin).copy(); }
@Test public void interceptConnect() throws Exception { serverInterceptor.interceptConnect(chain); final DownloadStrategy downloadStrategy = OkDownload.with().downloadStrategy(); verify(downloadStrategy).inspectNetworkOnWifi(eq(task)); verify(downloadStrategy).inspectNetworkAvailable(); } }
@Test(expected = FileBusyAfterRunException.class) public void check_fileConflictAfterRun() throws IOException { mockOkDownload(); when(OkDownload.with().downloadDispatcher().isFileConflictAfterRun(eq(task))) .thenReturn(true); check.check(); }
@Test public void isUseMultiBlock() throws IOException { mockOkDownload(); when(OkDownload.with().outputStreamFactory().supportSeek()).thenReturn(false); assertThat(strategy.isUseMultiBlock(false)).isFalse(); assertThat(strategy.isUseMultiBlock(true)).isFalse(); when(OkDownload.with().outputStreamFactory().supportSeek()).thenReturn(true); assertThat(strategy.isUseMultiBlock(true)).isTrue(); }
@Test public void isSameTaskPendingOrRunning() throws IOException { mockOkDownload(); final DownloadTask task = mock(DownloadTask.class); final DownloadDispatcher dispatcher = OkDownload.with().downloadDispatcher(); when(dispatcher.findSameTask(task)).thenReturn(task); assertThat(StatusUtil.isSameTaskPendingOrRunning(task)).isTrue(); when(dispatcher.findSameTask(task)).thenReturn(null); assertThat(StatusUtil.isSameTaskPendingOrRunning(task)).isFalse(); }
@Test(expected = ServerCanceledException.class) public void check_serverCanceled() throws IOException { mockOkDownload(); when(OkDownload.with().downloadStrategy() .isServerCanceled(0, false)) .thenReturn(true); check.check(); }
@Test public void enqueue() { final DownloadTask[] tasks = new DownloadTask[2]; tasks[0] = new DownloadTask.Builder("url1", "path", "filename1").build(); tasks[1] = new DownloadTask.Builder("url2", "path", "filename1").build(); final DownloadListener listener = mock(DownloadListener.class); DownloadTask.enqueue(tasks, listener); assertThat(tasks[0].getListener()).isEqualTo(listener); assertThat(tasks[1].getListener()).isEqualTo(listener); verify(OkDownload.with().downloadDispatcher()).enqueue(eq(tasks)); }