1717package com .google .gcloud .datastore ;
1818
1919import static com .google .common .base .Preconditions .checkNotNull ;
20- import static java .nio .charset .StandardCharsets .UTF_8 ;
2120
2221import com .google .api .services .datastore .DatastoreV1 ;
2322import com .google .api .services .datastore .DatastoreV1 .Value ;
2423import com .google .common .base .MoreObjects ;
2524import com .google .common .base .MoreObjects .ToStringHelper ;
26- import com .google .common .base . Preconditions ;
25+ import com .google .common .io . BaseEncoding ;
2726import com .google .protobuf .ByteString ;
2827import com .google .protobuf .InvalidProtocolBufferException ;
29- import com .google .protobuf .TextFormat ;
30- import com .google .protobuf .TextFormat .ParseException ;
31-
32- import java .io .UnsupportedEncodingException ;
33- import java .net .URLDecoder ;
34- import java .net .URLEncoder ;
3528
3629/**
3730 * A Google Cloud Datastore cursor.
@@ -44,7 +37,6 @@ public final class Cursor extends Serializable<DatastoreV1.Value> {
4437 private final transient ByteString byteString ;
4538
4639 Cursor (ByteString byteString ) {
47- Preconditions .checkArgument (byteString .isValidUtf8 (), "content is not a valid UTF-8" );
4840 this .byteString = byteString ;
4941 }
5042
@@ -76,23 +68,16 @@ ByteString byteString() {
7668 * Returns the cursor in an encoded form that can be used as part of a URL.
7769 */
7870 public String toUrlSafe () {
79- try {
80- return URLEncoder .encode (TextFormat .printToString (toPb ()), UTF_8 .name ());
81- } catch (UnsupportedEncodingException e ) {
82- throw new IllegalStateException ("Unexpected encoding exception" , e );
83- }
71+ return BaseEncoding .base64Url ().encode (byteString .toByteArray ());
8472 }
8573
8674 /**
8775 * Create a {@code Cursor} given its URL safe encoded form.
8876 */
8977 public static Cursor fromUrlSafe (String urlSafe ) {
9078 try {
91- String utf8Str = URLDecoder .decode (urlSafe , UTF_8 .name ());
92- DatastoreV1 .Value .Builder builder = DatastoreV1 .Value .newBuilder ();
93- TextFormat .merge (utf8Str , builder );
94- return fromPb (builder .build ());
95- } catch (UnsupportedEncodingException | ParseException e ) {
79+ return Cursor .copyFrom (BaseEncoding .base64Url ().decode (urlSafe ));
80+ } catch (IllegalArgumentException e ) {
9681 throw new IllegalStateException ("Unexpected decoding exception" , e );
9782 }
9883 }
0 commit comments