Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 

RxIo

Build Status Coverage Status Maven Central Version

The AsyncFiles class allows Java applications to easily read/write files asynchronously and without blocking. This is an equivalent to the standard JDK Files class but using non-blocking IO. 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.1</version>
</dependency>
implementation 'com.github.javasync:RxIo:1.2.1'

Usage

String path = "input.txt";
AsyncFiles
  .asyncQuery(path) // printing all lines from input.txt
  .subscribe((line, err) -> out.println(line)) // lack check err
  .join(); // block if you want to wait for completion
Path path = Paths.get("input.txt");
Files
  .lines(path) // printing all lines from input.txt
  .forEach(out::println)
Path path = Paths.get("output.txt")
List<String> data = asList("super", "brave", "isel", "gain");
AsyncFiles
  .write(path, data) // writing lines to output.txt
  .join(); // block if you want to wait for completion
/**
 *  Writing lines to output.txt
 */
Path path = Paths.get("output.txt")
List<String> data = asList("super", "brave", "isel", "gain");
Files.write(path, data);
Path in = Paths.get("input.txt");
Path out = Paths.get("output.txt");
AsyncFiles
  .readAllBytes(in)
  .thenCompose(bytes -> AsyncFiles.writeBytes(out, bytes))
  .join(); // block if you want to wait for completion
/**
 * Copying from one file to another.
 */
Path in = Paths.get("input.txt");
Path out = Paths.get("output.txt");
byte[] bytes = Files.readAllBytes(in);
Files.write(out, bytes);

The AsyncFiles::asyncQuery() returns an AsyncQuery that allows asynchronous subscription and chaining intermediate operations such as filter, map and others. In the following example we show how to print all words of a gutenberg.org file content without repetitions:

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

Alternatively, the AsyncFiles::lines() returns a reactive Publisher which is compatible with Reactor or RxJava streams. Thus we can use their utility methods to easily operate on the result of AsyncFiles::lines(). For example, with the utility methods of Reactor Flux we can rewrite the previous sample as:

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

About

Asynchronous non-blocking File Reader and Writer library for Java

Topics

Resources

License

Packages

No packages published