下面列出了com.google.protobuf.CodedInputStream# enableAliasing ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** 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});
}
/** 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});
}
@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;
}
}
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);
}
}
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;
}
@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);
}
}
/**
* 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
* — 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);
}