io.vertx.rxjava.core.file.AsyncFile 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.file;
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;
/**
 * Represents a file on the file-system which can be read from, or written to asynchronously.
 * 
 * This class also implements {@link io.vertx.rxjava.core.streams.ReadStream} and
 * {@link io.vertx.rxjava.core.streams.WriteStream}. This allows the data to be piped to and from
 * other streams, e.g. an {@link io.vertx.rxjava.core.http.HttpClientRequest} instance,
 * using the {@link io.vertx.rxjava.core.streams.Pipe} class
 *
 * 
 * NOTE: This class has been automatically generated from the {@link io.vertx.core.file.AsyncFile original} non RX-ified interface using Vert.x codegen.
 */
@RxGen(io.vertx.core.file.AsyncFile.class)
public class AsyncFile implements io.vertx.rxjava.core.streams.ReadStream, io.vertx.rxjava.core.streams.WriteStream {
  @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;
    AsyncFile that = (AsyncFile) o;
    return delegate.equals(that.delegate);
  }
  
  @Override
  public int hashCode() {
    return delegate.hashCode();
  }
  public static final TypeArg __TYPE_ARG = new TypeArg<>(    obj -> new AsyncFile((io.vertx.core.file.AsyncFile) obj),
    AsyncFile::getDelegate
  );
  private final io.vertx.core.file.AsyncFile delegate;
  
  public AsyncFile(io.vertx.core.file.AsyncFile delegate) {
    this.delegate = delegate;
  }
  public AsyncFile(Object delegate) {
    this.delegate = (io.vertx.core.file.AsyncFile)delegate;
  }
  public io.vertx.core.file.AsyncFile getDelegate() {
    return delegate;
  }
  private Observable observable;
  public synchronized Observable toObservable() {
    if (observable == null) {
      Function conv = io.vertx.rxjava.core.buffer.Buffer::newInstance;
      observable = RxHelper.toObservable(delegate, conv);
    }
    return observable;
  }
  private WriteStreamSubscriber subscriber;
  public synchronized WriteStreamSubscriber toSubscriber() {
    if (subscriber == null) {
      Function conv = io.vertx.rxjava.core.buffer.Buffer::getDelegate;
      subscriber = RxHelper.toSubscriber(getDelegate(), conv);
    }
    return subscriber;
  }
  private static final TypeArg TYPE_ARG_0 = new TypeArg(o1 -> io.vertx.rxjava.core.buffer.Buffer.newInstance((io.vertx.core.buffer.Buffer)o1), o1 -> o1.getDelegate());
  /**
   * Same as  but with an handler called when the operation completes
   * @param data 
   * @param handler 
   */
  public void write(io.vertx.rxjava.core.buffer.Buffer data, io.vertx.core.Handler> handler) { 
    delegate.write(data.getDelegate(), handler);
  }
  /**
   * Same as  but with an handler called when the operation completes
   * @param data 
   */
  public void write(io.vertx.rxjava.core.buffer.Buffer data) {
    write(data, ar -> { });
  }
    /**
   * Same as  but with an handler called when the operation completes
   * @param data 
   * @return 
   */
  public rx.Single rxWrite(io.vertx.rxjava.core.buffer.Buffer data) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      write(data, fut);
    }));
  }
  /**
   * Same as {@link io.vertx.rxjava.core.streams.WriteStream#end} but with an handler called when the operation completes
   * @param handler 
   */
  public void end(io.vertx.core.Handler> handler) { 
    delegate.end(handler);
  }
  /**
   * Same as {@link io.vertx.rxjava.core.streams.WriteStream#end} but with an handler called when the operation completes
   */
  public void end() {
    end(ar -> { });
  }
    /**
   * Same as {@link io.vertx.rxjava.core.streams.WriteStream#end} but with an handler called when the operation completes
   * @return 
   */
  public rx.Single rxEnd() { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      end(fut);
    }));
  }
  /**
   * Same as  but with an handler called when the operation completes
   * @param data 
   * @param handler 
   */
  public void end(io.vertx.rxjava.core.buffer.Buffer data, io.vertx.core.Handler> handler) { 
    delegate.end(data.getDelegate(), handler);
  }
  /**
   * Same as  but with an handler called when the operation completes
   * @param data 
   */
  public void end(io.vertx.rxjava.core.buffer.Buffer data) {
    end(data, ar -> { });
  }
    /**
   * Same as  but with an handler called when the operation completes
   * @param data 
   * @return 
   */
  public rx.Single rxEnd(io.vertx.rxjava.core.buffer.Buffer data) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      end(data, fut);
    }));
  }
  /**
   * This will return true if there are more bytes in the write queue than the value set using {@link io.vertx.rxjava.core.file.AsyncFile#setWriteQueueMaxSize}
   * @return true if write queue is full
   */
  public boolean writeQueueFull() { 
    boolean ret = delegate.writeQueueFull();
    return ret;
  }
  /**
   * Pause this stream and return a  to transfer the elements of this stream to a destination .
   * 
   * The stream will be resumed when the pipe will be wired to a WriteStream.
   * @return a pipe
   */
  public io.vertx.rxjava.core.streams.Pipe pipe() { 
    io.vertx.rxjava.core.streams.Pipe ret = io.vertx.rxjava.core.streams.Pipe.newInstance((io.vertx.core.streams.Pipe)delegate.pipe(), TYPE_ARG_0);
    return ret;
  }
  /**
   * Pipe this ReadStream to the WriteStream.
   * 
   * Elements emitted by this stream will be written to the write stream until this stream ends or fails.
   * 
   * Once this stream has ended or failed, the write stream will be ended and the handler will be
   * called with the result.
   * @param dst the destination write stream
   * @param handler 
   */
  public void pipeTo(io.vertx.rxjava.core.streams.WriteStream dst, io.vertx.core.Handler> handler) { 
    delegate.pipeTo(dst.getDelegate(), handler);
  }
  /**
   * Pipe this ReadStream to the WriteStream.
   * 
   * Elements emitted by this stream will be written to the write stream until this stream ends or fails.
   * 
   * Once this stream has ended or failed, the write stream will be ended and the handler will be
   * called with the result.
   * @param dst the destination write stream
   */
  public void pipeTo(io.vertx.rxjava.core.streams.WriteStream dst) {
    pipeTo(dst, ar -> { });
  }
    /**
   * Pipe this ReadStream to the WriteStream.
   * 
   * Elements emitted by this stream will be written to the write stream until this stream ends or fails.
   * 
   * Once this stream has ended or failed, the write stream will be ended and the handler will be
   * called with the result.
   * @param dst the destination write stream
   * @return 
   */
  public rx.Single rxPipeTo(io.vertx.rxjava.core.streams.WriteStream dst) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      pipeTo(dst, fut);
    }));
  }
  public io.vertx.rxjava.core.file.AsyncFile handler(io.vertx.core.Handler handler) { 
    delegate.handler(new Handler() {
      public void handle(io.vertx.core.buffer.Buffer event) {
        handler.handle(io.vertx.rxjava.core.buffer.Buffer.newInstance((io.vertx.core.buffer.Buffer)event));
      }
    });
    return this;
  }
  public io.vertx.rxjava.core.file.AsyncFile pause() { 
    delegate.pause();
    return this;
  }
  public io.vertx.rxjava.core.file.AsyncFile resume() { 
    delegate.resume();
    return this;
  }
  public io.vertx.rxjava.core.file.AsyncFile endHandler(io.vertx.core.Handler endHandler) { 
    delegate.endHandler(endHandler);
    return this;
  }
  public io.vertx.rxjava.core.file.AsyncFile setWriteQueueMaxSize(int maxSize) { 
    delegate.setWriteQueueMaxSize(maxSize);
    return this;
  }
  public io.vertx.rxjava.core.file.AsyncFile drainHandler(io.vertx.core.Handler handler) { 
    delegate.drainHandler(handler);
    return this;
  }
  public io.vertx.rxjava.core.file.AsyncFile exceptionHandler(io.vertx.core.Handler handler) { 
    delegate.exceptionHandler(handler);
    return this;
  }
  public io.vertx.rxjava.core.file.AsyncFile fetch(long amount) { 
    delegate.fetch(amount);
    return this;
  }
  /**
   * Close the file. The actual close happens asynchronously.
   * The handler will be called when the close is complete, or an error occurs.
   * @param handler the handler
   */
  public void close(io.vertx.core.Handler> handler) { 
    delegate.close(handler);
  }
  /**
   * Close the file. The actual close happens asynchronously.
   * The handler will be called when the close is complete, or an error occurs.
   */
  public void close() {
    close(ar -> { });
  }
    /**
   * Close the file. The actual close happens asynchronously.
   * The handler will be called when the close is complete, or an error occurs.
   * @return 
   */
  public rx.Single rxClose() { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      close(fut);
    }));
  }
  /**
   * Write a {@link io.vertx.rxjava.core.buffer.Buffer} to the file at position position in the file, asynchronously.
   * 
   * If position lies outside of the current size
   * of the file, the file will be enlarged to encompass it.
   * 
   * When multiple writes are invoked on the same file
   * there are no guarantees as to order in which those writes actually occur
   * 
   * The handler will be called when the write is complete, or if an error occurs.
   * @param buffer the buffer to write
   * @param position the position in the file to write it at
   * @param handler the handler to call when the write is complete
   */
  public void write(io.vertx.rxjava.core.buffer.Buffer buffer, long position, io.vertx.core.Handler> handler) { 
    delegate.write(buffer.getDelegate(), position, handler);
  }
  /**
   * Write a {@link io.vertx.rxjava.core.buffer.Buffer} to the file at position position in the file, asynchronously.
   * 
   * If position lies outside of the current size
   * of the file, the file will be enlarged to encompass it.
   * 
   * When multiple writes are invoked on the same file
   * there are no guarantees as to order in which those writes actually occur
   * 
   * The handler will be called when the write is complete, or if an error occurs.
   * @param buffer the buffer to write
   * @param position the position in the file to write it at
   */
  public void write(io.vertx.rxjava.core.buffer.Buffer buffer, long position) {
    write(buffer, position, ar -> { });
  }
    /**
   * Write a {@link io.vertx.rxjava.core.buffer.Buffer} to the file at position position in the file, asynchronously.
   * 
   * If position lies outside of the current size
   * of the file, the file will be enlarged to encompass it.
   * 
   * When multiple writes are invoked on the same file
   * there are no guarantees as to order in which those writes actually occur
   * 
   * The handler will be called when the write is complete, or if an error occurs.
   * @param buffer the buffer to write
   * @param position the position in the file to write it at
   * @return 
   */
  public rx.Single rxWrite(io.vertx.rxjava.core.buffer.Buffer buffer, long position) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      write(buffer, position, fut);
    }));
  }
  /**
   * Reads length bytes of data from the file at position position in the file, asynchronously.
   * 
   * The read data will be written into the specified Buffer buffer at position offset.
   * 
   * If data is read past the end of the file then zero bytes will be read.
   * When multiple reads are invoked on the same file there are no guarantees as to order in which those reads actually occur.
   * 
   * The handler will be called when the close is complete, or if an error occurs.
   * @param buffer the buffer to read into
   * @param offset the offset into the buffer where the data will be read
   * @param position the position in the file where to start reading
   * @param length the number of bytes to read
   * @param handler the handler to call when the write is complete
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.file.AsyncFile read(io.vertx.rxjava.core.buffer.Buffer buffer, int offset, long position, int length, io.vertx.core.Handler> handler) { 
    delegate.read(buffer.getDelegate(), offset, position, length, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.buffer.Buffer.newInstance((io.vertx.core.buffer.Buffer)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
    return this;
  }
  /**
   * Reads length bytes of data from the file at position position in the file, asynchronously.
   * 
   * The read data will be written into the specified Buffer buffer at position offset.
   * 
   * If data is read past the end of the file then zero bytes will be read.
   * When multiple reads are invoked on the same file there are no guarantees as to order in which those reads actually occur.
   * 
   * The handler will be called when the close is complete, or if an error occurs.
   * @param buffer the buffer to read into
   * @param offset the offset into the buffer where the data will be read
   * @param position the position in the file where to start reading
   * @param length the number of bytes to read
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.file.AsyncFile read(io.vertx.rxjava.core.buffer.Buffer buffer, int offset, long position, int length) {
    return 
read(buffer, offset, position, length, ar -> { });
  }
    /**
   * Reads length bytes of data from the file at position position in the file, asynchronously.
   * 
   * The read data will be written into the specified Buffer buffer at position offset.
   * 
   * If data is read past the end of the file then zero bytes will be read.
   * When multiple reads are invoked on the same file there are no guarantees as to order in which those reads actually occur.
   * 
   * The handler will be called when the close is complete, or if an error occurs.
   * @param buffer the buffer to read into
   * @param offset the offset into the buffer where the data will be read
   * @param position the position in the file where to start reading
   * @param length the number of bytes to read
   * @return a reference to this, so the API can be used fluently
   */
  public rx.Single rxRead(io.vertx.rxjava.core.buffer.Buffer buffer, int offset, long position, int length) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      read(buffer, offset, position, length, fut);
    }));
  }
  /**
   * Same as {@link io.vertx.rxjava.core.file.AsyncFile#flush} but the handler will be called when the flush is complete or if an error occurs
   * @param handler 
   * @return 
   */
  public io.vertx.rxjava.core.file.AsyncFile flush(io.vertx.core.Handler> handler) { 
    delegate.flush(handler);
    return this;
  }
  /**
   * Same as {@link io.vertx.rxjava.core.file.AsyncFile#flush} but the handler will be called when the flush is complete or if an error occurs
   * @return 
   */
  public io.vertx.rxjava.core.file.AsyncFile flush() {
    return 
flush(ar -> { });
  }
    /**
   * Same as {@link io.vertx.rxjava.core.file.AsyncFile#flush} but the handler will be called when the flush is complete or if an error occurs
   * @return 
   */
  public rx.Single rxFlush() { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      flush(fut);
    }));
  }
  /**
   * Sets the position from which data will be read from when using the file as a {@link io.vertx.rxjava.core.streams.ReadStream}.
   * @param readPos the position in the file
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.file.AsyncFile setReadPos(long readPos) { 
    delegate.setReadPos(readPos);
    return this;
  }
  /**
   * Sets the number of bytes that will be read when using the file as a {@link io.vertx.rxjava.core.streams.ReadStream}.
   * @param readLength the bytes that will be read from the file
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.file.AsyncFile setReadLength(long readLength) { 
    delegate.setReadLength(readLength);
    return this;
  }
  /**
   * @return the number of bytes that will be read when using the file as a {@link io.vertx.rxjava.core.streams.ReadStream}
   */
  public long getReadLength() { 
    long ret = delegate.getReadLength();
    return ret;
  }
  /**
   * Sets the position from which data will be written when using the file as a {@link io.vertx.rxjava.core.streams.WriteStream}.
   * @param writePos the position in the file
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.file.AsyncFile setWritePos(long writePos) { 
    delegate.setWritePos(writePos);
    return this;
  }
  /**
   * @return the current write position the file is at
   */
  public long getWritePos() { 
    long ret = delegate.getWritePos();
    return ret;
  }
  /**
   * Sets the buffer size that will be used to read the data from the file. Changing this value will impact how much
   * the data will be read at a time from the file system.
   * @param readBufferSize the buffer size
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.file.AsyncFile setReadBufferSize(int readBufferSize) { 
    delegate.setReadBufferSize(readBufferSize);
    return this;
  }
  /**
   * Like {@link io.vertx.rxjava.core.file.AsyncFile#size} but blocking.
   * @return 
   */
  public long sizeBlocking() { 
    long ret = delegate.sizeBlocking();
    return ret;
  }
  /**
   * Like {@link io.vertx.rxjava.core.file.AsyncFile#size} but the handler will be called when the operation is complete or if an error occurs.
   * @param handler 
   */
  public void size(io.vertx.core.Handler> handler) { 
    delegate.size(handler);
  }
  /**
   * Like {@link io.vertx.rxjava.core.file.AsyncFile#size} but the handler will be called when the operation is complete or if an error occurs.
   */
  public void size() {
    size(ar -> { });
  }
    /**
   * Like {@link io.vertx.rxjava.core.file.AsyncFile#size} but the handler will be called when the operation is complete or if an error occurs.
   * @return 
   */
  public rx.Single rxSize() { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      size(fut);
    }));
  }
  public static AsyncFile newInstance(io.vertx.core.file.AsyncFile arg) {
    return arg != null ? new AsyncFile(arg) : null;
  }
}