Skip to content

Commit 03ca1bf

Browse files
author
Ajay Kannan
committed
Simplify to/from url safe and fix access issue in workaround
1 parent f111953 commit 03ca1bf

2 files changed

Lines changed: 5 additions & 18 deletions

File tree

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Cursor.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,14 @@
1717
package com.google.gcloud.datastore;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20-
import static java.nio.charset.StandardCharsets.UTF_8;
2120

2221
import com.google.api.services.datastore.DatastoreV1;
2322
import com.google.api.services.datastore.DatastoreV1.Value;
2423
import com.google.common.base.MoreObjects;
2524
import com.google.common.base.MoreObjects.ToStringHelper;
25+
import com.google.common.io.BaseEncoding;
2626
import com.google.protobuf.ByteString;
2727
import com.google.protobuf.InvalidProtocolBufferException;
28-
import com.google.protobuf.TextFormat;
29-
import com.google.protobuf.TextFormat.ParseException;
30-
31-
import java.io.UnsupportedEncodingException;
32-
import java.net.URLDecoder;
33-
import java.net.URLEncoder;
3428

3529
/**
3630
* A Google Cloud Datastore cursor.
@@ -74,23 +68,16 @@ ByteString byteString() {
7468
* Returns the cursor in an encoded form that can be used as part of a URL.
7569
*/
7670
public String toUrlSafe() {
77-
try {
78-
return URLEncoder.encode(TextFormat.printToString(toPb()), UTF_8.name());
79-
} catch (UnsupportedEncodingException e) {
80-
throw new IllegalStateException("Unexpected encoding exception", e);
81-
}
71+
return BaseEncoding.base64Url().encode(byteString.toByteArray());
8272
}
8373

8474
/**
8575
* Create a {@code Cursor} given its URL safe encoded form.
8676
*/
8777
public static Cursor fromUrlSafe(String urlSafe) {
8878
try {
89-
String utf8Str = URLDecoder.decode(urlSafe, UTF_8.name());
90-
DatastoreV1.Value.Builder builder = DatastoreV1.Value.newBuilder();
91-
TextFormat.merge(utf8Str, builder);
92-
return fromPb(builder.build());
93-
} catch (UnsupportedEncodingException | ParseException e) {
79+
return Cursor.copyFrom(BaseEncoding.base64Url().decode(urlSafe));
80+
} catch (IllegalArgumentException e) {
9481
throw new IllegalStateException("Unexpected decoding exception", e);
9582
}
9683
}

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ public StructuredQuery<V> build() {
782782
}
783783
}
784784

785-
static final class Builder<V> extends BaseBuilder<V, Builder<V>> {
785+
public static final class Builder<V> extends BaseBuilder<V, Builder<V>> {
786786

787787
Builder(ResultType<V> resultType) {
788788
super(resultType);

0 commit comments

Comments
 (0)