@Test public void testDownloadFile() throws Exception { TestSubscriber<File> testSubscriber = new TestSubscriber<>(); FontDownloader.downloadFile(FONT_URL, FILE.getPath()).subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertReceivedOnNext(Collections.singletonList(FILE)); long downloadedFileSize = testSubscriber.getOnNextEvents().get(0).length(); assertEquals(EXPECTED_FILE_SIZE, downloadedFileSize, 0); }
static Observable<File> downloadFile(final String url, final String path) { return Observable.create(subscriber -> { final Request request = new Request.Builder().url(url).build(); try { if (!subscriber.isUnsubscribed()) { final File file = new File(path); if (!file.exists()) { file.getParentFile().mkdirs(); Timber.i("downloadFile: Downloading " + file.getName()); final Response response = sClient.newCall(request).execute(); final BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeAll(response.body().source()); sink.close(); } else Timber.i("downloadFile: Retrieved from cache " + file.getName()); subscriber.onNext(file); subscriber.onCompleted(); } } catch (IOException e) { subscriber.onError(new DownloadException(e)); } }); }
FontDownloader { static Observable<File> downloadFile(final String url, final String path) { return Observable.create(subscriber -> { final Request request = new Request.Builder().url(url).build(); try { if (!subscriber.isUnsubscribed()) { final File file = new File(path); if (!file.exists()) { file.getParentFile().mkdirs(); Timber.i("downloadFile: Downloading " + file.getName()); final Response response = sClient.newCall(request).execute(); final BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeAll(response.body().source()); sink.close(); } else Timber.i("downloadFile: Retrieved from cache " + file.getName()); subscriber.onNext(file); subscriber.onCompleted(); } } catch (IOException e) { subscriber.onError(new DownloadException(e)); } }); } }
FontDownloader { static Observable<File> downloadFile(final String url, final String path) { return Observable.create(subscriber -> { final Request request = new Request.Builder().url(url).build(); try { if (!subscriber.isUnsubscribed()) { final File file = new File(path); if (!file.exists()) { file.getParentFile().mkdirs(); Timber.i("downloadFile: Downloading " + file.getName()); final Response response = sClient.newCall(request).execute(); final BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeAll(response.body().source()); sink.close(); } else Timber.i("downloadFile: Retrieved from cache " + file.getName()); subscriber.onNext(file); subscriber.onCompleted(); } } catch (IOException e) { subscriber.onError(new DownloadException(e)); } }); } FontDownloader(FontPackage fontPackage); }
FontDownloader { static Observable<File> downloadFile(final String url, final String path) { return Observable.create(subscriber -> { final Request request = new Request.Builder().url(url).build(); try { if (!subscriber.isUnsubscribed()) { final File file = new File(path); if (!file.exists()) { file.getParentFile().mkdirs(); Timber.i("downloadFile: Downloading " + file.getName()); final Response response = sClient.newCall(request).execute(); final BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeAll(response.body().source()); sink.close(); } else Timber.i("downloadFile: Retrieved from cache " + file.getName()); subscriber.onNext(file); subscriber.onCompleted(); } } catch (IOException e) { subscriber.onError(new DownloadException(e)); } }); } FontDownloader(FontPackage fontPackage); Observable<File> downloadAllFonts(); Observable<File> downloadFontStyles(Style... styles); static Observable<File> downloadStyleFromPackages(List<FontPackage> packages, Style style); }
FontDownloader { static Observable<File> downloadFile(final String url, final String path) { return Observable.create(subscriber -> { final Request request = new Request.Builder().url(url).build(); try { if (!subscriber.isUnsubscribed()) { final File file = new File(path); if (!file.exists()) { file.getParentFile().mkdirs(); Timber.i("downloadFile: Downloading " + file.getName()); final Response response = sClient.newCall(request).execute(); final BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeAll(response.body().source()); sink.close(); } else Timber.i("downloadFile: Retrieved from cache " + file.getName()); subscriber.onNext(file); subscriber.onCompleted(); } } catch (IOException e) { subscriber.onError(new DownloadException(e)); } }); } FontDownloader(FontPackage fontPackage); Observable<File> downloadAllFonts(); Observable<File> downloadFontStyles(Style... styles); static Observable<File> downloadStyleFromPackages(List<FontPackage> packages, Style style);  }