io.vertx.rxjava.core.streams.Pump Maven / Gradle / Ivy
/*
 * Copyright 2014 Red Hat, Inc.
 *
 * Red Hat licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.vertx.rxjava.core.streams;
import rx.Observable;
import rx.Single;
import io.vertx.rx.java.RxHelper;
import io.vertx.rx.java.WriteStreamSubscriber;
import io.vertx.rx.java.SingleOnSubscribeAdapter;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Collectors;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.lang.rx.RxGen;
import io.vertx.lang.rx.TypeArg;
import io.vertx.lang.rx.MappingIterator;
/**
 * Pumps data from a {@link io.vertx.rxjava.core.streams.ReadStream} to a {@link io.vertx.rxjava.core.streams.WriteStream} and performs flow control where necessary to
 * prevent the write stream buffer from getting overfull.
 * 
 * Instances of this class read items from a {@link io.vertx.rxjava.core.streams.ReadStream} and write them to a {@link io.vertx.rxjava.core.streams.WriteStream}. If data
 * can be read faster than it can be written this could result in the write queue of the {@link io.vertx.rxjava.core.streams.WriteStream} growing
 * without bound, eventually causing it to exhaust all available RAM.
 * 
 * To prevent this, after each write, instances of this class check whether the write queue of the {@link io.vertx.rxjava.core.streams.WriteStream} is full, and if so, the {@link io.vertx.rxjava.core.streams.ReadStream} is paused, and a drainHandler is set on the
 * {@link io.vertx.rxjava.core.streams.WriteStream}.
 * 
 * When the {@link io.vertx.rxjava.core.streams.WriteStream} has processed half of its backlog, the drainHandler will be
 * called, which results in the pump resuming the {@link io.vertx.rxjava.core.streams.ReadStream}.
 * 
 * This class can be used to pump from any {@link io.vertx.rxjava.core.streams.ReadStream} to any {@link io.vertx.rxjava.core.streams.WriteStream},
 * e.g. from an {@link io.vertx.rxjava.core.http.HttpServerRequest} to an {@link io.vertx.rxjava.core.file.AsyncFile},
 * or from {@link io.vertx.rxjava.core.net.NetSocket} to a {@link io.vertx.rxjava.core.http.WebSocket}.
 * 
 * Please see the documentation for more information.
 *
 * 
 * NOTE: This class has been automatically generated from the {@link io.vertx.core.streams.Pump original} non RX-ified interface using Vert.x codegen.
 */
@RxGen(io.vertx.core.streams.Pump.class)
public class Pump {
  @Override
  public String toString() {
    return delegate.toString();
  }
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Pump that = (Pump) o;
    return delegate.equals(that.delegate);
  }
  
  @Override
  public int hashCode() {
    return delegate.hashCode();
  }
  public static final TypeArg __TYPE_ARG = new TypeArg<>(    obj -> new Pump((io.vertx.core.streams.Pump) obj),
    Pump::getDelegate
  );
  private final io.vertx.core.streams.Pump delegate;
  
  public Pump(io.vertx.core.streams.Pump delegate) {
    this.delegate = delegate;
  }
  public Pump(Object delegate) {
    this.delegate = (io.vertx.core.streams.Pump)delegate;
  }
  public io.vertx.core.streams.Pump getDelegate() {
    return delegate;
  }
  /**
   * Create a new Pump with the given ReadStream and WriteStream
   * @param rs the read stream
   * @param ws the write stream
   * @return the pump
   */
  public static  io.vertx.rxjava.core.streams.Pump pump(io.vertx.rxjava.core.streams.ReadStream rs, io.vertx.rxjava.core.streams.WriteStream ws) { 
    io.vertx.rxjava.core.streams.Pump ret = io.vertx.rxjava.core.streams.Pump.newInstance((io.vertx.core.streams.Pump)io.vertx.core.streams.Pump.pump(rs.getDelegate(), ws.getDelegate()));
    return ret;
  }
  /**
   * Create a new Pump with the given ReadStream and WriteStream
   * @param rs the read stream
   * @param ws the write stream
   * @return the pump
   */
  public static  io.vertx.rxjava.core.streams.Pump pump(rx.Observable rs, io.vertx.rxjava.core.streams.WriteStream ws) { 
    io.vertx.rxjava.core.streams.Pump ret = io.vertx.rxjava.core.streams.Pump.newInstance((io.vertx.core.streams.Pump)io.vertx.core.streams.Pump.pump(io.vertx.rx.java.ReadStreamSubscriber.asReadStream(rs,Function.identity()).resume(), ws.getDelegate()));
    return ret;
  }
  /**
   * Create a new Pump with the given ReadStream and WriteStream and
   * writeQueueMaxSize
   * @param rs the read stream
   * @param ws the write stream
   * @param writeQueueMaxSize the max size of the write queue
   * @return the pump
   */
  public static  io.vertx.rxjava.core.streams.Pump pump(io.vertx.rxjava.core.streams.ReadStream rs, io.vertx.rxjava.core.streams.WriteStream ws, int writeQueueMaxSize) { 
    io.vertx.rxjava.core.streams.Pump ret = io.vertx.rxjava.core.streams.Pump.newInstance((io.vertx.core.streams.Pump)io.vertx.core.streams.Pump.pump(rs.getDelegate(), ws.getDelegate(), writeQueueMaxSize));
    return ret;
  }
  /**
   * Create a new Pump with the given ReadStream and WriteStream and
   * writeQueueMaxSize
   * @param rs the read stream
   * @param ws the write stream
   * @param writeQueueMaxSize the max size of the write queue
   * @return the pump
   */
  public static  io.vertx.rxjava.core.streams.Pump pump(rx.Observable rs, io.vertx.rxjava.core.streams.WriteStream ws, int writeQueueMaxSize) { 
    io.vertx.rxjava.core.streams.Pump ret = io.vertx.rxjava.core.streams.Pump.newInstance((io.vertx.core.streams.Pump)io.vertx.core.streams.Pump.pump(io.vertx.rx.java.ReadStreamSubscriber.asReadStream(rs,Function.identity()).resume(), ws.getDelegate(), writeQueueMaxSize));
    return ret;
  }
  /**
   * Set the write queue max size to maxSize
   * @param maxSize the max size
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.streams.Pump setWriteQueueMaxSize(int maxSize) { 
    delegate.setWriteQueueMaxSize(maxSize);
    return this;
  }
  /**
   * Start the Pump. The Pump can be started and stopped multiple times.
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.streams.Pump start() { 
    delegate.start();
    return this;
  }
  /**
   * Stop the Pump. The Pump can be started and stopped multiple times.
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.streams.Pump stop() { 
    delegate.stop();
    return this;
  }
  /**
   * Return the total number of items pumped by this pump.
   * @return 
   */
  public int numberPumped() { 
    int ret = delegate.numberPumped();
    return ret;
  }
  public static Pump newInstance(io.vertx.core.streams.Pump arg) {
    return arg != null ? new Pump(arg) : null;
  }
}