Skip to content

Commit c7e5cd6

Browse files
committed
Fix a potential bug.
1 parent 266ec18 commit c7e5cd6

File tree

1 file changed

+14
-18
lines changed
  • firebase-firestore/src/main/java/com/google/firebase/firestore/bundle

1 file changed

+14
-18
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/bundle/BundleReader.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ private BundleElement readNextElement() throws IOException, JSONException {
100100
return null;
101101
}
102102

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);
106107
}
107108

108109
/**
@@ -176,26 +177,21 @@ int getByteCount() {
176177
*
177178
* <p>Returns an object containing the Json string and its UTF8 byte count.
178179
*/
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();
181182

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()) {
186185
throw abort("Reached the end of bundle when more data was expected.");
187186
}
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;
196187
}
197188

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();
199195
}
200196

201197
/**

0 commit comments

Comments
 (0)