Java Code Examples for com.google.protobuf.UnsafeByteOperations#unsafeWrap()

The following examples show how to use com.google.protobuf.UnsafeByteOperations#unsafeWrap() . 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
void serializeMessage(OutputStream out, Message msg) throws IOException {
  // Serialize the protobuf message
  UnsynchronizedBuffer buffer = threadLocalBuffer.get();
  ByteString serializedMsg;
  try {
    msg.writeTo(buffer);
    // Make a bytestring from it
    serializedMsg = UnsafeByteOperations.unsafeWrap(buffer.toArray());
  } finally {
    buffer.reset();
  }

  // Wrap the serialized message in a WireMessage
  WireMessage wireMsg = WireMessage.newBuilder().setNameBytes(getClassNameBytes(msg.getClass()))
      .setWrappedMessage(serializedMsg).build();

  // Write the WireMessage to the provided OutputStream
  wireMsg.writeTo(out);
}
 
Example 2
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 3
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 4
Source File: KeyUtils.java    From etcd-java with Apache License 2.0 5 votes vote down vote up
public static ByteString fromHexString(CharSequence seq) {
    int len = seq.length();
    if (len == 0) {
        return ByteString.EMPTY;
    }
    if (len % 2 != 0) {
        throw new IllegalArgumentException("must be even number of chars");
    }
    int blen = len >> 1;
    byte[] bytes = new byte[blen];
    for (int i = 0, j = 0; i < blen; i ++, j += 2) {
        bytes[i] = (byte) ((digitFor(seq.charAt(j)) << 4) | digitFor(seq.charAt(j + 1)));
    }
    return UnsafeByteOperations.unsafeWrap(bytes);
}
 
Example 5
Source File: KeyUtils.java    From etcd-java with Apache License 2.0 4 votes vote down vote up
public static ByteString singleByte(int b) {
    return UnsafeByteOperations.unsafeWrap(new byte[] { (byte) b });
}
 
Example 6
Source File: Service.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
public RpcMetadataResponse(@JsonProperty("serverAddress") String serverAddress) {
  this.serverAddress = serverAddress;
  this.serverAddressAsBytes = UnsafeByteOperations.unsafeWrap(serverAddress.getBytes(UTF_8));
}
 
Example 7
Source File: ProtobufTranslationImpl.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
private static ByteString wrapClassName(Class<?> clz) {
  return UnsafeByteOperations.unsafeWrap(clz.getName().getBytes(UTF_8));
}