io.vertx.rxjava.core.streams.Pipe 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;
/**
 * Pipe 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 pipe 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.Pipe original} non RX-ified interface using Vert.x codegen.
 */
@RxGen(io.vertx.core.streams.Pipe.class)
public class Pipe {
  @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;
    Pipe that = (Pipe) o;
    return delegate.equals(that.delegate);
  }
  
  @Override
  public int hashCode() {
    return delegate.hashCode();
  }
  public static final TypeArg __TYPE_ARG = new TypeArg<>(    obj -> new Pipe((io.vertx.core.streams.Pipe) obj),
    Pipe::getDelegate
  );
  private final io.vertx.core.streams.Pipe delegate;
  public final TypeArg __typeArg_0;
  
  public Pipe(io.vertx.core.streams.Pipe delegate) {
    this.delegate = delegate;
    this.__typeArg_0 = TypeArg.unknown();  }
  public Pipe(Object delegate, TypeArg typeArg_0) {
    this.delegate = (io.vertx.core.streams.Pipe)delegate;
    this.__typeArg_0 = typeArg_0;
  }
  public io.vertx.core.streams.Pipe getDelegate() {
    return delegate;
  }
  /**
   * Set to true to call {@link io.vertx.rxjava.core.streams.WriteStream#end} when the source ReadStream fails, false otherwise.
   * @param end true to end the stream on a source ReadStream failure
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.streams.Pipe endOnFailure(boolean end) { 
    delegate.endOnFailure(end);
    return this;
  }
  /**
   * Set to true to call {@link io.vertx.rxjava.core.streams.WriteStream#end} when the source ReadStream succeeds, false otherwise.
   * @param end true to end the stream on a source ReadStream success
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.streams.Pipe endOnSuccess(boolean end) { 
    delegate.endOnSuccess(end);
    return this;
  }
  /**
   * Set to true to call {@link io.vertx.rxjava.core.streams.WriteStream#end} when the source ReadStream completes, false otherwise.
   * 
   * Calling this overwrites {@link io.vertx.rxjava.core.streams.Pipe#endOnFailure} and {@link io.vertx.rxjava.core.streams.Pipe#endOnSuccess}.
   * @param end true to end the stream on a source ReadStream completion
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.streams.Pipe endOnComplete(boolean end) { 
    delegate.endOnComplete(end);
    return this;
  }
  /**
   * Start to pipe the elements to the destination WriteStream.
   * 
   * When the operation fails with a write error, the source stream is resumed.
   * @param dst the destination write stream
   * @param completionHandler the handler called when the pipe operation completes
   */
  public void to(io.vertx.rxjava.core.streams.WriteStream dst, io.vertx.core.Handler> completionHandler) { 
    delegate.to(dst.getDelegate(), completionHandler);
  }
  /**
   * Start to pipe the elements to the destination WriteStream.
   * 
   * When the operation fails with a write error, the source stream is resumed.
   * @param dst the destination write stream
   */
  public void to(io.vertx.rxjava.core.streams.WriteStream dst) {
    to(dst, ar -> { });
  }
    /**
   * Start to pipe the elements to the destination WriteStream.
   * 
   * When the operation fails with a write error, the source stream is resumed.
   * @param dst the destination write stream
   * @return 
   */
  public rx.Single rxTo(io.vertx.rxjava.core.streams.WriteStream dst) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      to(dst, fut);
    }));
  }
  /**
   * Close the pipe.
   * 
   * The streams handlers will be unset and the read stream resumed unless it is already ended.
   */
  public void close() { 
    delegate.close();
  }
  public static  Pipe newInstance(io.vertx.core.streams.Pipe arg) {
    return arg != null ? new Pipe(arg) : null;
  }
  public static  Pipe newInstance(io.vertx.core.streams.Pipe arg, TypeArg __typeArg_T) {
    return arg != null ? new Pipe(arg, __typeArg_T) : null;
  }
}