|
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 |
| -import java.io.ByteArrayOutputStream; |
| 20 | +import com.google.protobuf.CodedOutputStream; |
| 21 | + |
21 | 22 | import java.io.IOException;
|
22 | 23 | import java.io.OutputStream;
|
23 | 24 | import java.util.stream.StreamSupport;
|
|
29 | 30 | */
|
30 | 31 | public class StreamedProtoOutputFormatter extends ProtoOutputFormatter {
|
31 | 32 |
|
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 |
| - |
44 | 33 | @Override
|
45 | 34 | public String getName() {
|
46 | 35 | return "streamed_proto";
|
@@ -79,23 +68,23 @@ private Build.Target toProto(Target target) {
|
79 | 68 | }
|
80 | 69 | }
|
81 | 70 |
|
82 |
| - private synchronized void writeToOutputStreamThreadSafe(ByteArrayOutputStream bout) { |
| 71 | + private synchronized void writeToOutputStreamThreadSafe(byte[] data) { |
83 | 72 | try {
|
84 |
| - bout.writeTo(out); |
| 73 | + out.write(data); |
85 | 74 | } catch (IOException e) {
|
86 | 75 | throw new WrappedIOException(e);
|
87 | 76 | }
|
88 | 77 | }
|
89 | 78 | };
|
90 | 79 | }
|
91 | 80 |
|
92 |
| - private static ByteArrayOutputStream writeDelimited(Build.Target targetProtoBuffer) { |
| 81 | + private static byte[] writeDelimited(Build.Target targetProtoBuffer) { |
93 | 82 | try {
|
94 |
| - var bout = |
95 |
| - new ByteArrayOutputStream( |
96 |
| - targetProtoBuffer.getSerializedSize() + MAX_BYTES_FOR_VARINT32_ENCODING); |
97 |
| - targetProtoBuffer.writeDelimitedTo(bout); |
98 |
| - return bout; |
| 83 | + var serializedSize = targetProtoBuffer.getSerializedSize(); |
| 84 | + var headerSize = CodedOutputStream.computeUInt32SizeNoTag(serializedSize); |
| 85 | + var output = new byte[headerSize + serializedSize]; |
| 86 | + targetProtoBuffer.writeTo(CodedOutputStream.newInstance(output, headerSize, output.length - headerSize)); |
| 87 | + return output; |
99 | 88 | } catch (IOException e) {
|
100 | 89 | throw new WrappedIOException(e);
|
101 | 90 | }
|
|
0 commit comments