Skip to content

Commit 87d73f2

Browse files
committed
More fixes
1 parent c7e5cd6 commit 87d73f2

File tree

1 file changed

+12
-9
lines changed
  • firebase-firestore/src/main/java/com/google/firebase/firestore/bundle

1 file changed

+12
-9
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import androidx.annotation.Nullable;
1818
import com.google.firebase.firestore.util.Logger;
19+
import java.io.ByteArrayOutputStream;
1920
import java.io.IOException;
2021
import java.io.InputStream;
2122
import java.nio.ByteBuffer;
@@ -178,20 +179,22 @@ int getByteCount() {
178179
* <p>Returns an object containing the Json string and its UTF8 byte count.
179180
*/
180181
private String readJson(int bytesToRead) throws IOException {
181-
StringBuilder json = new StringBuilder();
182+
ByteArrayOutputStream jsonBytes = new ByteArrayOutputStream();
182183

183-
while (buffer.remaining() < bytesToRead) {
184-
if (!pullMoreData()) {
184+
int remaining = bytesToRead;
185+
while (remaining > 0) {
186+
if (buffer.remaining() == 0 && !pullMoreData()) {
185187
throw abort("Reached the end of bundle when more data was expected.");
186188
}
187-
}
188189

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)));
190+
int read = Math.min(remaining, buffer.remaining());
191+
jsonBytes.write(buffer.slice().array(), 0, read);
192+
buffer.position(buffer.position() + read);
193+
194+
remaining -= read;
195+
}
193196

194-
return json.toString();
197+
return jsonBytes.toString(charset.name());
195198
}
196199

197200
/**

0 commit comments

Comments
 (0)