|
16 | 16 |
|
17 | 17 | import androidx.annotation.Nullable;
|
18 | 18 | import com.google.firebase.firestore.util.Logger;
|
| 19 | +import java.io.ByteArrayOutputStream; |
19 | 20 | import java.io.IOException;
|
20 | 21 | import java.io.InputStream;
|
21 | 22 | import java.nio.ByteBuffer;
|
@@ -178,20 +179,22 @@ int getByteCount() {
|
178 | 179 | * <p>Returns an object containing the Json string and its UTF8 byte count.
|
179 | 180 | */
|
180 | 181 | private String readJson(int bytesToRead) throws IOException {
|
181 |
| - StringBuilder json = new StringBuilder(); |
| 182 | + ByteArrayOutputStream jsonBytes = new ByteArrayOutputStream(); |
182 | 183 |
|
183 |
| - while (buffer.remaining() < bytesToRead) { |
184 |
| - if (!pullMoreData()) { |
| 184 | + int remaining = bytesToRead; |
| 185 | + while (remaining > 0) { |
| 186 | + if (buffer.remaining() == 0 && !pullMoreData()) { |
185 | 187 | throw abort("Reached the end of bundle when more data was expected.");
|
186 | 188 | }
|
187 |
| - } |
188 | 189 |
|
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 | + } |
193 | 196 |
|
194 |
| - return json.toString(); |
| 197 | + return jsonBytes.toString(charset.name()); |
195 | 198 | }
|
196 | 199 |
|
197 | 200 | /**
|
|
0 commit comments