diff --git a/zipkin2/src/main/java/zipkin2/codec/Encoding.java b/zipkin2/src/main/java/zipkin2/codec/Encoding.java index 5f5fa41d323..f4cd8954181 100644 --- a/zipkin2/src/main/java/zipkin2/codec/Encoding.java +++ b/zipkin2/src/main/java/zipkin2/codec/Encoding.java @@ -1,5 +1,5 @@ /** - * Copyright 2015-2017 The OpenZipkin Authors + * Copyright 2015-2018 The OpenZipkin Authors * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -31,6 +31,45 @@ public enum Encoding { } return sizeInBytes; } + }, + /** + * Repeated (type 2) fields are length-prefixed, the value is a concatenation with no additional + * overhead. + * + *
See https://developers.google.com/protocol-buffers/docs/encoding#optional
+ */
+ PROTO3 {
+ /** Returns the size of a length-prefixed field in a protobuf message */
+ @Override public int listSizeInBytes(int encodedSizeInBytes) {
+ return 1 // assumes field number <= 127
+ + unsignedVarint(encodedSizeInBytes) // bytes to encode the length
+ + encodedSizeInBytes; // the actual length
+ }
+
+ /** Returns a concatenation of length-prefixed size for each value */
+ @Override public int listSizeInBytes(List See https://developers.google.com/protocol-buffers/docs/encoding#varints
+ *
+ * This logic is the same as {@code com.squareup.wire.WireOutput.varint32Size} v2.3.0
+ */
+ int unsignedVarint(int value) {
+ if ((value & (0xffffffff << 7)) == 0) return 1;
+ if ((value & (0xffffffff << 14)) == 0) return 2;
+ if ((value & (0xffffffff << 21)) == 0) return 3;
+ if ((value & (0xffffffff << 28)) == 0) return 4;
+ return 5;
+ }
};
/** Like {@link #listSizeInBytes(List)}, except for a single element. */
diff --git a/zipkin2/src/test/java/zipkin2/codec/EncodingTest.java b/zipkin2/src/test/java/zipkin2/codec/EncodingTest.java
index 9c231989340..e221f40aa57 100644
--- a/zipkin2/src/test/java/zipkin2/codec/EncodingTest.java
+++ b/zipkin2/src/test/java/zipkin2/codec/EncodingTest.java
@@ -1,5 +1,5 @@
/**
- * Copyright 2015-2017 The OpenZipkin Authors
+ * Copyright 2015-2018 The OpenZipkin Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
@@ -13,7 +13,6 @@
*/
package zipkin2.codec;
-import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
@@ -22,13 +21,13 @@
public class EncodingTest {
- @Test public void emptyList_json() throws IOException {
+ @Test public void emptyList_json() {
List