Git Product home page Git Product logo

rxjava-file's Introduction

rxjava-file


Maven Central

Status: released to Maven Central

Requires Java 7.

Observable utilities for files:

  • tail a file (either lines or byte[])
  • trigger tail updates using Java 7 and later NIO WatchService events
  • or trigger tail updates using any Observable
  • stream WatchEvents from a WatchService
  • backpressure support
  • tested on Linux and Windows 7 (not OSX, help appreciated!)

Release Notes

Maven site reports are here including javadoc.

Getting started

Add this maven dependency to your pom.xml:

<dependency>
  <groupId>com.github.davidmoten</groupId>
  <artifactId>rxjava-file</artifactId>
  <version>0.4</version>
</dependency>

How to build

git clone https://github.com/davidmoten/rxjava-file
cd rxjava-file
mvn clean install 

Examples

###Tail a text file with NIO

Tail the lines of the text log file /var/log/server.log as an Observable<String>:

import com.github.davidmoten.rx.FileObservable;
import rx.Observable;
import java.io.File; 
 
Observable<String> items = 
     FileObservable.tailer()
                   .file("/var/log/server.log")
                   .startPosition(0)
                   .sampleTimeMs(500)
                   .chunkSize(8192)
                   .utf8()
                   .tailText();
                     

or, using defaults (will use default charset):

Observable<String> items = 
     FileObservable.tailer()
                   .file("/var/log/server.log")
                   .tailText();

Note that if you want the Observable<String> to be emitting line by line then wrap it with a call like StringObservable.split(observable, "\n"). StringObservable is in the RxJava rxjava-string artifact.

###Tail a text file without NIO

The above example uses a WatchService to generate WatchEvents to prompt rereads of the end of the file to perform the tail.

To use polling instead (say every 5 seconds):

Observable<String> items = 
     FileObservable.tailer()
                   .file(new File("var/log/server.log"))
                   .source(Observable.interval(5, TimeUnit.SECONDS)
                   .tailText();

###Tail a binary file with NIO

Observable<byte[]> items = 
     FileObservable.tailer()
                   .file("/tmp/dump.bin")
                   .tail();

###Tail a binary file without NIO

Observable<byte[]> items = 
     FileObservable.tailer()
                   .file("/tmp/dump.bin")
                   .source(Observable.interval(5, TimeUnit.SECONDS)
                   .tail();

rxjava-file's People

Contributors

davidmoten avatar enricsala avatar matthiasbalke avatar

Watchers

James Cloos avatar Guilherme Ceschiatti B. Moreira avatar  avatar

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.