|
17 | 17 | import com.google.devtools.build.lib.packages.Target;
|
18 | 18 | import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
|
19 | 19 | import com.google.devtools.build.lib.query2.proto.proto2api.Build;
|
20 |
| - |
21 | 20 | import java.io.ByteArrayOutputStream;
|
22 | 21 | import java.io.IOException;
|
23 | 22 | import java.io.OutputStream;
|
|
29 | 28 | * on a {@code Build.QueryResult} object the full result can be reconstructed.
|
30 | 29 | */
|
31 | 30 | public class StreamedProtoOutputFormatter extends ProtoOutputFormatter {
|
| 31 | + |
| 32 | + /** |
| 33 | + * The most bytes that protobuf delimited proto format will prepend to a proto message. See <a |
| 34 | + * href="https://github.com/protocolbuffers/protobuf/blob/c11033dc27c3e9c1913e45b62fb5d4c5b5644b3e/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java#L72"> |
| 35 | + * <code>writeDelimitedTo</code></a> and <a |
| 36 | + * href="https://github.com/protocolbuffers/protobuf/blob/c11033dc27c3e9c1913e45b62fb5d4c5b5644b3e/java/core/src/main/java/com/google/protobuf/WireFormat.java#L28"> |
| 37 | + * <code>MAX_VARINT32_SIZE</code></a>. |
| 38 | + * |
| 39 | + * <p>The value for int32 (used by {@code writeDelimitedTo} is actually 5, but we pick 10 just to |
| 40 | + * be safe. |
| 41 | + */ |
| 42 | + private static final int MAX_BYTES_FOR_VARINT32_ENCODING = 10; |
| 43 | + |
32 | 44 | @Override
|
33 | 45 | public String getName() {
|
34 | 46 | return "streamed_proto";
|
@@ -75,7 +87,9 @@ private synchronized void writeToOutputStreamThreadSafe(ByteArrayOutputStream bo
|
75 | 87 |
|
76 | 88 | private static ByteArrayOutputStream writeDelimited(Build.Target targetProtoBuffer) {
|
77 | 89 | try {
|
78 |
| - var bout = new ByteArrayOutputStream(targetProtoBuffer.getSerializedSize() + 10); |
| 90 | + var bout = |
| 91 | + new ByteArrayOutputStream( |
| 92 | + targetProtoBuffer.getSerializedSize() + MAX_BYTES_FOR_VARINT32_ENCODING); |
79 | 93 | targetProtoBuffer.writeDelimitedTo(bout);
|
80 | 94 | return bout;
|
81 | 95 | } catch (IOException e) {
|
|
0 commit comments