2828import com .google .protobuf .InvalidProtocolBufferException ;
2929import com .google .protobuf .util .JsonFormat ;
3030import java .nio .ByteBuffer ;
31+ import java .nio .charset .Charset ;
3132import java .nio .charset .StandardCharsets ;
3233import java .util .HashMap ;
3334import 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