Skip to content

Commit f384640

Browse files
Merge branch 'mrschmidt/bundle/reader' into mrschmidt/bundle/loader
2 parents 726d4b7 + 17ba69d commit f384640

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ public class BundleReader {
3535

3636
private final BundleSerializer serializer;
3737
private final InputStreamReader dataReader;
38+
private final Charset charset = Charset.forName("UTF-8");
3839

3940
@Nullable BundleMetadata metadata;
4041
private CharBuffer buffer;
4142
long bytesRead;
4243

4344
public BundleReader(BundleSerializer serializer, InputStream data) {
4445
this.serializer = serializer;
45-
dataReader = new InputStreamReader(data, Charset.forName("UTF-8"));
46+
dataReader = new InputStreamReader(data, charset);
4647
buffer = CharBuffer.allocate(BUFFER_CAPACITY);
4748

4849
buffer.flip(); // Start the buffer in "reading mode"
@@ -101,7 +102,7 @@ private BundleElement readNextElement() throws IOException, JSONException {
101102
}
102103

103104
String json = readJsonString(Integer.parseInt(lengthPrefix));
104-
bytesRead += lengthPrefix.length() + json.length();
105+
bytesRead += lengthPrefix.length() + json.getBytes(charset).length;
105106
return decodeBundleElement(json);
106107
}
107108

@@ -159,7 +160,7 @@ private int indexOfOpenBracket() {
159160
* <p>Returns a string decoded from the read bytes.
160161
*/
161162
private String readJsonString(int length) throws IOException {
162-
StringBuilder json = new StringBuilder();
163+
StringBuilder json = new StringBuilder(length);
163164

164165
int remaining = length;
165166
while (remaining > 0) {

firebase-firestore/src/test/java/com/google/firebase/firestore/bundle/BundleReaderTest.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ public class BundleReaderTest {
113113
"bar",
114114
Value.newBuilder().setIntegerValue(42).build())),
115115
Document.DocumentState.SYNCED));
116+
public static final BundledDocumentMetadata DOC3_METADATA =
117+
new BundledDocumentMetadata(
118+
key("coll/doc3"), version(5600002L), /* exists= */ true, Collections.emptyList());
119+
public static final BundleDocument DOC3 =
120+
new BundleDocument(
121+
new Document(
122+
key("coll/doc3"),
123+
version(30004002L),
124+
ObjectValue.fromMap(
125+
map("unicodeValue", Value.newBuilder().setStringValue("\uD83D\uDE0A").build())),
126+
Document.DocumentState.SYNCED));
116127

117128
@Test
118129
public void testReadsQueryAndDocument() throws IOException, JSONException {
@@ -311,18 +322,20 @@ public void testCombinesMultipleBufferReads() throws IOException, JSONException
311322
@Test
312323
public void testReadsUnicodeData() throws IOException, JSONException {
313324
TestBundleBuilder bundleBuilder = new TestBundleBuilder(TEST_PROJECT);
325+
String docMetadata = addDoc3Metadata(bundleBuilder);
326+
String doc = addDoc3(bundleBuilder); // DOC3 contains Unicode data
314327
String json =
315-
bundleBuilder.build(
316-
"bundle-\uD83D\uDE0A", /* createTimeMicros= */ 6000000L, /* version= */ 1);
328+
bundleBuilder.build("bundle-1", /* createTimeMicros= */ 6000000L, /* version= */ 1);
317329

318330
BundleReader bundleReader =
319331
new BundleReader(
320332
SERIALIZER, new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
321333

322-
BundleMetadata expectedMetadata =
323-
new BundleMetadata("bundle-\uD83D\uDE0A", 1, version(6000000L));
324-
BundleMetadata actualMetadata = bundleReader.getBundleMetadata();
325-
assertEquals(expectedMetadata, actualMetadata);
334+
List<BundleElement> bundleElements = verifyAllElements(bundleReader, docMetadata, doc);
335+
336+
assertEquals(BUNDLE_METADATA, bundleElements.get(0));
337+
assertEquals(DOC3_METADATA, bundleElements.get(1));
338+
assertEquals(DOC3, bundleElements.get(2));
326339
}
327340

328341
private String addDeletedDocMetadata(TestBundleBuilder bundleBuilder) {
@@ -351,6 +364,19 @@ private String addDoc2(TestBundleBuilder bundleBuilder) {
351364
"{ foo: { stringValue: 'value2' }, bar: { integerValue: 42 } }");
352365
}
353366

367+
private String addDoc3Metadata(TestBundleBuilder bundleBuilder) {
368+
return bundleBuilder.addDocumentMetadata(
369+
"coll/doc3", /* readTimeMicros= */ 5600002L, /* exists= */ true);
370+
}
371+
372+
private String addDoc3(TestBundleBuilder bundleBuilder) {
373+
return bundleBuilder.addDocument(
374+
"coll/doc3",
375+
/* createTimeMicros= */ 1200002L,
376+
/* updateTimeMicros= */ 30004002L,
377+
"{ unicodeValue: { stringValue: '\uD83D\uDE0A' } }");
378+
}
379+
354380
private String addDoc2Metadata(TestBundleBuilder bundleBuilder) {
355381
return bundleBuilder.addDocumentMetadata("coll/doc1", 5600001L, true);
356382
}

0 commit comments

Comments
 (0)