当前位置: 首页>>代码示例>>Java>>正文


Java Metadata.BinaryMarshaller方法代码示例

本文整理汇总了Java中io.grpc.Metadata.BinaryMarshaller方法的典型用法代码示例。如果您正苦于以下问题:Java Metadata.BinaryMarshaller方法的具体用法?Java Metadata.BinaryMarshaller怎么用?Java Metadata.BinaryMarshaller使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.grpc.Metadata的用法示例。


在下文中一共展示了Metadata.BinaryMarshaller方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: PROTOBUF_MARSHALLER

import io.grpc.Metadata; //导入方法依赖的package包/类
/**
 * A metadata marshaller that encodes objects as protobuf according to their proto IDL specification.
 *
 * @param clazz the type to serialize
 * @param <T>
 */
public static <T extends GeneratedMessageV3> Metadata.BinaryMarshaller<T> PROTOBUF_MARSHALLER(Class<T> clazz) {
    try {
        Method defaultInstance = clazz.getMethod("getDefaultInstance");
        GeneratedMessageV3 instance = (GeneratedMessageV3) defaultInstance.invoke(null);

        return new Metadata.BinaryMarshaller<T>() {
            @Override
            public byte[] toBytes(T value) {
                return value.toByteArray();
            }

            @SuppressWarnings("unchecked")
            @Override
            public T parseBytes(byte[] serialized) {
                try {
                    return (T) instance.getParserForType().parseFrom(serialized);
                } catch (InvalidProtocolBufferException ipbe) {
                    throw new IllegalArgumentException(ipbe);
                }
            }
        };
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
        throw new IllegalStateException(ex);
    }
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:32,代码来源:MoreMetadata.java

示例2: metadataMarshaller

import io.grpc.Metadata; //导入方法依赖的package包/类
/**
 * Produce a metadata marshaller for a protobuf type.
 */
public static <T extends MessageLite> Metadata.BinaryMarshaller<T> metadataMarshaller(
    final T instance) {
  return new Metadata.BinaryMarshaller<T>() {
    @Override
    public byte[] toBytes(T value) {
      return value.toByteArray();
    }

    @Override
    @SuppressWarnings("unchecked")
    public T parseBytes(byte[] serialized) {
      try {
        return (T) instance.getParserForType().parseFrom(serialized, globalRegistry);
      } catch (InvalidProtocolBufferException ipbe) {
        throw new IllegalArgumentException(ipbe);
      }
    }
  };
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:23,代码来源:ProtoLiteUtils.java

示例3: utf8Marshaller

import io.grpc.Metadata; //导入方法依赖的package包/类
private static Metadata.BinaryMarshaller<String> utf8Marshaller() {
  return new Metadata.BinaryMarshaller<String>() {

    @Override
    public byte[] toBytes(String value) {
      return value.getBytes(Charsets.UTF_8);
    }

    @Override
    public String parseBytes(byte[] serialized) {
      return new String(serialized, Charsets.UTF_8);
    }
  };
}
 
开发者ID:venus-boot,项目名称:saluki,代码行数:15,代码来源:GrpcUtil.java

示例4: protobufMarshallerRoundtrip

import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void protobufMarshallerRoundtrip() {
    HelloRequest request = HelloRequest.newBuilder().setName("World").build();

    Metadata.BinaryMarshaller<HelloRequest> marshaller = MoreMetadata.PROTOBUF_MARSHALLER(HelloRequest.class);
    byte[] bytes = marshaller.toBytes(request);
    HelloRequest request2 = marshaller.parseBytes(bytes);

    assertThat(request2).isEqualTo(request);
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:11,代码来源:MoreMetadataTest.java

示例5: metadataMarshaller_invalid

import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void metadataMarshaller_invalid() {
  Metadata.BinaryMarshaller<Type> metadataMarshaller =
      ProtoLiteUtils.metadataMarshaller(Type.getDefaultInstance());
  try {
    metadataMarshaller.parseBytes(new byte[] {-127});
    fail("Expected exception");
  } catch (IllegalArgumentException ex) {
    assertNotNull(((InvalidProtocolBufferException) ex.getCause()).getUnfinishedMessage());
  }
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:12,代码来源:ProtoLiteUtilsTest.java

示例6: metadataMarshaller_roundtrip

import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void metadataMarshaller_roundtrip() {
  Metadata.BinaryMarshaller<Type> metadataMarshaller =
      ProtoLiteUtils.metadataMarshaller(Type.getDefaultInstance());
  assertEquals(proto, metadataMarshaller.parseBytes(metadataMarshaller.toBytes(proto)));
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:7,代码来源:ProtoLiteUtilsTest.java


注:本文中的io.grpc.Metadata.BinaryMarshaller方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。