com.github.davidmoten.rx.internal.operators.OperatorFileTailer Maven / Gradle / Ivy
package com.github.davidmoten.rx.internal.operators;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.util.concurrent.atomic.AtomicLong;
import com.github.davidmoten.rx.Bytes;
import com.github.davidmoten.rx.subjects.PublishSubjectSingleSubscriber;
import rx.Observable;
import rx.Observable.Operator;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.observers.Subscribers;
/**
* Reacts to source events by emitting new lines written to a file since the
* last source event.
*/
public class OperatorFileTailer implements Operator {
private final File file;
private final AtomicLong currentPosition = new AtomicLong();
private final int maxBytesPerEmission;
/**
* Constructor.
*
* @param file
* text file to tail
* @param startPosition
* start tailing the file after this many bytes
* @param maxBytesPerEmission
* maximum number of bytes per emission
*/
public OperatorFileTailer(File file, long startPosition, int maxBytesPerEmission) {
if (file == null)
throw new NullPointerException("file cannot be null");
this.file = file;
this.currentPosition.set(startPosition);
this.maxBytesPerEmission = maxBytesPerEmission;
}
/**
* Constructor. Emits byte arrays of up to 8*1024 bytes.
*
* @param file
* file to tail
* @param startPosition
* start position in bytes
*/
public OperatorFileTailer(File file, long startPosition) {
this(file, startPosition, 8192);
}
@Override
public Subscriber super Object> call(Subscriber super byte[]> child) {
final PublishSubjectSingleSubscriber super Object> subject = PublishSubjectSingleSubscriber
.create();
Subscriber super Object> parent = Subscribers.from(subject);
child.add(parent);
subject
// report new lines for each event
.concatMap(reportNewLines(file, currentPosition, maxBytesPerEmission))
// subscribe
.unsafeSubscribe(child);
return parent;
}
private static Func1