Search in sources :

Example 1 with CodedInputByteBufferNano

use of com.google.protobuf.nano.CodedInputByteBufferNano in project grpc-java by grpc.

the class NanoUtils method marshaller.

/** Adapt {@code parser} to a {@code Marshaller}. */
public static <T extends MessageNano> Marshaller<T> marshaller(final MessageNanoFactory<T> factory) {
    return new Marshaller<T>() {

        @Override
        public InputStream stream(T value) {
            return new NanoProtoInputStream(value);
        }

        @Override
        public T parse(InputStream stream) {
            try {
                // TODO(simonma): Investigate whether we can do 0-copy here. 
                CodedInputByteBufferNano input = CodedInputByteBufferNano.newInstance(ByteStreams.toByteArray(stream));
                input.setSizeLimit(Integer.MAX_VALUE);
                T message = factory.newInstance();
                message.mergeFrom(input);
                return message;
            } catch (IOException ipbe) {
                throw Status.INTERNAL.withDescription("Failed parsing nano proto message").withCause(ipbe).asRuntimeException();
            }
        }
    };
}
Also used : Marshaller(io.grpc.MethodDescriptor.Marshaller) InputStream(java.io.InputStream) CodedInputByteBufferNano(com.google.protobuf.nano.CodedInputByteBufferNano) IOException(java.io.IOException)

Aggregations

CodedInputByteBufferNano (com.google.protobuf.nano.CodedInputByteBufferNano)1 Marshaller (io.grpc.MethodDescriptor.Marshaller)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1