|
17 | 17 | package com.google.gcloud.datastore; |
18 | 18 |
|
19 | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
20 | | -import static java.nio.charset.StandardCharsets.UTF_8; |
21 | 20 |
|
22 | 21 | import com.google.api.services.datastore.DatastoreV1; |
23 | 22 | import com.google.api.services.datastore.DatastoreV1.Value; |
24 | 23 | import com.google.common.base.MoreObjects; |
25 | 24 | import com.google.common.base.MoreObjects.ToStringHelper; |
| 25 | +import com.google.common.io.BaseEncoding; |
26 | 26 | import com.google.protobuf.ByteString; |
27 | 27 | 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; |
34 | 28 |
|
35 | 29 | /** |
36 | 30 | * A Google Cloud Datastore cursor. |
@@ -74,23 +68,16 @@ ByteString byteString() { |
74 | 68 | * Returns the cursor in an encoded form that can be used as part of a URL. |
75 | 69 | */ |
76 | 70 | 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()); |
82 | 72 | } |
83 | 73 |
|
84 | 74 | /** |
85 | 75 | * Create a {@code Cursor} given its URL safe encoded form. |
86 | 76 | */ |
87 | 77 | public static Cursor fromUrlSafe(String urlSafe) { |
88 | 78 | 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) { |
94 | 81 | throw new IllegalStateException("Unexpected decoding exception", e); |
95 | 82 | } |
96 | 83 | } |
|
0 commit comments