Methods Summary |
---|
public abstract android.net.Uri$Builder | buildUpon()Constructs a new builder, copying the attributes from this Uri.
|
public int | compareTo(android.net.Uri other)Compares the string representation of this Uri with that of
another.
return toString().compareTo(other.toString());
|
public static java.lang.String | decode(java.lang.String s)Decodes '%'-escaped octets in the given string using the UTF-8 scheme.
Replaces invalid octets with the unicode replacement character
("\\uFFFD").
/*
Compared to java.net.URLEncoderDecoder.decode(), this method decodes a
chunk at a time instead of one character at a time, and it doesn't
throw exceptions. It also only allocates memory when necessary--if
there's nothing to decode, this method won't do much.
*/
if (s == null) {
return null;
}
// Lazily-initialized buffers.
StringBuilder decoded = null;
ByteArrayOutputStream out = null;
int oldLength = s.length();
// This loop alternates between copying over normal characters and
// escaping in chunks. This results in fewer method calls and
// allocations than decoding one character at a time.
int current = 0;
while (current < oldLength) {
// Start in "copying" mode where we copy over normal characters.
// Find the next escape sequence.
int nextEscape = s.indexOf('%", current);
if (nextEscape == NOT_FOUND) {
if (decoded == null) {
// We didn't actually decode anything.
return s;
} else {
// Append the remainder and return the decoded string.
decoded.append(s, current, oldLength);
return decoded.toString();
}
}
// Prepare buffers.
if (decoded == null) {
// Looks like we're going to need the buffers...
// We know the new string will be shorter. Using the old length
// may overshoot a bit, but it will save us from resizing the
// buffer.
decoded = new StringBuilder(oldLength);
out = new ByteArrayOutputStream(4);
} else {
// Clear decoding buffer.
out.reset();
}
// Append characters leading up to the escape.
if (nextEscape > current) {
decoded.append(s, current, nextEscape);
current = nextEscape;
} else {
// assert current == nextEscape
}
// Switch to "decoding" mode where we decode a string of escape
// sequences.
// Decode and append escape sequences. Escape sequences look like
// "%ab" where % is literal and a and b are hex digits.
try {
do {
if (current + 2 >= oldLength) {
// Truncated escape sequence.
out.write(REPLACEMENT);
} else {
int a = Character.digit(s.charAt(current + 1), 16);
int b = Character.digit(s.charAt(current + 2), 16);
if (a == -1 || b == -1) {
// Non hex digits.
out.write(REPLACEMENT);
} else {
// Combine the hex digits into one byte and write.
out.write((a << 4) + b);
}
}
// Move passed the escape sequence.
current += 3;
} while (current < oldLength && s.charAt(current) == '%");
// Decode UTF-8 bytes into a string and append it.
decoded.append(out.toString(DEFAULT_ENCODING));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
} catch (IOException e) {
throw new AssertionError(e);
}
}
// If we don't have a buffer, we didn't have to decode anything.
return decoded == null ? s : decoded.toString();
|
public static java.lang.String | encode(java.lang.String s)Encodes characters in the given string as '%'-escaped octets
using the UTF-8 scheme. Leaves letters ("A-Z", "a-z"), numbers
("0-9"), and unreserved characters ("_-!.~'()*") intact. Encodes
all other characters.
return encode(s, null);
|
public static java.lang.String | encode(java.lang.String s, java.lang.String allow)Encodes characters in the given string as '%'-escaped octets
using the UTF-8 scheme. Leaves letters ("A-Z", "a-z"), numbers
("0-9"), and unreserved characters ("_-!.~'()*") intact. Encodes
all other characters with the exception of those specified in the
allow argument.
if (s == null) {
return null;
}
// Lazily-initialized buffers.
StringBuilder encoded = null;
int oldLength = s.length();
// This loop alternates between copying over allowed characters and
// encoding in chunks. This results in fewer method calls and
// allocations than encoding one character at a time.
int current = 0;
while (current < oldLength) {
// Start in "copying" mode where we copy over allowed chars.
// Find the next character which needs to be encoded.
int nextToEncode = current;
while (nextToEncode < oldLength
&& isAllowed(s.charAt(nextToEncode), allow)) {
nextToEncode++;
}
// If there's nothing more to encode...
if (nextToEncode == oldLength) {
if (current == 0) {
// We didn't need to encode anything!
return s;
} else {
// Presumably, we've already done some encoding.
encoded.append(s, current, oldLength);
return encoded.toString();
}
}
if (encoded == null) {
encoded = new StringBuilder();
}
if (nextToEncode > current) {
// Append allowed characters leading up to this point.
encoded.append(s, current, nextToEncode);
} else {
// assert nextToEncode == current
}
// Switch to "encoding" mode.
// Find the next allowed character.
current = nextToEncode;
int nextAllowed = current + 1;
while (nextAllowed < oldLength
&& !isAllowed(s.charAt(nextAllowed), allow)) {
nextAllowed++;
}
// Convert the substring to bytes and encode the bytes as
// '%'-escaped octets.
String toEncode = s.substring(current, nextAllowed);
try {
byte[] bytes = toEncode.getBytes(DEFAULT_ENCODING);
int bytesLength = bytes.length;
for (int i = 0; i < bytesLength; i++) {
encoded.append('%");
encoded.append(HEX_DIGITS[(bytes[i] & 0xf0) >> 4]);
encoded.append(HEX_DIGITS[bytes[i] & 0xf]);
}
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
current = nextAllowed;
}
// Encoded could still be null at this point if s is empty.
return encoded == null ? s : encoded.toString();
|
public boolean | equals(java.lang.Object o)Compares this Uri to another object for equality. Returns true if the
encoded string representations of this Uri and the given Uri are
equal. Case counts. Paths are not normalized. If one Uri specifies a
default port explicitly and the other leaves it implicit, they will not
be considered equal.
if (!(o instanceof Uri)) {
return false;
}
Uri other = (Uri) o;
return toString().equals(other.toString());
|
public static android.net.Uri | fromFile(java.io.File file)Creates a Uri from a file. The URI has the form
"file://". Encodes path characters with the exception of
'/'.
Example: "file:///tmp/android.txt"
if (file == null) {
throw new NullPointerException("file");
}
PathPart path = PathPart.fromDecoded(file.getAbsolutePath());
return new HierarchicalUri(
"file", Part.EMPTY, path, Part.NULL, Part.NULL);
|
public static android.net.Uri | fromParts(java.lang.String scheme, java.lang.String ssp, java.lang.String fragment)Creates an opaque Uri from the given components. Encodes the ssp
which means this method cannot be used to create hierarchical URIs.
if (scheme == null) {
throw new NullPointerException("scheme");
}
if (ssp == null) {
throw new NullPointerException("ssp");
}
return new OpaqueUri(scheme, Part.fromDecoded(ssp),
Part.fromDecoded(fragment));
|
public abstract java.lang.String | getAuthority()Gets the decoded authority part of this URI. For
server addresses, the authority is structured as follows:
{@code [ userinfo '@' ] host [ ':' port ]}
Examples: "google.com", "bob@google.com:80"
|
public abstract java.lang.String | getEncodedAuthority()Gets the encoded authority part of this URI. For
server addresses, the authority is structured as follows:
{@code [ userinfo '@' ] host [ ':' port ]}
Examples: "google.com", "bob@google.com:80"
|
public abstract java.lang.String | getEncodedFragment()Gets the encoded fragment part of this URI, everything after the '#'.
|
public abstract java.lang.String | getEncodedPath()Gets the encoded path.
|
public abstract java.lang.String | getEncodedQuery()Gets the encoded query component from this URI. The query comes after
the query separator ('?') and before the fragment separator ('#'). This
method would return "q=android" for
"http://www.google.com/search?q=android".
|
public abstract java.lang.String | getEncodedSchemeSpecificPart()Gets the scheme-specific part of this URI, i.e. everything between the
scheme separator ':' and the fragment separator '#'. If this is a
relative URI, this method returns the entire URI. Leaves escaped octets
intact.
Example: "//www.google.com/search?q=android"
|
public abstract java.lang.String | getEncodedUserInfo()Gets the encoded user information from the authority.
For example, if the authority is "nobody@google.com", this method will
return "nobody".
|
public abstract java.lang.String | getFragment()Gets the decoded fragment part of this URI, everything after the '#'.
|
public abstract java.lang.String | getHost()Gets the encoded host from the authority for this URI. For example,
if the authority is "bob@google.com", this method will return
"google.com".
|
public abstract java.lang.String | getLastPathSegment()Gets the decoded last segment in the path.
|
public abstract java.lang.String | getPath()Gets the decoded path.
|
public abstract java.util.List | getPathSegments()Gets the decoded path segments.
|
public abstract int | getPort()Gets the port from the authority for this URI. For example,
if the authority is "google.com:80", this method will return 80.
|
public abstract java.lang.String | getQuery()Gets the decoded query component from this URI. The query comes after
the query separator ('?') and before the fragment separator ('#'). This
method would return "q=android" for
"http://www.google.com/search?q=android".
|
public java.lang.String | getQueryParameter(java.lang.String key)Searches the query string for the first value with the given key.
if (isOpaque()) {
throw new UnsupportedOperationException(NOT_HIERARCHICAL);
}
String query = getQuery();
if (query == null) {
return null;
}
String encodedKey;
try {
encodedKey = URLEncoder.encode(key, DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
String prefix = encodedKey + "=";
if (query.length() < prefix.length()) {
return null;
}
int start;
if (query.startsWith(prefix)) {
// It's the first parameter.
start = prefix.length();
} else {
// It must be later in the query string.
prefix = "&" + prefix;
start = query.indexOf(prefix);
if (start == -1) {
// Not found.
return null;
}
start += prefix.length();
}
// Find end of value.
int end = query.indexOf('&", start);
if (end == -1) {
end = query.length();
}
String value = query.substring(start, end);
return decode(value);
|
public java.util.List | getQueryParameters(java.lang.String key)Searches the query string for parameter values with the given key.
if (isOpaque()) {
throw new UnsupportedOperationException(NOT_HIERARCHICAL);
}
String query = getQuery();
if (query == null) {
return Collections.emptyList();
}
String encodedKey;
try {
encodedKey = URLEncoder.encode(key, DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
// Prepend query with "&" making the first parameter the same as the
// rest.
query = "&" + query;
// Parameter prefix.
String prefix = "&" + encodedKey + "=";
ArrayList<String> values = new ArrayList<String>();
int start = 0;
int length = query.length();
while (start < length) {
start = query.indexOf(prefix, start);
if (start == -1) {
// No more values.
break;
}
// Move start to start of value.
start += prefix.length();
// Find end of value.
int end = query.indexOf('&", start);
if (end == -1) {
end = query.length();
}
String value = query.substring(start, end);
values.add(decode(value));
start = end;
}
return Collections.unmodifiableList(values);
|
public abstract java.lang.String | getScheme()Gets the scheme of this URI. Example: "http"
|
public abstract java.lang.String | getSchemeSpecificPart()Gets the scheme-specific part of this URI, i.e. everything between the
scheme separator ':' and the fragment separator '#'. If this is a
relative URI, this method returns the entire URI. Decodes escaped octets.
Example: "//www.google.com/search?q=android"
|
public abstract java.lang.String | getUserInfo()Gets the decoded user information from the authority.
For example, if the authority is "nobody@google.com", this method will
return "nobody".
|
public int | hashCode()Hashes the encoded string represention of this Uri consistently with
{@link #equals(Object)}.
return toString().hashCode();
|
public boolean | isAbsolute()Returns true if this URI is absolute, i.e. if it contains an
explicit scheme.
return !isRelative();
|
private static boolean | isAllowed(char c, java.lang.String allow)Returns true if the given character is allowed.
return (c >= 'A" && c <= 'Z")
|| (c >= 'a" && c <= 'z")
|| (c >= '0" && c <= '9")
|| "_-!.~'()*".indexOf(c) != NOT_FOUND
|| (allow != null && allow.indexOf(c) != NOT_FOUND);
|
public abstract boolean | isHierarchical()Returns true if this URI is hierarchical like "http://google.com".
Absolute URIs are hierarchical if the scheme-specific part starts with
a '/'. Relative URIs are always hierarchical.
|
public boolean | isOpaque()Returns true if this URI is opaque like "mailto:nobody@google.com". The
scheme-specific part of an opaque URI cannot start with a '/'.
return !isHierarchical();
|
public abstract boolean | isRelative()Returns true if this URI is relative, i.e. if it doesn't contain an
explicit scheme.
|
public static android.net.Uri | parse(java.lang.String uriString)Creates a Uri which parses the given encoded URI string.
return new StringUri(uriString);
|
public abstract java.lang.String | toString()Returns the encoded string representation of this URI.
Example: "http://google.com/"
|
public static android.net.Uri | withAppendedPath(android.net.Uri baseUri, java.lang.String pathSegment)Creates a new Uri by appending an already-encoded path segment to a
base Uri.
Builder builder = baseUri.buildUpon();
builder = builder.appendEncodedPath(pathSegment);
return builder.build();
|
public static void | writeToParcel(android.os.Parcel out, android.net.Uri uri)Writes a Uri to a Parcel.
if (uri == null) {
out.writeInt(NULL_TYPE_ID);
} else {
uri.writeToParcel(out, 0);
}
|