Skip to content

Commit

Permalink
fix: Ensures bundles are encoded as UTF8 bytes. (#695)
Browse files Browse the repository at this point in the history
* Ensures bundles are encoded as UTF8 bytes.

* fix: Formatted.
  • Loading branch information
wu-hui committed Jul 13, 2021
1 parent 402ce47 commit 0946a17
Showing 1 changed file with 7 additions and 3 deletions.
Expand Up @@ -28,6 +28,7 @@
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
Expand All @@ -39,6 +40,7 @@ public final class FirestoreBundle {
static final int BUNDLE_SCHEMA_VERSION = 1;
// Printer to encode protobuf objects into JSON string.
private static final JsonFormat.Printer PRINTER = JsonFormat.printer();
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

// Raw byte array to hold the content of the bundle.
private byte[] bundleData;
Expand Down Expand Up @@ -182,12 +184,12 @@ public FirestoreBundle build() {
.setCreateTime(latestReadTime.toProto())
.setVersion(BUNDLE_SCHEMA_VERSION)
.setTotalDocuments(documents.size())
.setTotalBytes(buffer.toString().getBytes().length)
.setTotalBytes(buffer.toString().getBytes(DEFAULT_CHARSET).length)
.build();
BundleElement element = BundleElement.newBuilder().setMetadata(metadata).build();
buffer.insert(0, elementToLengthPrefixedStringBuilder(element));

return new FirestoreBundle(buffer.toString().getBytes(StandardCharsets.UTF_8));
return new FirestoreBundle(buffer.toString().getBytes(DEFAULT_CHARSET));
}

private StringBuilder elementToLengthPrefixedStringBuilder(BundleElement element) {
Expand All @@ -197,7 +199,9 @@ private StringBuilder elementToLengthPrefixedStringBuilder(BundleElement element
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
return new StringBuilder().append(elementJson.getBytes().length).append(elementJson);
return new StringBuilder()
.append(elementJson.getBytes(DEFAULT_CHARSET).length)
.append(elementJson);
}
}

Expand Down

0 comments on commit 0946a17

Please sign in to comment.