Java Code Examples for com.google.protobuf.ByteString#newCodedInput()

The following examples show how to use com.google.protobuf.ByteString#newCodedInput() . 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: ProtobufTranslationImpl.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@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;
  }
}
 
Example 2
Source File: ProtobufTranslationImpl.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@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;
  }
}
 
Example 3
Source File: BuildOptions.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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;
}