diff options
Diffstat (limited to 'core/java/android/net/Uri.java')
| -rw-r--r-- | core/java/android/net/Uri.java | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/core/java/android/net/Uri.java b/core/java/android/net/Uri.java index ddca4b464123..e4744f79fcda 100644 --- a/core/java/android/net/Uri.java +++ b/core/java/android/net/Uri.java @@ -1987,17 +1987,26 @@ public abstract class Uri implements Parcelable, Comparable<Uri> { * Enum which indicates which representation of a given part we have. */ static class Representation { - static final int BOTH = 0; static final int ENCODED = 1; static final int DECODED = 2; } volatile String encoded; volatile String decoded; + private final int mCanonicalRepresentation; AbstractPart(String encoded, String decoded) { - this.encoded = encoded; - this.decoded = decoded; + if (encoded != NOT_CACHED) { + this.mCanonicalRepresentation = Representation.ENCODED; + this.encoded = encoded; + this.decoded = NOT_CACHED; + } else if (decoded != NOT_CACHED) { + this.mCanonicalRepresentation = Representation.DECODED; + this.encoded = NOT_CACHED; + this.decoded = decoded; + } else { + throw new IllegalArgumentException("Neither encoded nor decoded"); + } } abstract String getEncoded(); @@ -2009,25 +2018,21 @@ public abstract class Uri implements Parcelable, Comparable<Uri> { } final void writeTo(Parcel parcel) { - @SuppressWarnings("StringEquality") - boolean hasEncoded = encoded != NOT_CACHED; - - @SuppressWarnings("StringEquality") - boolean hasDecoded = decoded != NOT_CACHED; - - if (hasEncoded && hasDecoded) { - parcel.writeInt(Representation.BOTH); - parcel.writeString(encoded); - parcel.writeString(decoded); - } else if (hasEncoded) { - parcel.writeInt(Representation.ENCODED); - parcel.writeString(encoded); - } else if (hasDecoded) { - parcel.writeInt(Representation.DECODED); - parcel.writeString(decoded); + final String canonicalValue; + if (mCanonicalRepresentation == Representation.ENCODED) { + canonicalValue = encoded; + } else if (mCanonicalRepresentation == Representation.DECODED) { + canonicalValue = decoded; } else { - throw new IllegalArgumentException("Neither encoded nor decoded"); + throw new IllegalArgumentException("Unknown representation: " + + mCanonicalRepresentation); + } + if (canonicalValue == NOT_CACHED) { + throw new AssertionError("Canonical value not cached (" + + mCanonicalRepresentation + ")"); } + parcel.writeInt(mCanonicalRepresentation); + parcel.writeString(canonicalValue); } } @@ -2059,13 +2064,12 @@ public abstract class Uri implements Parcelable, Comparable<Uri> { static Part readFrom(Parcel parcel) { int representation = parcel.readInt(); + String value = parcel.readString(); switch (representation) { - case Representation.BOTH: - return from(parcel.readString(), parcel.readString()); case Representation.ENCODED: - return fromEncoded(parcel.readString()); + return fromEncoded(value); case Representation.DECODED: - return fromDecoded(parcel.readString()); + return fromDecoded(value); default: throw new IllegalArgumentException("Unknown representation: " + representation); @@ -2127,6 +2131,11 @@ public abstract class Uri implements Parcelable, Comparable<Uri> { private static class EmptyPart extends Part { public EmptyPart(String value) { super(value, value); + if (value != null && !value.isEmpty()) { + throw new IllegalArgumentException("Expected empty value, got: " + value); + } + // Avoid having to re-calculate the non-canonical value. + encoded = decoded = value; } @Override @@ -2245,14 +2254,12 @@ public abstract class Uri implements Parcelable, Comparable<Uri> { static PathPart readFrom(Parcel parcel) { int representation = parcel.readInt(); switch (representation) { - case Representation.BOTH: - return from(parcel.readString(), parcel.readString()); case Representation.ENCODED: return fromEncoded(parcel.readString()); case Representation.DECODED: return fromDecoded(parcel.readString()); default: - throw new IllegalArgumentException("Bad representation: " + representation); + throw new IllegalArgumentException("Unknown representation: " + representation); } } |
