Skip to content

Commit 913d4a3

Browse files
committed
address jin feedback
1 parent 0c9a1d2 commit 913d4a3

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.google.devtools.build.lib.packages.Target;
1818
import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
1919
import com.google.devtools.build.lib.query2.proto.proto2api.Build;
20-
2120
import java.io.ByteArrayOutputStream;
2221
import java.io.IOException;
2322
import java.io.OutputStream;
@@ -29,6 +28,19 @@
2928
* on a {@code Build.QueryResult} object the full result can be reconstructed.
3029
*/
3130
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+
3244
@Override
3345
public String getName() {
3446
return "streamed_proto";
@@ -75,7 +87,9 @@ private synchronized void writeToOutputStreamThreadSafe(ByteArrayOutputStream bo
7587

7688
private static ByteArrayOutputStream writeDelimited(Build.Target targetProtoBuffer) {
7789
try {
78-
var bout = new ByteArrayOutputStream(targetProtoBuffer.getSerializedSize() + 10);
90+
var bout =
91+
new ByteArrayOutputStream(
92+
targetProtoBuffer.getSerializedSize() + MAX_BYTES_FOR_VARINT32_ENCODING);
7993
targetProtoBuffer.writeDelimitedTo(bout);
8094
return bout;
8195
} catch (IOException e) {

0 commit comments

Comments
 (0)