项目作者: javasync

项目描述 :
Asynchronous non-blocking File Reader and Writer library for Java
高级语言: Java
项目地址: git://github.com/javasync/RxIo.git
创建时间: 2018-06-06T15:49:43Z
项目社区:https://github.com/javasync/RxIo

开源协议:MIT License

下载


RxIo

Build Status
Coverage Status
Maven Central Version

The AsyncFiles class allows JVM
applications to easily read/write files asynchronously with non-blocking IO.
AsyncFiles take advantage of Java AsynchronousFileChannel
to perform asynchronous I/O operations.

AsyncFiles provides equivalent operations to the standard JDK
Files
class but using non-blocking IO and an asynchronous API with different
asynchronous idioms, namely:
CompletableFuture,
jayield AsyncQuery,
reactive-streams Publisher,
Kotlin coroutines and Kotlin Asynchronous Flow.

In section Usage we present some examples using the
AsyncFiles
class side by side with the corresponding blocking version of
Files.

Installation

First, in order to include it to your project,
simply add this dependency:










MavenGradle


xml <dependency> <groupId>com.github.javasync</groupId> <artifactId>RxIo</artifactId> <version>1.2.5</version> </dependency>



groovy implementation 'com.github.javasync:RxIo:1.2.5'



## Usage

Kotlin examples:












kotlin suspend fun copyNio(from: String, to: String) { val data = Path(from).readText() // suspension point Path(to).writeText(data) // suspension point }



kotlin fun copy(from: String, to: String) { val data = File(from).readText() File(to).writeText(data) }



kotlin Path("input.txt") .lines() // Flow<String> .onEach(::println) .collect() // block if you want to wait for completion



kotlin Path("input.txt") .readLines() // List<String> .forEach(::println)

Java examples:
















java AsyncFiles .readAllBytes("input.txt") .thenCompose(bytes -> AsyncFiles.writeBytes("output.txt", bytes)) .join(); // block if you want to wait for completion



java Path in = Paths.get("input.txt"); Path out = Paths.get("output.txt"); byte[] bytes = Files.readAllBytes(in); Files.write(out, bytes);



java AsyncFiles .asyncQuery("input.txt") .onNext((line, err) -> out.println(line)) // lack check err .blockingSubscribe(); // block if you want to wait for completion



java Path path = Paths.get("input.txt"); Files .lines(path) .forEach(out::println)



java List<String> data = asList("super", "brave", "isel", "gain"); AsyncFiles .write("output.txt", data) // writing lines to output.txt .join(); // block if you want to wait for completion



java List<String> data = asList("super", "brave", "isel", "gain"); Path path = Paths.get("output.txt") Files.write(path, data);

The AsyncFiles::lines()
returns a reactive Publisher
which is compatible with Reactor or RxJava streams.
Thus we can use the utility methods of Reactor Flux
to easily operate on the result of AsyncFiles::lines().
In the following example we show how to print all words of a gutenberg.org file content without repetitions:

  1. Flux
  2. .from(AsyncFiles.lines(file))
  3. .filter(line -> !line.isEmpty()) // Skip empty lines
  4. .skip(14) // Skip gutenberg header
  5. .takeWhile(line -> !line.contains("*** END OF ")) // Skip gutenberg footnote
  6. .flatMap(line -> Flux.fromArray(line.split("\\W+")))
  7. .distinct()
  8. .doOnNext(out::println)
  9. .doOnError(Throwable::printStackTrace)
  10. .blockLast(); // block if you want to wait for completion

Alternatively, the AsyncFiles::asyncQuery()
returns an AsyncQuery that allows asynchronous subscription and chaining intermediate operations
such as filter, map and others.
We can rewrite the previous sample as:

  1. AsyncFiles
  2. .asyncQuery(file)
  3. .filter(line -> !line.isEmpty()) // Skip empty lines
  4. .skip(14) // Skip gutenberg header
  5. .takeWhile(line -> !line.contains("*** END OF ")) // Skip gutenberg footnote
  6. .flatMapMerge(line -> AsyncQuery.of(line.split("\\W+")))
  7. .distinct()
  8. .subscribe((word, err) -> {
  9. if(err != null) err.printStackTrace();
  10. else out.println(word);
  11. })
  12. .join(); // block if you want to wait for completion