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

io.vertx.grpcio.common.impl.BridgeMessageDecoder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2011-2022 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.grpcio.common.impl;

import io.grpc.Decompressor;
import io.grpc.MethodDescriptor;
import io.vertx.grpc.common.WireFormat;
import io.vertx.grpc.common.GrpcMessage;
import io.vertx.grpc.common.GrpcMessageDecoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class BridgeMessageDecoder implements GrpcMessageDecoder {

  private MethodDescriptor.Marshaller marshaller;
  private Decompressor decompressor;

  public BridgeMessageDecoder(MethodDescriptor.Marshaller marshaller, Decompressor decompressor) {
    this.marshaller = marshaller;
    this.decompressor = decompressor;
  }

  @Override
  public T decode(GrpcMessage msg) {
    if (msg.encoding().equals("identity")) {
      return marshaller.parse(new ByteArrayInputStream(msg.payload().getBytes()));
    } else {
      try (InputStream in = decompressor.decompress(new ByteArrayInputStream(msg.payload().getBytes()))) {
        return marshaller.parse(in);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  }

  @Override
  public WireFormat format() {
    return WireFormat.PROTOBUF;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy