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

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

There is a newer version: 7.9.0
Show newest version
package com.arangodb.shaded.vertx.ext.web.codec.impl;

import com.arangodb.shaded.vertx.codegen.annotations.Nullable;
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.buffer.Buffer;
import com.arangodb.shaded.vertx.core.parsetools.JsonParser;
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;

import java.util.Objects;

public class JsonStreamBodyCodec implements BodyCodec {

  private final JsonParser parser;
  private final StreamingBodyCodec delegate;

  public JsonStreamBodyCodec(JsonParser parser) {
    this.parser = Objects.requireNonNull(parser, "The parser must be set");
    this.delegate = new StreamingBodyCodec(new WriteStream() {
      @Override
      public WriteStream exceptionHandler(Handler handler) {
        parser.exceptionHandler(handler);
        return this;
      }

      @Override
      public Future write(Buffer buffer) {
        parser.handle(buffer);
        return Future.succeededFuture();
      }

      @Override
      public void write(Buffer data, Handler> handler) {
        parser.handle(data);
        if (handler != null) {
          handler.handle(Future.succeededFuture());
        }
      }

      @Override
      public void end(Handler> handler) {
        parser.end();
        if (handler != null) {
          handler.handle(Future.succeededFuture());
        }
      }

      @Override
      public WriteStream setWriteQueueMaxSize(int i) {
        return this;
      }

      @Override
      public boolean writeQueueFull() {
        return false;
      }

      @Override
      public WriteStream drainHandler(@Nullable Handler handler) {
        return this;
      }
    });
  }

  /**
   * @return the JSON parser.
   */
  JsonParser getParser() {
    return parser;
  }


  @Override
  public void create(Handler>> handler) {
    delegate.create(handler);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy