@@ -100,9 +100,10 @@ private BundleElement readNextElement() throws IOException, JSONException {
100
100
return null ;
101
101
}
102
102
103
- ReadJsonResult result = readJson (Integer .parseInt (lengthPrefix ));
104
- bytesRead += lengthPrefix .getBytes (charset ).length + result .getByteCount ();
105
- return decodeBundleElement (result .getJson ());
103
+ int jsonStringByteCount = Integer .parseInt (lengthPrefix );
104
+ String json = readJson (jsonStringByteCount );
105
+ bytesRead += lengthPrefix .getBytes (charset ).length + jsonStringByteCount ;
106
+ return decodeBundleElement (json );
106
107
}
107
108
108
109
/**
@@ -176,26 +177,21 @@ int getByteCount() {
176
177
*
177
178
* <p>Returns an object containing the Json string and its UTF8 byte count.
178
179
*/
179
- private ReadJsonResult readJson (int length ) throws IOException {
180
- StringBuilder json = new StringBuilder (length );
180
+ private String readJson (int bytesToRead ) throws IOException {
181
+ StringBuilder json = new StringBuilder ();
181
182
182
- int remaining = length ;
183
- int bytesRead = 0 ;
184
- while (remaining > 0 ) {
185
- if (buffer .remaining () == 0 && !pullMoreData ()) {
183
+ while (buffer .remaining () < bytesToRead ) {
184
+ if (!pullMoreData ()) {
186
185
throw abort ("Reached the end of bundle when more data was expected." );
187
186
}
188
-
189
- int read = Math .min (remaining , buffer .remaining ());
190
- byte [] bytes = new byte [read ];
191
- buffer .get (bytes );
192
- json .append (charset .decode (ByteBuffer .wrap (bytes )));
193
-
194
- bytesRead += read ;
195
- remaining -= read ;
196
187
}
197
188
198
- return new ReadJsonResult (json .toString (), bytesRead );
189
+ // By now `buffer` has enough content for the desired JSON string.
190
+ byte [] bytes = new byte [bytesToRead ];
191
+ buffer .get (bytes );
192
+ json .append (charset .decode (ByteBuffer .wrap (bytes )));
193
+
194
+ return json .toString ();
199
195
}
200
196
201
197
/**
0 commit comments