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

com.arangodb.shaded.vertx.ext.web.codec.impl.StreamingBodyCodec Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright 2014 Red Hat, Inc.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */
package com.arangodb.shaded.vertx.ext.web.codec.impl;

import com.arangodb.shaded.vertx.core.AsyncResult;
import com.arangodb.shaded.vertx.core.Future;
import com.arangodb.shaded.vertx.core.Handler;
import com.arangodb.shaded.vertx.core.Promise;
import com.arangodb.shaded.vertx.core.buffer.Buffer;
import com.arangodb.shaded.vertx.core.streams.WriteStream;
import com.arangodb.shaded.vertx.ext.web.codec.BodyCodec;
import com.arangodb.shaded.vertx.ext.web.codec.spi.BodyStream;

/**
 * @author Julien Viet
 */
public class StreamingBodyCodec implements BodyCodec {

  private final WriteStream stream;
  private final boolean close;
  private Throwable error;

  public StreamingBodyCodec(WriteStream stream) {
    this(stream, true);
  }

  public StreamingBodyCodec(WriteStream stream, boolean close) {
    this.stream = stream;
    this.close = close;
  }

  public void init() {
    stream.exceptionHandler(err -> {
      synchronized (StreamingBodyCodec.this) {
        error = err;
      }
    });
  }

  @Override
  public void create(Handler>> handler) {
    AsyncResult> result;
    synchronized (this) {
      if (error != null) {
        result = Future.failedFuture(error);
      } else {
        result = Future.succeededFuture(new BodyStream() {

          final Promise promise = Promise.promise();

          @Override
          public Future result() {
            return promise.future();
          }

          @Override
          public void handle(Throwable cause) {
            promise.tryFail(cause);
          }

          @Override
          public WriteStream exceptionHandler(Handler handler) {
            stream.exceptionHandler(handler);
            return this;
          }

          @Override
          public void write(Buffer data, Handler> handler) {
            stream.write(data, handler);
          }

          @Override
          public Future write(Buffer data) {
            Promise promise = Promise.promise();
            write(data, promise);
            return promise.future();
          }

          @Override
          public void end(Handler> handler) {
            if (close) {
              stream.end(ar -> {
                if (ar.succeeded()) {
                  promise.tryComplete();
                } else {
                  promise.tryFail(ar.cause());
                }
                if (handler != null) {
                  handler.handle(ar);
                }
              });
            } else {
              promise.tryComplete();
              if (handler != null) {
                handler.handle(Future.succeededFuture());
              }
            }
          }

          @Override
          public WriteStream setWriteQueueMaxSize(int maxSize) {
            stream.setWriteQueueMaxSize(maxSize);
            return this;
          }

          @Override
          public boolean writeQueueFull() {
            return stream.writeQueueFull();
          }

          @Override
          public WriteStream drainHandler(Handler handler) {
            stream.drainHandler(handler);
            return this;
          }
        });
      }
    }
    handler.handle(result);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy