com.google.protobuf.ByteString#newCodedInput ( )源码实例Demo

下面列出了com.google.protobuf.ByteString#newCodedInput ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


@Override public Request parseRequest(byte[] bytes) throws IOException {
  ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes);
  CodedInputStream inputStream = byteString.newCodedInput();
  // Enable aliasing to avoid an extra copy to get at the serialized Request inside of the
  // WireMessage.
  inputStream.enableAliasing(true);
  WireMessage wireMsg = WireMessage.parseFrom(inputStream);

  String serializedMessageClassName = wireMsg.getName();

  try {
    RequestTranslator translator = getParserForRequest(serializedMessageClassName);

    // The ByteString should be logical offsets into the original byte array
    return translator.transform(wireMsg.getWrappedMessage());
  } catch (RuntimeException e) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("Failed to parse request message '{}'", TextFormat.shortDebugString(wireMsg));
    }
    throw e;
  }
}
 

@Override public Response parseResponse(byte[] bytes) throws IOException {
  ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes);
  CodedInputStream inputStream = byteString.newCodedInput();
  // Enable aliasing to avoid an extra copy to get at the serialized Response inside of the
  // WireMessage.
  inputStream.enableAliasing(true);
  WireMessage wireMsg = WireMessage.parseFrom(inputStream);

  String serializedMessageClassName = wireMsg.getName();
  try {
    ResponseTranslator translator = getParserForResponse(serializedMessageClassName);

    return translator.transform(wireMsg.getWrappedMessage());
  } catch (RuntimeException e) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("Failed to parse response message '{}'", TextFormat.shortDebugString(wireMsg));
    }
    throw e;
  }
}
 
源代码3 项目: bazel   文件: BuildOptions.java

@Override
public OptionsDiffForReconstruction deserialize(
    DeserializationContext context, CodedInputStream codedIn)
    throws SerializationException, IOException {
  OptionsDiffCache cache = context.getDependency(OptionsDiffCache.class);
  ByteString bytes = codedIn.readBytes();
  OptionsDiffForReconstruction diff = cache.getOptionsDiffFromBytes(bytes);
  if (diff == null) {
    CodedInputStream codedInput = bytes.newCodedInput();
    context = context.getNewNonMemoizingContext();
    Map<Class<? extends FragmentOptions>, Map<String, Object>> differingOptions =
        context.deserialize(codedInput);
    ImmutableSet<Class<? extends FragmentOptions>> extraFirstFragmentClasses =
        context.deserialize(codedInput);
    ImmutableList<FragmentOptions> extraSecondFragments = context.deserialize(codedInput);
    byte[] baseFingerprint = codedInput.readByteArray();
    String checksum = context.deserialize(codedInput);
    Map<Label, Object> differingStarlarkOptions = context.deserialize(codedInput);
    List<Label> extraFirstStarlarkOptions = context.deserialize(codedInput);
    Map<Label, Object> extraSecondStarlarkOptions = context.deserialize(codedInput);
    diff =
        new OptionsDiffForReconstruction(
            differingOptions,
            extraFirstFragmentClasses,
            extraSecondFragments,
            baseFingerprint,
            checksum,
            differingStarlarkOptions,
            extraFirstStarlarkOptions,
            extraSecondStarlarkOptions,
            /*original=*/ null);
    cache.putBytesFromOptionsDiff(diff, bytes);
  }
  return diff;
}