本文整理汇总了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);
}
}
示例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);
}
}
};
}
示例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);
}
};
}
示例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);
}
示例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());
}
}
示例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)));
}