@@ -93,18 +93,20 @@ public static Optional<AggregationMessage> deserialize(final ByteString data) {
93
93
try {
94
94
switch (type ) {
95
95
case 0x01 :
96
- return Optional .of (new AggregationMessage (Messages .HostIdentification .parseFrom (payloadBytes )));
96
+ return Optional .of (new AggregationMessage (Messages .HostIdentification .parseFrom (payloadBytes ), length ));
97
97
case 0x03 :
98
- return Optional .of (new AggregationMessage (Messages .HeartbeatRecord .parseFrom (payloadBytes )));
98
+ return Optional .of (new AggregationMessage (Messages .HeartbeatRecord .parseFrom (payloadBytes ), length ));
99
99
case 0x04 :
100
- return Optional .of (new AggregationMessage (Messages .StatisticSetRecord .parseFrom (payloadBytes )));
100
+ return Optional .of (new AggregationMessage (Messages .StatisticSetRecord .parseFrom (payloadBytes ), length ));
101
101
case 0x05 :
102
102
// 0x05 is the message type for all supporting data
103
103
switch (subType ) {
104
104
case 0x01 :
105
- return Optional .of (new AggregationMessage (Messages .SamplesSupportingData .parseFrom (payloadBytes )));
105
+ return Optional .of (new AggregationMessage (Messages .SamplesSupportingData .parseFrom (payloadBytes ), length ));
106
106
case 0x02 :
107
- return Optional .of (new AggregationMessage (Messages .SparseHistogramSupportingData .parseFrom (payloadBytes )));
107
+ return Optional .of (new AggregationMessage (
108
+ Messages .SparseHistogramSupportingData .parseFrom (payloadBytes ),
109
+ length ));
108
110
default :
109
111
LOGGER .warn (
110
112
String .format ("Invalid protocol buffer, unknown subtype; type=%s, subtype=%s, bytes=%s" ,
@@ -134,24 +136,28 @@ private static boolean typeHasSubtype(final byte type) {
134
136
* @return <code>Buffer</code> containing serialized message.
135
137
*/
136
138
public ByteString serialize () {
139
+ return AggregationMessage .serialize (_message );
140
+ }
141
+
142
+ private static ByteString serialize (final GeneratedMessageV3 message ) {
137
143
final ByteStringBuilder b = ByteString .createBuilder ();
138
- if (_message instanceof Messages .HostIdentification ) {
144
+ if (message instanceof Messages .HostIdentification ) {
139
145
b .putByte ((byte ) 0x01 );
140
- } else if (_message instanceof Messages .HeartbeatRecord ) {
146
+ } else if (message instanceof Messages .HeartbeatRecord ) {
141
147
b .putByte ((byte ) 0x03 );
142
- } else if (_message instanceof Messages .StatisticSetRecord ) {
148
+ } else if (message instanceof Messages .StatisticSetRecord ) {
143
149
b .putByte ((byte ) 0x04 );
144
- } else if (_message instanceof Messages .SamplesSupportingData ) {
150
+ } else if (message instanceof Messages .SamplesSupportingData ) {
145
151
b .putByte ((byte ) 0x05 );
146
152
b .putByte ((byte ) 0x01 );
147
- } else if (_message instanceof Messages .SparseHistogramSupportingData ) {
153
+ } else if (message instanceof Messages .SparseHistogramSupportingData ) {
148
154
b .putByte ((byte ) 0x05 );
149
155
b .putByte ((byte ) 0x02 );
150
156
} else {
151
- throw new IllegalArgumentException (String .format ("Unsupported message; message=%s" , _message ));
157
+ throw new IllegalArgumentException (String .format ("Unsupported message; message=%s" , message ));
152
158
}
153
159
try {
154
- _message .writeTo (b .asOutputStream ());
160
+ message .writeTo (b .asOutputStream ());
155
161
} catch (final IOException e ) {
156
162
throw new RuntimeException (e );
157
163
}
@@ -165,14 +171,20 @@ public GeneratedMessageV3 getMessage() {
165
171
}
166
172
167
173
public int getLength () {
168
- return _message . getSerializedSize () + HEADER_SIZE_IN_BYTES ;
174
+ return _length ;
169
175
}
170
176
171
177
private AggregationMessage (final GeneratedMessageV3 message ) {
178
+ this (message , serialize (message ).length ());
179
+ }
180
+
181
+ private AggregationMessage (final GeneratedMessageV3 message , final int length ) {
182
+ _length = length ;
172
183
_message = message ;
173
184
}
174
185
175
186
private final GeneratedMessageV3 _message ;
187
+ private final int _length ;
176
188
177
189
private static final int BYTE_SIZE_IN_BYTES = 1 ;
178
190
private static final int INTEGER_SIZE_IN_BYTES = Integer .SIZE / 8 ;
0 commit comments