All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.httpproxy.impl.BufferedReadStream Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */
package io.vertx.httpproxy.impl;

import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.streams.ReadStream;

public class BufferedReadStream implements ReadStream {

  private long demand = 0L;
  private Handler endHandler;
  private Handler handler;
  private boolean ended = false;
  private final Buffer content;

  public BufferedReadStream() {
    this.content = Buffer.buffer();
  }

  public BufferedReadStream(Buffer content) {
    this.content = content;
  }

  @Override
  public ReadStream exceptionHandler(Handler handler) {
    return this;
  }

  @Override
  public ReadStream handler(Handler handler) {
    this.handler = handler;
    return this;
  }

  @Override
  public ReadStream pause() {
    demand = 0L;
    return this;
  }

  @Override
  public ReadStream resume() {
    fetch(Long.MAX_VALUE);
    return this;
  }

  @Override
  public ReadStream fetch(long amount) {
    if (!ended && amount > 0) {
      ended = true;
      demand += amount;
      if (demand < 0L) {
        demand = Long.MAX_VALUE;
      }
      if (demand != Long.MAX_VALUE) {
        demand--;
      }
      if (handler != null && content.length() > 0) {
        handler.handle(content);
      }
      if (endHandler != null) {
        endHandler.handle(null);
      }
    }
    return this;
  }

  @Override
  public ReadStream endHandler(Handler endHandler) {
    this.endHandler = endHandler;
    return this;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy