Class ReadStreamSubscriber<R,​J>

  • All Implemented Interfaces:
    ReadStream<J>, StreamBase, Observer<R>, Subscription

    public class ReadStreamSubscriber<R,​J>
    extends Subscriber<R>
    implements ReadStream<J>
    An RxJava Subscriber that turns an Observable into a ReadStream.

    The stream implements the pause() and resume() operation by maintaining a buffer of BUFFER_SIZE elements between the Observable and the ReadStream.

    When the subscriber is created it requests 0 elements to activate the subscriber's back-pressure. Setting the handler initially on the ReadStream triggers a request of BUFFER_SIZE elements. When the item buffer is half empty, new elements are requested to fill the buffer back to BUFFER_SIZE elements.

    The #endHandler(Handler) is called when the Observable is completed or has failed and no pending elements, emitted before the completion or failure, are still in the buffer, i.e the handler is not called when the stream is paused.

    Author:
    Julien Viet
    • Constructor Detail

      • ReadStreamSubscriber

        public ReadStreamSubscriber​(java.util.function.Function<R,​J> adapter,
                                    java.util.function.Consumer<Subscriber<R>> doSubscribe)
    • Method Detail

      • asReadStream

        public static <R,​J> ReadStream<J> asReadStream​(Observable<R> observable,
                                                             java.util.function.Function<R,​J> adapter)
      • handler

        public ReadStream<J> handler​(Handler<J> handler)
        Description copied from interface: ReadStream
        Set a data handler. As data is read, the handler will be called with the data.
        Specified by:
        handler in interface ReadStream<R>
        Returns:
        a reference to this, so the API can be used fluently
      • pause

        public ReadStream<J> pause()
        Description copied from interface: ReadStream
        Pause the ReadStream, it sets the buffer in fetch mode and clears the actual demand.

        While it's paused, no data will be sent to the data handler.

        Specified by:
        pause in interface ReadStream<R>
        Returns:
        a reference to this, so the API can be used fluently
      • fetch

        public ReadStream<J> fetch​(long amount)
        Description copied from interface: ReadStream
        Fetch the specified amount of elements. If the ReadStream has been paused, reading will recommence with the specified amount of items, otherwise the specified amount will be added to the current stream demand.
        Specified by:
        fetch in interface ReadStream<R>
        Returns:
        a reference to this, so the API can be used fluently
      • resume

        public ReadStream<J> resume()
        Description copied from interface: ReadStream
        Resume reading, and sets the buffer in flowing mode.

        If the ReadStream has been paused, reading will recommence on it.

        Specified by:
        resume in interface ReadStream<R>
        Returns:
        a reference to this, so the API can be used fluently
      • endHandler

        public ReadStream<J> endHandler​(Handler<Void> handler)
        Description copied from interface: ReadStream
        Set an end handler. Once the stream has ended, and there is no more data to be read, this handler will be called.
        Specified by:
        endHandler in interface ReadStream<R>
        Returns:
        a reference to this, so the API can be used fluently
      • onNext

        public void onNext​(R item)
        Specified by:
        onNext in interface Observer<R>