當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。