Java Code Examples for com.google.protobuf.CodedInputStream#enableAliasing()

The following examples show how to use com.google.protobuf.CodedInputStream#enableAliasing() . 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: StateTest.java    From zetasketch with Apache License 2.0 6 votes vote down vote up
/** Verify that the data is aliased when possible. */
@Test
@SuppressWarnings("boxing")
public void parseData_AliasesArray() throws IOException {
  byte[] bytes = build(HyperLogLogPlusUniqueStateProto.newBuilder()
      .setData(ByteString.copyFrom(new byte[] {1, 2, 3})));
  CodedInputStream stream = CodedInputStream.newInstance(bytes);
  stream.enableAliasing(true);
  State state = new State();
  state.parse(stream);

  // Preconditions.
  int idx = Bytes.indexOf(bytes, new byte[] {1, 2, 3});
  assertThat(idx).isAtLeast(0);
  assertThat(state.data.toByteArray()).isEqualTo(new byte[] {1, 2, 3});

  // Modify the bytes, make sure that it is reflected in builder.
  bytes[idx + 1] = 4;
  assertThat(state.data.toByteArray()).isEqualTo(new byte[] {1, 4, 3});
}
 
Example 2
Source File: StateTest.java    From zetasketch with Apache License 2.0 6 votes vote down vote up
/** Verify that the data is aliased when possible. */
@Test
@SuppressWarnings("boxing")
public void parseSparseData_AliasesArray() throws IOException {
  byte[] bytes = build(HyperLogLogPlusUniqueStateProto.newBuilder()
      .setSparseData(ByteString.copyFrom(new byte[] {1, 2, 3})));
  CodedInputStream stream = CodedInputStream.newInstance(bytes);
  stream.enableAliasing(true);
  State state = new State();
  state.parse(stream);

  // Preconditions.
  int idx = Bytes.indexOf(bytes, new byte[] {1, 2, 3});
  assertThat(idx).isAtLeast(0);
  assertThat(state.sparseData.toByteArray()).isEqualTo(new byte[] {1, 2, 3});

  // Modify the bytes, make sure that it is reflected in builder.
  bytes[idx + 1] = 4;
  assertThat(state.sparseData.toByteArray()).isEqualTo(new byte[] {1, 4, 3});
}
 
Example 3
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 4
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 5
Source File: HyperLogLogPlusPlus.java    From zetasketch with Apache License 2.0 5 votes vote down vote up
private static HyperLogLogPlusPlus<?> forProto(CodedInputStream proto) {
  try {
    // Enable aliasing if possible to avoid unnecessary data copies. This is safe since the
    // ByteSlice used in the State employs copy-on-write.
    State state = new State();
    proto.enableAliasing(true);
    state.parse(proto);
    return new HyperLogLogPlusPlus<>(state);
  } catch (IOException e) {
    throw new IllegalArgumentException(e);
  }
}
 
Example 6
Source File: ProtoBufSerializationProvider.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private static CodedInputStream newCodedInputStream(final ByteBuffer[] buffers, final int lengthOfData) {
    // Because we allocated a new internal ByteBuffer that will never be mutated we may just wrap it and
    // enable aliasing to avoid an extra copying inside parser for a deserialized message.
    final CodedInputStream in = unsafeWrap(mergeByteBuffers(buffers, lengthOfData)).newCodedInput();
    in.enableAliasing(true);
    return in;
}
 
Example 7
Source File: MessageLiteCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public MessageLite deserialize(DeserializationContext unusedContext, CodedInputStream codedIn)
    throws IOException, SerializationException {
  // Don't hold on to full byte array when constructing this proto.
  codedIn.enableAliasing(false);
  try {
    MessageLite.Builder builder = builderSupplier.get();
    codedIn.readMessage(builder, ExtensionRegistryLite.getEmptyRegistry());
    return builder.build();
  } catch (InvalidProtocolBufferException e) {
    throw new SerializationException("Failed to parse proto of type " + type, e);
  } finally {
    codedIn.enableAliasing(true);
  }
}
 
Example 8
Source File: State.java    From zetasketch with Apache License 2.0 2 votes vote down vote up
/**
 * Parses a serialized HyperLogLog++ {@link AggregatorStateProto} and populates this object's
 * fields. Note that {@link #data} and {@link #sparseData} will <em>alias</em> the given bytes
 * &mdash; sharing the same memory.
 *
 * @throws IOException If the stream does not contain a serialized {@link AggregatorStateProto} or
 *     if fields are set that would typically not belong
 */
public void parse(byte[] bytes) throws IOException {
  CodedInputStream stream = CodedInputStream.newInstance(bytes);
  stream.enableAliasing(true);
  parse(stream);
}