@@ -113,12 +113,25 @@ private Build.Target toProto(Target target) {
113
113
private static byte [] writeDelimited (Build .Target targetProtoBuffer ) {
114
114
try {
115
115
var serializedSize = targetProtoBuffer .getSerializedSize ();
116
- var headerSize = CodedOutputStream .computeUInt32SizeNoTag (serializedSize );
117
- var output = new byte [headerSize + serializedSize ];
118
- var codedOut = CodedOutputStream .newInstance (output , headerSize , output .length - headerSize );
119
- targetProtoBuffer .writeTo (codedOut );
120
- codedOut .flush ();
116
+ - var headerSize = CodedOutputStream .computeUInt32SizeNoTag (serializedSize );
117
+ - var output = new byte [headerSize + serializedSize ];
118
+ - var codedOut = CodedOutputStream .newInstance (output , headerSize , output .length - headerSize );
119
+ - targetProtoBuffer .writeTo (codedOut );
120
+ + int headerSize = CodedOutputStream .computeUInt32SizeNoTag (serializedSize );
121
+ + byte [] output = new byte [headerSize + serializedSize ];
122
+ +
123
+ + // 1. write the var-int length prefix
124
+ + CodedOutputStream headerOut = CodedOutputStream .newInstance (output , 0 , headerSize );
125
+ + headerOut .writeUInt32NoTag (serializedSize );
126
+ + headerOut .flush ();
127
+ +
128
+ + // 2. write the message bytes immediately after the prefix
129
+ + CodedOutputStream bodyOut =
130
+ + CodedOutputStream .newInstance (output , headerSize , serializedSize );
131
+ + targetProtoBuffer .writeTo (bodyOut );
132
+ + bodyOut .flush ();
121
133
return output ;
134
+ }
122
135
} catch (IOException e ) {
123
136
throw new WrappedIOException (e );
124
137
}
0 commit comments