Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensures bundles are encoded as UTF8 bytes. #695

Merged
merged 2 commits into from Jul 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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