Skip to content

Commit 0946a17

Browse files
authored
fix: Ensures bundles are encoded as UTF8 bytes. (#695)
* Ensures bundles are encoded as UTF8 bytes. * fix: Formatted.
1 parent 402ce47 commit 0946a17

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreBundle.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.protobuf.InvalidProtocolBufferException;
2929
import com.google.protobuf.util.JsonFormat;
3030
import java.nio.ByteBuffer;
31+
import java.nio.charset.Charset;
3132
import java.nio.charset.StandardCharsets;
3233
import java.util.HashMap;
3334
import java.util.List;
@@ -39,6 +40,7 @@ public final class FirestoreBundle {
3940
static final int BUNDLE_SCHEMA_VERSION = 1;
4041
// Printer to encode protobuf objects into JSON string.
4142
private static final JsonFormat.Printer PRINTER = JsonFormat.printer();
43+
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
4244

4345
// Raw byte array to hold the content of the bundle.
4446
private byte[] bundleData;
@@ -182,12 +184,12 @@ public FirestoreBundle build() {
182184
.setCreateTime(latestReadTime.toProto())
183185
.setVersion(BUNDLE_SCHEMA_VERSION)
184186
.setTotalDocuments(documents.size())
185-
.setTotalBytes(buffer.toString().getBytes().length)
187+
.setTotalBytes(buffer.toString().getBytes(DEFAULT_CHARSET).length)
186188
.build();
187189
BundleElement element = BundleElement.newBuilder().setMetadata(metadata).build();
188190
buffer.insert(0, elementToLengthPrefixedStringBuilder(element));
189191

190-
return new FirestoreBundle(buffer.toString().getBytes(StandardCharsets.UTF_8));
192+
return new FirestoreBundle(buffer.toString().getBytes(DEFAULT_CHARSET));
191193
}
192194

193195
private StringBuilder elementToLengthPrefixedStringBuilder(BundleElement element) {
@@ -197,7 +199,9 @@ private StringBuilder elementToLengthPrefixedStringBuilder(BundleElement element
197199
} catch (InvalidProtocolBufferException e) {
198200
throw new RuntimeException(e);
199201
}
200-
return new StringBuilder().append(elementJson.getBytes().length).append(elementJson);
202+
return new StringBuilder()
203+
.append(elementJson.getBytes(DEFAULT_CHARSET).length)
204+
.append(elementJson);
201205
}
202206
}
203207

0 commit comments

Comments
 (0)