com.google.protobuf.Parser Java Examples

The following examples show how to use com.google.protobuf.Parser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PubSubClient.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Subscriber() {
    Parser<T> parser = null;
    try {
        Class<?> cl = getClass();
        while (!Subscriber.class.equals(cl.getSuperclass())) {
            // case of multiple inheritance, we are trying to get the
            // first available generic info
            if (cl.getGenericSuperclass() instanceof ParameterizedType) {
                break;
            }
            cl = cl.getSuperclass();
        }
        Class<T> type = ((Class<T>) ((ParameterizedType) cl.getGenericSuperclass())
                .getActualTypeArguments()[0]);
        parser = (Parser<T>) type.getDeclaredField("PARSER").get(null);
    } catch (Exception e) {
        System.out.println("Error: callback creation failed");
        e.printStackTrace();
    }
    this.parser = parser;
}
 
Example #2
Source File: ProtoDecoder.java    From xrpc with Apache License 2.0 6 votes vote down vote up
/**
 * Decode a ByteBuf body from protobuf format to an object of designated Class type.
 *
 * @param body current http request
 * @param clazz target class for decoding
 * @return object of type clazz
 */
@Override
@SuppressWarnings("unchecked")
public <T> T decode(ByteBuf body, CharSequence contentType, Class<T> clazz) throws IOException {
  // TODO (AD): given a Content-Type of application/protobuf; proto=org.some.Message,
  // we currently ignore the 2nd part, but should at least validate it in the future.

  if (!MessageLite.class.isAssignableFrom(clazz)) {
    throw new IllegalArgumentException(
        String.format("%s does not extend from MessageLite", clazz.getName()));
  }

  MessageLite message = protoDefaultInstances.get(clazz);
  Parser<?> parser = message.getParserForType();
  try (ByteBufInputStream stream = new ByteBufInputStream(body)) {
    return (T) parser.parseFrom(stream);
  }
}
 
Example #3
Source File: ProtoCoder.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Get the memoized {@link Parser}, possibly initializing it lazily. */
protected Parser<T> getParser() {
  if (memoizedParser == null) {
    try {
      if (DynamicMessage.class.equals(protoMessageClass)) {
        throw new IllegalArgumentException(
            "DynamicMessage is not supported by the ProtoCoder, use the DynamicProtoCoder.");
      } else {
        @SuppressWarnings("unchecked")
        T protoMessageInstance =
            (T) protoMessageClass.getMethod("getDefaultInstance").invoke(null);
        @SuppressWarnings("unchecked")
        Parser<T> tParser = (Parser<T>) protoMessageInstance.getParserForType();
        memoizedParser = tParser;
      }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
      throw new IllegalArgumentException(e);
    }
  }
  return memoizedParser;
}
 
Example #4
Source File: ByteSerializerFactory.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <P extends com.google.protobuf.Message> Serializer<P, byte[]> visitProtobufFormat(Class<P> clazz) {

  Parser<P> parser;
  try {
    Method defaultInstanceGetter = clazz.getDeclaredMethod("getDefaultInstance");
    com.google.protobuf.Message defaultInst = (com.google.protobuf.Message) defaultInstanceGetter.invoke(null);
    parser = (Parser<P>) defaultInst.getParserForType();
    Preconditions.checkNotNull(parser);
  } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
    throw new DatastoreFatalException("Unable to get the parser for " + clazz.getName(), e);
  }

  return new ProtobufSerializer<>(clazz, parser);
}
 
Example #5
Source File: SyncMessageReader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public XMessageHeader readHeader() throws IOException {
    if (!this.hasReadHeader) {
        try {
            readMessageHeader();
        } catch (IOException ex) {
            throw new CJCommunicationsException("Cannot read packet header", ex);
        }
    }
    int type = this.header.getMessageType(); // forces header read if necessary

    Class<? extends GeneratedMessage> messageClass = MessageConstants.getMessageClassForType(type);

    if (messageClass == Error.class) {
        // throw an error/exception if receive an Error message
        throw new XProtocolError(readAndParse((Parser<Error>) MessageConstants.MESSAGE_CLASS_TO_PARSER.get(Error.class)));
    }

    return this.header;
}
 
Example #6
Source File: MessagePayloadSerializerPb.java    From flink-statefun with Apache License 2.0 6 votes vote down vote up
@Override
public Object copy(@Nonnull ClassLoader targetClassLoader, @Nonnull Object what) {
  Objects.requireNonNull(targetClassLoader);
  if (!(what instanceof Message)) {
    throw new IllegalStateException();
  }
  Message message = (Message) what;
  ByteString messageBytes = message.toByteString();
  try {
    Parser<? extends Message> parser =
        parserForClassName(targetClassLoader, what.getClass().getName());
    return parser.parseFrom(messageBytes);
  } catch (InvalidProtocolBufferException | ClassNotFoundException e) {
    throw new IllegalStateException(e);
  }
}
 
Example #7
Source File: ProtoBufSerializationProvider.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
ProtoBufSerializationProvider(final Class<T> targetClass, final GrpcMessageEncoding messageEncoding,
                              final Parser<T> parser) {
    this.targetClass = targetClass;
    this.messageEncoding = messageEncoding;
    this.serializer = new ProtoSerializer(messageEncoding);
    this.parser = parser;
}
 
Example #8
Source File: ProtoBufSerializePerformanceTest.java    From fastjgame with Apache License 2.0 5 votes vote down vote up
private static void codecTest(Message msg, int loopTimes, Map<Class<?>, Parser<? extends Message>> parserMap) throws IOException {
    final long start = System.currentTimeMillis();
    for (int index = 0; index < loopTimes; index++) {
        // 这里需要简单模拟下解码过程
        final CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(buffer);
        writeTypeId(msg, codedOutputStream);
        msg.writeTo(codedOutputStream);

        final CodedInputStream inputStream = CodedInputStream.newInstance(buffer, 0, codedOutputStream.getTotalBytesWritten());
        final Class<?> messageClass = readType(inputStream);
        final Parser<?> parser = parserMap.get(messageClass);
        final Object decodeMsg = parser.parseFrom(inputStream);
    }
    System.out.println("codec " + loopTimes + " times cost timeMs " + (System.currentTimeMillis() - start));
}
 
Example #9
Source File: PolyglotUtil.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
public static <M extends Message> M parseProtobufOrThrow(Parser<M> parser, InputStream input) {
  try {
    return parser.parseFrom(input);
  } catch (IOException e) {
    throw new IllegalStateException("Unable to parse a Protobuf message", e);
  }
}
 
Example #10
Source File: MessagePayloadSerializerPb.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private Parser<? extends Message> findParser(
    ClassLoader userCodeClassLoader, String messageClassName) throws ClassNotFoundException {
  Class<? extends Message> messageType =
      Class.forName(messageClassName, true, userCodeClassLoader).asSubclass(Message.class);

  return ProtobufReflectionUtil.protobufParser(messageType);
}
 
Example #11
Source File: MessagePayloadSerializerPb.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private Parser<? extends Message> parserForClassName(
    ClassLoader userCodeClassLoader, String messageClassName) throws ClassNotFoundException {

  ObjectOpenHashMap<ClassLoader, Parser<? extends Message>> classLoaders =
      PARSER_CACHE.get(messageClassName);
  if (classLoaders == null) {
    PARSER_CACHE.put(messageClassName, classLoaders = new ObjectOpenHashMap<>());
  }
  Parser<? extends Message> parser = classLoaders.get(userCodeClassLoader);
  if (parser == null) {
    classLoaders.put(
        userCodeClassLoader, parser = findParser(userCodeClassLoader, messageClassName));
  }
  return parser;
}
 
Example #12
Source File: MessagePayloadSerializerPb.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserialize(@Nonnull ClassLoader targetClassLoader, @Nonnull Payload payload) {
  try {
    Parser<? extends Message> parser =
        parserForClassName(targetClassLoader, payload.getClassName());
    return parser.parseFrom(payload.getPayloadBytes());
  } catch (InvalidProtocolBufferException | ClassNotFoundException e) {
    throw new IllegalStateException(e);
  }
}
 
Example #13
Source File: ProtobufReflectionUtil.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <M extends Message> Parser<M> protobufParser(Class<M> messageClass) {
  Object parser = getParserFromGeneratedMessage(messageClass);
  if (!(parser instanceof Parser)) {
    throw new IllegalStateException(
        "was expecting a protobuf parser to be return from the static parser() method on the type  "
            + messageClass
            + " but instead got "
            + parser);
  }
  return (Parser<M>) parser;
}
 
Example #14
Source File: ProtobufReflectionUtil.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <M extends Message> Parser<M> protobufParser(Class<M> messageClass) {
  Object parser = getParserFromGeneratedMessage(messageClass);
  if (!(parser instanceof Parser)) {
    throw new IllegalStateException(
        "was expecting a protobuf parser to be return from the static parser() method on the type  "
            + messageClass
            + " but instead got "
            + parser);
  }
  return (Parser<M>) parser;
}
 
Example #15
Source File: JobsProtoUtil.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Generic method to convert Protostuff to Protobuf. Uses LegacyProtobufSerializer because deserializing with the
 * regular protobuf serializer does not handle repeated fields correctly.
 * @param protobufParser Parser for protobuf object
 * @param protostuff Protostuff object to convert
 * @param <M> Type of Protobuf
 * @param <T> Type of Protobuff
 * @return Converted object as Protobuf
 */
private static <M extends GeneratedMessageV3, T extends Message<T> & Schema<T>>
M toBuf(Parser<M> protobufParser, T protostuff) {
  try {
    LinkedBuffer buffer = LinkedBuffer.allocate();
    byte[] bytes = ProtobufIOUtil.toByteArray(protostuff, protostuff.cachedSchema(), buffer);
    // LegacyProtobufSerializer is necessary as it deals with stuff/buf grouping differences
    return LegacyProtobufSerializer.parseFrom(protobufParser, bytes);
  } catch (InvalidProtocolBufferException e) {
    throw new IllegalArgumentException("Cannot convert from protostuff to protobuf");
  }
}
 
Example #16
Source File: BinarySerializer.java    From fastjgame with Apache License 2.0 5 votes vote down vote up
/**
 * @param typeModelMapper 类型映射信息
 * @param filter          由于{@link BinarySerializer}支持的消息类是确定的,不能加入,但是允许过滤删除
 */
@SuppressWarnings("unchecked")
public static BinarySerializer newInstance(TypeModelMapper typeModelMapper, Predicate<Class<?>> filter) {
    final Set<Class<?>> supportedClassSet = getFilteredSupportedClasses(filter);
    final List<PojoCodecImpl<?>> codecList = new ArrayList<>(supportedClassSet.size());
    try {
        for (Class<?> messageClazz : supportedClassSet) {
            // protoBuf消息
            if (Message.class.isAssignableFrom(messageClazz)) {
                Parser<?> parser = ProtoUtils.findParser((Class<? extends Message>) messageClazz);
                codecList.add(new ProtoMessageCodec(messageClazz, parser));
                continue;
            }

            // protoBufEnum
            if (ProtocolMessageEnum.class.isAssignableFrom(messageClazz)) {
                final Internal.EnumLiteMap<?> mapper = ProtoUtils.findMapper((Class<? extends ProtocolMessageEnum>) messageClazz);
                codecList.add(new ProtoEnumCodec(messageClazz, mapper));
                continue;
            }

            // 带有DBEntity和SerializableClass注解的所有类,和手写Serializer的类
            final Class<? extends PojoCodecImpl<?>> serializerClass = CodecScanner.getCodecClass(messageClazz);
            if (serializerClass != null) {
                final PojoCodecImpl<?> codec = createCodecInstance(serializerClass);
                codecList.add(new CustomPojoCodec(codec));
                continue;
            }

            throw new IllegalArgumentException("Unsupported class " + messageClazz.getName());
        }

        final CodecRegistry codecRegistry = CodecRegistrys.fromAppPojoCodecs(typeModelMapper, codecList);
        return new BinarySerializer(codecRegistry);
    } catch (Exception e) {
        return ExceptionUtils.rethrow(e);
    }
}
 
Example #17
Source File: ObjectReaderImp.java    From fastjgame with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T readMessage(Parser<T> parser) throws Exception {
    final BinaryTag tag = inputStream.readTag();
    if (tag == BinaryTag.NULL) {
        return null;
    }
    checkTag(tag, BinaryTag.POJO);

    @SuppressWarnings("unchecked") final T message = (T) PojoCodec.readPojoImp(inputStream, codecRegistry);
    return message;
}
 
Example #18
Source File: ProtoBufSerializationProvider.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Override
public <X> StreamingDeserializer<X> getDeserializer(final Class<X> classToDeSerialize) {
    if (targetClass != classToDeSerialize) {
        throw new SerializationException("Unknown class to deserialize: " + classToDeSerialize.getName());
    }
    @SuppressWarnings("unchecked")
    Parser<X> parser = (Parser<X>) this.parser;
    return new ProtoDeserializer<>(parser, messageEncoding);
}
 
Example #19
Source File: ProtobufSerializationProvider.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> StreamingDeserializer<T> getDeserializer(final Class<T> classToDeSerialize) {
    requireMessageLite(classToDeSerialize);
    Parser<T> parser = parsers.computeIfAbsent(classToDeSerialize, parserForClass::apply);
    return new ProtobufDeserializer<>(parser);
}
 
Example #20
Source File: NoticeFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends GeneratedMessage> T parseNotice(ByteString payload, Class<T> noticeClass) {
    try {
        Parser<T> parser = (Parser<T>) MessageConstants.MESSAGE_CLASS_TO_PARSER.get(noticeClass);
        return parser.parseFrom(payload);
    } catch (InvalidProtocolBufferException ex) {
        throw new CJCommunicationsException(ex);
    }
}
 
Example #21
Source File: DynamicProtoCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Get the memoized {@link Parser}, possibly initializing it lazily. */
@Override
protected Parser<DynamicMessage> getParser() {
  if (memoizedParser == null) {
    DynamicMessage protoMessageInstance =
        DynamicMessage.newBuilder(domain.getDescriptor(messageName)).build();
    memoizedParser = protoMessageInstance.getParserForType();
  }
  return memoizedParser;
}
 
Example #22
Source File: ProtoListUtil.java    From OpenYOLO-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a list of protos, using the provided parser, from the provided {@link ByteString}.
 * @throws IOException if the proto list could not be parsed.
 */
public static <T extends MessageLite> List<T> readMessageList(
        ByteString bytes,
        Parser<T> parser)
        throws IOException {
    InputStream stream = bytes.newInput();
    return readMessageList(stream, parser);
}
 
Example #23
Source File: ProtoListUtil.java    From OpenYOLO-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a list of protos, using the provided parser, from the provided byte array.
 * @throws IOException if the proto list could not be parsed.
 */
public static <T extends MessageLite> List<T> readMessageList(
        byte[] bytes,
        Parser<T> parser)
        throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    return readMessageList(bais, parser);
}
 
Example #24
Source File: ProtoListUtil.java    From OpenYOLO-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a list of protos, using the provided parser, from the provided input stream.
 * @throws IOException if the proto list could not be parsed.
 */
public static <T extends MessageLite> List<T> readMessageList(
        InputStream stream,
        Parser<T> parser)
        throws IOException {
    DataInputStream dis = new DataInputStream(stream);
    int messageCount = dis.readInt();

    ArrayList<T> messages = new ArrayList<>(messageCount);
    for (int i = 0; i < messageCount; i++) {
        messages.add(parser.parseDelimitedFrom(stream));
    }

    return messages;
}
 
Example #25
Source File: TypeSpecificMarshaller.java    From curiostack with MIT License 5 votes vote down vote up
/**
 * Serialize to JSON the message encoded in binary protobuf format in {@code encodedMessage}. Used
 * to write the content of type wrappers in {@link com.google.protobuf.Any}.
 */
void writeValue(ByteString encodedMessage, JsonGenerator gen) throws IOException {
  // getParserForTYpe for T returns Parser<T>
  @SuppressWarnings("unchecked")
  Parser<T> parser = (Parser<T>) prototype.getParserForType();
  writeValue(parser.parseFrom(encodedMessage), gen);
}
 
Example #26
Source File: TypeSpecificMarshaller.java    From curiostack with MIT License 5 votes vote down vote up
/**
 * Serialize to JSON the message encoded in binary protobuf format in {@code encodedMessage}
 * without starting or ending a new JSON object. Used to write the content of normal messages in
 * {@link com.google.protobuf.Any}, which will take care of creating the JSON object to store the
 * type url.
 */
void doWrite(ByteString encodedMessage, JsonGenerator gen) throws IOException {
  // getParserForTYpe for T returns Parser<T>
  @SuppressWarnings("unchecked")
  Parser<T> parser = (Parser<T>) prototype.getParserForType();
  doWrite(parser.parseFrom(encodedMessage), gen);
}
 
Example #27
Source File: UserRPCServer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private <M extends Message> M parse(byte[] body, Parser<M> parser, Class<M> type) throws RpcException {

      try {
        return parser.parseFrom(body);
      } catch (InvalidProtocolBufferException e) {
        throw  new RpcException(String.format("Failure while decoding %s body.", type.getSimpleName()), e);
      }
    }
 
Example #28
Source File: DelegateCASMap.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public DelegateCASMap(
    ContentAddressableStorage contentAddressableStorage,
    Parser<V> parser,
    DigestUtil digestUtil) {
  this.contentAddressableStorage = contentAddressableStorage;
  this.parser = parser;
  this.digestUtil = digestUtil;
}
 
Example #29
Source File: RpcBus.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static <T> T get(byte[] pBody, Parser<T> parser) throws RpcException {
  try {
    return parser.parseFrom(pBody);
  } catch (InvalidProtocolBufferException e) {
    throw new RpcException(String.format("Failure while decoding message with parser of type. %s", parser.getClass().getCanonicalName()), e);
  }
}
 
Example #30
Source File: RpcBus.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static <T> T get(ByteString pBody, Parser<T> parser) throws RpcException {
  try {
    return parser.parseFrom(pBody);
  } catch (InvalidProtocolBufferException e) {
    throw new RpcException(String.format("Failure while decoding message with parser of type. %s", parser.getClass().getCanonicalName()), e);
  }
}