com.google.protobuf.UnsafeByteOperations#unsafeWrap ( )源码实例Demo

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

源代码1 项目: calcite-avatica   文件: ProtobufTranslationImpl.java
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);
}
 
源代码2 项目: calcite-avatica   文件: ProtobufTranslationImpl.java
@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;
  }
}
 
源代码3 项目: calcite-avatica   文件: ProtobufTranslationImpl.java
@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;
  }
}
 
源代码4 项目: etcd-java   文件: KeyUtils.java
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);
}
 
源代码5 项目: etcd-java   文件: KeyUtils.java
public static ByteString singleByte(int b) {
    return UnsafeByteOperations.unsafeWrap(new byte[] { (byte) b });
}
 
源代码6 项目: calcite-avatica   文件: Service.java
public RpcMetadataResponse(@JsonProperty("serverAddress") String serverAddress) {
  this.serverAddress = serverAddress;
  this.serverAddressAsBytes = UnsafeByteOperations.unsafeWrap(serverAddress.getBytes(UTF_8));
}
 
源代码7 项目: calcite-avatica   文件: ProtobufTranslationImpl.java
private static ByteString wrapClassName(Class<?> clz) {
  return UnsafeByteOperations.unsafeWrap(clz.getName().getBytes(UTF_8));
}