Filesystem { public Single<String> readTextFileFully(String pathToFile) { String filePath = getWindowsPathUsableForNio(pathToFile); return Single.create(s -> { AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(filePath), StandardOpenOption.READ); long capacity = channel.size(); if (capacity > Integer.MAX_VALUE) { throw new IllegalArgumentException("File is too big. Max size is " + Integer.MAX_VALUE + " bytes."); } ByteBuffer buffer = ByteBuffer.allocate((int) channel.size()); channel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, final ByteBuffer attachment) { s.onSuccess(new String(attachment.array())); } @Override public void failed(final Throwable exc, final ByteBuffer attachment) { s.onError(exc); } }); }); }  Flowable<String> readTextFile(String pathToFile); Flowable<String> readTextFileFromClasspath(String resourcePath); Single<String> readTextFileFully(String pathToFile); Single<String> readTextFileFullyFromClasspath(String resourcePath); Completable writeFile(final String content, String filePath); Completable writeFileSync(String content, String filePath, boolean append); Completable writeFileSync(String content, Charset encoding, String filePath, boolean append);  }
@Test void testReadFileFully() { TestObserver<String> observer = filesystem.readTextFileFully("src/test/resources/someFile.txt") .test(); await().atMost(2, SECONDS).until(observer::isTerminated); observer.assertValue("This is some content in some file." + System.lineSeparator() + System.lineSeparator() + "This is more content."); }
@Test void testReadFileFullyWithUnknownPathCausesErrorCallbackToBeInvoked() { TestObserver<String> observer = filesystem.readTextFileFully("doesntExist.txt") .test(); await().atMost(2, SECONDS).until(observer::isTerminated); observer.assertError(NoSuchFileException.class); }