io.vertx.rx.java

## Class ReadStreamSubscriber<R,J>

- java.lang.Object
- - rx.Subscriber<R>
  - - io.vertx.rx.java.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

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `BUFFER_SIZE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ReadStreamSubscriber(java.util.function.Function<R,J> adapter, java.util.function.Consumer<Subscriber<R>> doSubscribe)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static <R,J> ReadStream<J>` | `asReadStream(Observable<R> observable, java.util.function.Function<R,J> adapter)`
    `ReadStream<J>` | `endHandler(Handler<Void> handler)` Set an end handler.
    `ReadStream<J>` | `exceptionHandler(Handler<Throwable> handler)` Set an exception handler on the read stream.
    `ReadStream<J>` | `fetch(long amount)` Fetch the specified `amount` of elements.
    `ReadStream<J>` | `handler(Handler<J> handler)` Set a data handler.
    `void` | `onCompleted()`
    `void` | `onError(Throwable e)`
    `void` | `onNext(R item)`
    `ReadStream<J>` | `pause()` Pause the `ReadStream`, it sets the buffer in `fetch` mode and clears the actual demand.
    `ReadStream<J>` | `resume()` Resume reading, and sets the buffer in `flowing` mode.

    - ### Methods inherited from class rx.Subscriber

      `add, isUnsubscribed, onStart, request, setProducer, unsubscribe`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface io.vertx.core.streams.ReadStream

      `collect, pipe, pipeTo, pipeTo`

- - ### Field Detail

    - #### BUFFER_SIZE

          public static final int BUFFER_SIZE

      See Also:
        Constant Field Values

  - ### 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<J>`
      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<J>`
      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<J>`
      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<J>`
      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<J>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### exceptionHandler

          public ReadStream<J> exceptionHandler(Handler<Throwable> handler)

      Description copied from interface: `ReadStream`
      Set an exception handler on the read stream.
      Specified by:
        `exceptionHandler` in interface `ReadStream<J>`
      Specified by:
        `exceptionHandler` in interface `StreamBase`
      Parameters:
        `handler` - the exception handler
      Returns:
        a reference to this, so the API can be used fluently

    - #### onCompleted

          public void onCompleted()

      Specified by:
        `onCompleted` in interface `Observer<R>`

    - #### onError

          public void onError(Throwable e)

      Specified by:
        `onError` in interface `Observer<R>`

    - #### onNext

          public void onNext(R item)

      Specified by:
        `onNext` in interface `Observer<R>`

Copyright © 2025 Eclipse. All rights reserved.