Search in sources :

Example 6 with BaseFlowableOperator

use of com.dexels.navajo.document.stream.io.BaseFlowableOperator in project navajo by Dexels.

the class JSON method parseFlowable.

public static FlowableOperator<Flowable<JSONEvent>, byte[]> parseFlowable(int queueSize) {
    return new BaseFlowableOperator<Flowable<JSONEvent>, byte[]>(queueSize) {

        @Override
        public Subscriber<? super byte[]> apply(Subscriber<? super Flowable<JSONEvent>> child) throws Exception {
            return new Subscriber<byte[]>() {

                JSONToIterable parser = new JSONToIterable(new JsonFactory());

                @Override
                public void onComplete() {
                    Flowable<JSONEvent> fromIterable = Flowable.fromIterable(parser.endOfInput());
                    queue.offer(fromIterable);
                    done = true;
                    drain(child);
                }

                @Override
                public void onError(Throwable t) {
                    error = t;
                    done = true;
                    drain(child);
                }

                @Override
                public void onNext(byte[] data) {
                    Flowable<JSONEvent> fromIterable;
                    try {
                        fromIterable = Flowable.fromIterable(parser.feed(data));
                        queue.offer(fromIterable);
                        drain(child);
                    } catch (IOException e) {
                        child.onError(e);
                        return;
                    }
                }

                @Override
                public void onSubscribe(Subscription s) {
                    subscription = s;
                    child.onSubscribe(new Subscription() {

                        @Override
                        public void request(long n) {
                            BackpressureHelper.add(requested, n);
                            drain(child);
                        }

                        @Override
                        public void cancel() {
                            cancelled = true;
                            s.cancel();
                        }
                    });
                    s.request(1);
                }
            };
        }
    };
}
Also used : Subscriber(org.reactivestreams.Subscriber) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BaseFlowableOperator(com.dexels.navajo.document.stream.io.BaseFlowableOperator) IOException(java.io.IOException) Subscription(org.reactivestreams.Subscription) Flowable(io.reactivex.Flowable)

Aggregations

BaseFlowableOperator (com.dexels.navajo.document.stream.io.BaseFlowableOperator)6 Subscriber (org.reactivestreams.Subscriber)6 Subscription (org.reactivestreams.Subscription)6 IOException (java.io.IOException)5 Flowable (io.reactivex.Flowable)4 FlowableOperator (io.reactivex.FlowableOperator)2 FlowableSubscriber (io.reactivex.FlowableSubscriber)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors (java.util.stream.Collectors)2 CRC32 (java.util.zip.CRC32)2 Deflater (java.util.zip.Deflater)2 Msg (com.dexels.navajo.document.stream.api.Msg)1 NavajoStreamEvent (com.dexels.navajo.document.stream.events.NavajoStreamEvent)1 SaxXmlFeeder (com.dexels.navajo.document.stream.impl.SaxXmlFeeder)1 Binary (com.dexels.navajo.document.types.Binary)1 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 FlowableTransformer (io.reactivex.FlowableTransformer)1