Description
Create a URI instance for the given location String, replacing spaces with "%20" quotes first.
License
Open Source License
Parameter
Parameter | Description |
---|
location | the location String to convert into a URI instance |
Exception
Parameter | Description |
---|
URISyntaxException | if the location wasn't a valid URI |
Return
the URI instance
Declaration
public static URI toURI(String location) throws URISyntaxException
Method Source Code
//package com.java2s;
//License from project: Open Source License
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
public class Main {
/**//www .j ava 2s.c om
* Create a URI instance for the given URL, replacing spaces with "%20"
* quotes first.
* <p>
* Furthermore, this method works on JDK 1.4 as well, in contrast to the
* <code>URL.toURI()</code> method.
* @param url
* the URL to convert into a URI instance
* @return the URI instance
* @throws URISyntaxException
* if the URL wasn't a valid URI
* @see java.net.URL#toURI()
*/
public static URI toURI(URL url) throws URISyntaxException {
return toURI(url.toString());
}
/**
* Create a URI instance for the given location String, replacing spaces
* with "%20" quotes first.
* @param location
* the location String to convert into a URI instance
* @return the URI instance
* @throws URISyntaxException
* if the location wasn't a valid URI
*/
public static URI toURI(String location) throws URISyntaxException {
return new URI(replace(location, " ", "%20"));
}
private static String replace(String line, String oldString, String newString) {
if (line == null) {
return null;
}
int i = 0;
if ((i = line.indexOf(oldString, i)) >= 0) {
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ((i = line.indexOf(oldString, i)) > 0) {
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
}
Related
- toURI(final String path)
- toURI(final String uriString)
- toUri(java.io.File file)
- toUri(String endpoint, Protocol protocol)
- toUri(String ip, String path)
- toURI(String location)
- toURI(String location)
- toURI(String location)
- toURI(String location)