Creates a URI from a string assumed to be valid. Useful for defining URI constants in code. Not for user input. @throws IllegalArgumentException if 's' is not a valid RFC 3986 URI.
(String s)
| 212 | * @throws IllegalArgumentException if 's' is not a valid RFC 3986 URI. |
| 213 | */ |
| 214 | public static Uri create(String s) { |
| 215 | Builder builder = new Builder(); |
| 216 | int i = 0; |
| 217 | final int n = s.length(); |
| 218 | |
| 219 | // 3.1. Scheme: Look for a ':' before '/', '?', or '#'. |
| 220 | int schemeColon = -1; |
| 221 | for (; i < n; ++i) { |
| 222 | char c = s.charAt(i); |
| 223 | if (c == ':') { |
| 224 | schemeColon = i; |
| 225 | break; |
| 226 | } else if (c == '/' || c == '?' || c == '#') { |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | if (schemeColon < 0) { |
| 231 | throw new IllegalArgumentException("Missing required scheme."); |
| 232 | } |
| 233 | builder.setRawScheme(s.substring(0, schemeColon)); |
| 234 | |
| 235 | // 3.2. Authority. Look for '//' then keep scanning until '/', '?', or '#'. |
| 236 | i = schemeColon + 1; |
| 237 | if (i + 1 < n && s.charAt(i) == '/' && s.charAt(i + 1) == '/') { |
| 238 | // "//" just means we have an authority. Skip over it. |
| 239 | i += 2; |
| 240 | |
| 241 | int authorityStart = i; |
| 242 | for (; i < n; ++i) { |
| 243 | char c = s.charAt(i); |
| 244 | if (c == '/' || c == '?' || c == '#') { |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | builder.setRawAuthority(s.substring(authorityStart, i)); |
| 249 | } |
| 250 | |
| 251 | // 3.3. Path: Whatever is left before '?' or '#'. |
| 252 | int pathStart = i; |
| 253 | for (; i < n; ++i) { |
| 254 | char c = s.charAt(i); |
| 255 | if (c == '?' || c == '#') { |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | builder.setRawPath(s.substring(pathStart, i)); |
| 260 | |
| 261 | // 3.4. Query, if we stopped at '?'. |
| 262 | if (i < n && s.charAt(i) == '?') { |
| 263 | i++; // Skip '?' |
| 264 | int queryStart = i; |
| 265 | for (; i < n; ++i) { |
| 266 | char c = s.charAt(i); |
| 267 | if (c == '#') { |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | builder.setRawQuery(s.substring(queryStart, i)); |