下面列出了com.google.protobuf.UnsafeByteOperations#unsafeWrap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
@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;
}
}
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);
}
public static ByteString singleByte(int b) {
return UnsafeByteOperations.unsafeWrap(new byte[] { (byte) b });
}
public RpcMetadataResponse(@JsonProperty("serverAddress") String serverAddress) {
this.serverAddress = serverAddress;
this.serverAddressAsBytes = UnsafeByteOperations.unsafeWrap(serverAddress.getBytes(UTF_8));
}
private static ByteString wrapClassName(Class<?> clz) {
return UnsafeByteOperations.unsafeWrap(clz.getName().getBytes(UTF_8));
}