Code Monkey home page Code Monkey logo

rxio's Introduction

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:

Maven Gradle
<dependency> 
    <groupId>com.github.javasync</groupId>
    <artifactId>RxIo</artifactId>
    <version>1.2.5</version>
</dependency>
implementation 'com.github.javasync:RxIo:1.2.5'

Usage

Kotlin examples:

suspend fun copyNio(from: String, to: String) {
  val data = Path(from).readText() // suspension point
  Path(to).writeText(data)         // suspension point
}
fun copy(from: String, to: String) {
    val data = File(from).readText()
    File(to).writeText(data)
}
Path("input.txt")
  .lines()   // Flow<String>
  .onEach(::println)
  .collect() // block if you want to wait for completion
Path("input.txt")
  .readLines() // List<String>
  .forEach(::println)

Java examples:

AsyncFiles
  .readAllBytes("input.txt")
  .thenCompose(bytes -> AsyncFiles.writeBytes("output.txt", bytes))
  .join(); // block if you want to wait for completion
Path in = Paths.get("input.txt");
Path out = Paths.get("output.txt");
byte[] bytes = Files.readAllBytes(in);
Files.write(out, bytes);
AsyncFiles
  .asyncQuery("input.txt")
  .onNext((line, err) -> out.println(line)) // lack check err
  .blockingSubscribe(); // block if you want to wait for completion
Path path = Paths.get("input.txt");
Files
  .lines(path)
  .forEach(out::println)
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
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:

Flux
    .from(AsyncFiles.lines(file))
    .filter(line -> !line.isEmpty())                   // Skip empty lines
    .skip(14)                                          // Skip gutenberg header
    .takeWhile(line -> !line.contains("*** END OF "))  // Skip gutenberg footnote
    .flatMap(line -> Flux.fromArray(line.split("\\W+")))
    .distinct()
    .doOnNext(out::println)
    .doOnError(Throwable::printStackTrace)
    .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:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.