Search in sources :

Example 1 with ObservableOperator

use of io.reactivex.ObservableOperator in project navajo by Dexels.

the class StreamDocument method createBinary.

public static ObservableOperator<Binary, byte[]> createBinary() {
    return new ObservableOperator<Binary, byte[]>() {

        @Override
        public Observer<? super byte[]> apply(Observer<? super Binary> out) throws Exception {
            Binary result = new Binary();
            result.startBinaryPush();
            return new Observer<byte[]>() {

                @Override
                public void onComplete() {
                    try {
                        result.finishPushContent();
                    } catch (IOException e) {
                        e.printStackTrace();
                        out.onError(e);
                        return;
                    }
                    out.onNext(result);
                    out.onComplete();
                }

                @Override
                public void onError(Throwable e) {
                    out.onError(e);
                }

                @Override
                public void onNext(byte[] b) {
                    result.pushContent(b);
                }

                @Override
                public void onSubscribe(Disposable d) {
                    out.onSubscribe(new Disposable() {

                        private boolean disposed = false;

                        @Override
                        public void dispose() {
                            disposed = true;
                        }

                        @Override
                        public boolean isDisposed() {
                            return disposed;
                        }
                    });
                }
            };
        }
    };
}
Also used : Disposable(io.reactivex.disposables.Disposable) Observer(io.reactivex.Observer) Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) ObservableOperator(io.reactivex.ObservableOperator)

Example 2 with ObservableOperator

use of io.reactivex.ObservableOperator in project navajo by Dexels.

the class CSV method rows.

public static ObservableOperator<Row, String> rows(final String columnSeparator) {
    return new ObservableOperator<Row, String>() {

        @Override
        public Observer<? super String> apply(Observer<? super Row> child) throws Exception {
            return new Observer<String>() {

                final AtomicInteger counter = new AtomicInteger();

                final List<String> columns = new ArrayList<>();

                private Disposable disposable;

                @Override
                public void onComplete() {
                    child.onComplete();
                }

                @Override
                public void onError(Throwable e) {
                    logger.error("Error: ", e);
                    child.onError(e);
                }

                @Override
                public void onNext(String line) {
                    int current = counter.getAndIncrement();
                    if (current == 0) {
                        columns.addAll(CSV.columnNames(columnSeparator, line));
                    } else {
                        Row r = CSV.parseLine(columnSeparator, columns, line);
                        if (!disposable.isDisposed()) {
                            child.onNext(r);
                        }
                    }
                }

                @Override
                public void onSubscribe(Disposable d) {
                    disposable = d;
                }
            };
        }
    };
}
Also used : Disposable(io.reactivex.disposables.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observer(io.reactivex.Observer) List(java.util.List) ArrayList(java.util.ArrayList) ObservableOperator(io.reactivex.ObservableOperator)

Aggregations

ObservableOperator (io.reactivex.ObservableOperator)2 Observer (io.reactivex.Observer)2 Disposable (io.reactivex.disposables.Disposable)2 Binary (com.dexels.navajo.document.types.Binary)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1