Skip to content

Commit 7dfd805

Browse files
Comments
1 parent c5a0912 commit 7dfd805

File tree

7 files changed

+34
-37
lines changed

7 files changed

+34
-37
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.google.firebase.firestore.model.DocumentKey;
1919

2020
/** A document that was saved to a bundle. */
21-
public class BundleDocument extends BundleElement {
21+
public class BundleDocument implements BundleElement {
2222
private Document document;
2323

2424
public BundleDocument(Document document) {

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
import org.json.JSONException;
1818
import org.json.JSONObject;
1919

20-
public abstract class BundleElement {
20+
public interface BundleElement {
2121

22-
public static BundleElement fromJson(BundleSerializer serializer, String json)
23-
throws JSONException {
22+
static BundleElement fromJson(BundleSerializer serializer, String json) throws JSONException {
2423
JSONObject object = new JSONObject(json);
2524

2625
if (object.has("metadata")) {
@@ -35,6 +34,4 @@ public static BundleElement fromJson(BundleSerializer serializer, String json)
3534
throw new IllegalArgumentException("Cannot decode unknown Bundle element: " + json);
3635
}
3736
}
38-
39-
protected BundleElement() {}
4037
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.google.firebase.firestore.model.SnapshotVersion;
1818

1919
/** Represents a Firestore bundle saved by the SDK in its local storage. */
20-
public class BundleMetadata extends BundleElement {
20+
public class BundleMetadata implements BundleElement {
2121
private final String bundleId;
2222
private final int version;
2323
private final SnapshotVersion createTime;

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.InputStream;
2020
import java.io.InputStreamReader;
2121
import java.nio.CharBuffer;
22+
import java.nio.charset.Charset;
2223
import org.json.JSONException;
2324

2425
/**
@@ -27,7 +28,7 @@
2728
* <p>The class takes a bundle stream and presents abstractions to read bundled elements out of the
2829
* underlying content.
2930
*/
30-
public class BundleReader extends BundleElement {
31+
public class BundleReader {
3132
/** The capacity for the internal char buffer. */
3233
protected static final int BUFFER_CAPACITY = 1024;
3334

@@ -40,8 +41,10 @@ public class BundleReader extends BundleElement {
4041

4142
public BundleReader(BundleSerializer serializer, InputStream data) {
4243
this.serializer = serializer;
43-
dataReader = new InputStreamReader(data);
44+
dataReader = new InputStreamReader(data, Charset.forName("UTF-8"));
4445
buffer = CharBuffer.allocate(BUFFER_CAPACITY);
46+
47+
buffer.flip(); // Start the buffer in "reading mode"
4548
}
4649

4750
/** Returns the metadata element from the bundle. */
@@ -91,23 +94,23 @@ public void close() throws IOException {
9194
*/
9295
@Nullable
9396
private BundleElement readNextElement() throws IOException, JSONException {
94-
int length = readLength();
95-
if (length == -1) {
97+
String lengthPrefix = readLengthPrefix();
98+
if (lengthPrefix == null) {
9699
return null;
97100
}
98101

99-
String json = readJsonString(length);
100-
bytesRead += (int) (Math.log10(length) + 1) + length;
102+
String json = readJsonString(Integer.parseInt(lengthPrefix));
103+
bytesRead += lengthPrefix.length() + json.length();
101104
return BundleElement.fromJson(serializer, json);
102105
}
103106

104107
/**
105108
* Reads the length prefix from the beginning of the internal buffer until the first '{'. Returns
106109
* the integer-decoded length.
107110
*
108-
* <p>If it reached the end of the stream, returns -1.
111+
* <p>If it reached the end of the stream, returns null.
109112
*/
110-
private int readLength() throws IOException {
113+
private @Nullable String readLengthPrefix() throws IOException {
111114
int nextOpenBracket;
112115

113116
while ((nextOpenBracket = indexOfOpenBracket()) == -1) {
@@ -119,7 +122,7 @@ private int readLength() throws IOException {
119122
// We broke out of the loop because underlying stream is closed, and there happens to be no
120123
// more data to process.
121124
if (buffer.remaining() == 0) {
122-
return -1;
125+
return null;
123126
}
124127

125128
// We broke out of the loop because underlying stream is closed, but still cannot find an
@@ -130,14 +133,14 @@ private int readLength() throws IOException {
130133

131134
char[] c = new char[nextOpenBracket];
132135
buffer.get(c);
133-
return Integer.parseInt(new String(c));
136+
return new String(c);
134137
}
135138

136139
/** Returns the index of the first open bracket, or -1 if none is found. */
137140
private int indexOfOpenBracket() {
138141
buffer.mark();
139142
try {
140-
for (int i = 0; i < buffer.limit(); ++i) {
143+
for (int i = 0; i < buffer.remaining(); ++i) {
141144
if (buffer.get() == '{') {
142145
return i;
143146
}
@@ -155,20 +158,22 @@ private int indexOfOpenBracket() {
155158
* <p>Returns a string decoded from the read bytes.
156159
*/
157160
private String readJsonString(int length) throws IOException {
158-
char[] c = new char[length];
159-
160-
int read = Math.min(length, buffer.remaining());
161-
buffer.get(c, 0, read);
161+
StringBuilder json = new StringBuilder();
162162

163-
while (read < length) {
163+
int remaining = length;
164+
while (remaining > 0) {
164165
if (!pullMoreData()) {
165166
raiseError("Reached the end of bundle when more data was expected.");
166167
}
167-
int toRead = Math.min(length, buffer.remaining());
168-
buffer.get(c, read, toRead);
169-
read += toRead;
168+
169+
int read = Math.min(remaining, buffer.remaining());
170+
json.append(buffer, 0, read);
171+
buffer.position(buffer.position() + read);
172+
173+
remaining -= read;
170174
}
171-
return new String(c);
175+
176+
return json.toString();
172177
}
173178

174179
/**
@@ -179,14 +184,9 @@ private String readJsonString(int length) throws IOException {
179184
private boolean pullMoreData() throws IOException {
180185
if (buffer.remaining() == 0) {
181186
buffer.compact();
187+
dataReader.read(buffer);
188+
buffer.flip();
182189
}
183-
184-
int read;
185-
do {
186-
read = dataReader.read(buffer);
187-
} while (read > 0);
188-
buffer.flip();
189-
190190
return buffer.remaining() > 0;
191191
}
192192

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// TODO(bundles): Figure out whether we need this class (it is not needed in Web).
2222

2323
/** Metadata describing a Firestore document saved in the bundle. */
24-
public class BundledDocumentMetadata extends BundleElement {
24+
public class BundledDocumentMetadata implements BundleElement {
2525
private final DocumentKey key;
2626
private final SnapshotVersion readTime;
2727
private final boolean exists;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.google.firebase.firestore.core.Target;
1919

2020
/** A bundled query represents a query target and its limit. */
21-
public class BundledQuery extends BundleElement {
21+
public class BundledQuery implements BundleElement {
2222
private final Target target;
2323
private final Query.LimitType limitType;
2424

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.google.firebase.firestore.model.SnapshotVersion;
1818

1919
/** Represents a named query saved by the SDK in its local storage. */
20-
public class NamedQuery extends BundleElement {
20+
public class NamedQuery implements BundleElement {
2121
private final String name;
2222
private final BundledQuery bundledQuery;
2323
private final SnapshotVersion readTime;

0 commit comments

Comments
 (0)