public final class URLUtil extends Object
Work with any hierarchical URLs. Does not work with opaque URLs, except for a few functions which work with "jar:" URLs.
Note that, for these few functions, the path of "jar:" URL (e.g. jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class) is everything after "!/", including the leading "/".
Modifier and Type | Class and Description |
---|---|
static class |
URLUtil.URLComparator
Compares two URLs by their
external forms . |
Modifier and Type | Field and Description |
---|---|
static URLUtil.URLComparator |
COMPARATOR
A ready-to-use URLComparator.
|
static URL[] |
EMPTY_LIST
A ready-to-use empty list of URLs.
|
Modifier and Type | Method and Description |
---|---|
static URLConnection |
checkHttpConnection(URLConnection connection,
boolean followRedirects)
Establish specified HTTP connection and check whether it returns
a "200 OK".
|
static String |
contentTypeToCharset(String contentType)
Returns the value of the charset parameter possibly found in specified
content type.
|
static String |
contentTypeToMedia(String contentType)
Parses a content type such as "text/html; charset=ISO-8859-1"
and returns the media type (for the above example "text/html").
|
static int |
copyFile(String srcLocation,
String dstLocation)
Copies the contents of specified URL to specified "file:" URL.
|
static URL |
createURL(String spec)
Same as
new URL(spec) , except that non-ASCII characters
and other illegal characters such as spaces possibly found in
spec are %HH-encoded. |
static URL |
createURL(URL context,
String spec)
Same as
new URL(context, spec) , except that non-ASCII
characters and other illegal characters such as spaces possibly found
in spec are %HH-encoded. |
static URL |
createURL(URL context,
String spec,
URLStreamHandler handler)
Same as
new URL(context, spec, handler) , except that
non-ASCII characters and other illegal characters such as spaces
possibly found in spec are %HH-encoded. |
static boolean |
exists(URL url)
Equivalent to
exists(url, true, -1) . |
static boolean |
exists(URL url,
boolean followRedirects,
int timeout)
Tests whether specified URL corresponds to an existing resource.
|
static String |
getBaseName(URL url)
Same as
getRawBaseName(java.net.URL) except that the returned value is
decoded using URIComponent.decode(java.lang.String) . |
static String |
getExtension(URL url)
Same as
getRawExtension(java.net.URL) except that the returned value is
decoded using URIComponent.decode(java.lang.String) . |
static String |
getFragment(URL url)
Same as
URL.getRef except that the returned value is
decoded using URIComponent.decode(java.lang.String) . |
static URL |
getParent(URL url)
Returns the parent of specified URL, if any.
|
static String |
getPath(URL url)
Same as
getRawPath(java.net.URL) except that the returned value is
decoded using URIComponent.decode(java.lang.String) . |
static String |
getRawBaseName(URL url)
Returns the raw (that is, possibly containing %HH escapes) basename
part of the path, if specified URL has a path.
|
static String |
getRawExtension(URL url)
Returns the raw (that is, possibly containing %HH escapes) extension of
the path, if specified URL has a path.
|
static String |
getRawPath(URL url)
Returns the raw (that is, possibly containing %HH escapes) path,
if specified URL has a path.
|
static String |
getRawRelativePath(URL url,
URL base)
Returns the path of specified URL relative to specified base URL.
|
static String |
getRawUserName(URL url)
Returns the raw (that is, possibly containing %HH escapes) user
name, if a user info is found in specified URL.
|
static String |
getRawUserPassword(URL url)
Returns the raw (that is, possibly containing %HH escapes) user
password, if a user info is found in specified URL.
|
static URL |
getRoot(URL url)
Returns the root of specified URL.
|
static String |
getUserName(URL url)
Same as
getRawUserName(java.net.URL) except that the returned value is
decoded using URIComponent.decode(java.lang.String) . |
static String |
getUserPassword(URL url)
Same as
getRawUserPassword(java.net.URL) except that the returned value is
decoded using URIComponent.decode(java.lang.String) . |
static boolean |
isAncestorOf(URL ancestorURL,
URL url)
Tests whether first specified URL is an ancestor of second specified URL.
|
static boolean |
isDataURL(URL url)
Returns
true if specified URL is a data: URL,
otherwise returns false |
static boolean |
isFileURL(URL url)
Returns
true if specified URL is a file: URL,
otherwise returns false |
static boolean |
isJarURL(URL url)
Returns
true if specified URL is a jar: URL,
otherwise returns false |
static long |
lastModified(URL url)
Equivalent to
lastModified(url, true, -1) . |
static long |
lastModified(URL url,
boolean followRedirects,
int timeout)
Returns the date of the resource having specified URL.
|
static byte[] |
loadBytes(URL url)
Equivalent to
loadBytes(url, true, -1) . |
static byte[] |
loadBytes(URL url,
boolean followRedirects,
int timeout)
Loads the content of an URL containing binary data.
|
static String |
loadString(URL url)
Equivalent to
loadString(url, null, true, -1) . |
static String |
loadString(URL url,
String charset)
Equivalent to
loadString(url, charset, true, -1) . |
static String |
loadString(URL url,
String charset,
boolean followRedirects,
int timeout)
Loads the content of an URL containing text.
|
static URL |
newURL(String spec)
A cover for
URL.URL(String) . |
static URL |
newURL(String protocol,
String host,
int port,
String file)
A cover for
URL.URL(String, String, int, String) . |
static URL |
newURL(String protocol,
String host,
int port,
String file,
URLStreamHandler handler)
|
static URL |
newURL(String protocol,
String host,
String file)
A cover for
URL.URL(String, String, String) . |
static URL |
newURL(URL context,
String spec)
A cover for
URL.URL(URL, String) . |
static URL |
newURL(URL context,
String spec,
URLStreamHandler handler)
A cover for
URL.URL(URL, String, URLStreamHandler) . |
static String |
normalizeContentType(String contentType,
String defaultCharset)
Returns a normalized string form for specified content type.
|
static URLConnection |
openConnectionNoCache(URL url)
Similar to
url.openConnection except that the accessed
resource may not be a cached copy. |
static URLConnection |
openConnectionUseCache(URL url)
Similar to
url.openConnection except that the accessed
resource may be a cached copy. |
static InputStream |
openStreamNoCache(URL url)
Similar to
url.openStream except that the accessed
resource may not be a cached copy. |
static InputStream |
openStreamUseCache(URL url)
Similar to
url.openStream except that the accessed
resource may be a cached copy. |
static boolean |
sameContentType(String ct1,
String ct2,
String defaultCharset)
Tests whether specified content types are identical.
|
static boolean |
sameRoot(URL url1,
URL url2)
Returns
true if specified URLs have the same root. |
static URL |
setExtension(URL url,
String extension)
Same as
setRawExtension(java.net.URL, java.lang.String) except that specified extension is
quoted using URIComponent.quotePath(java.lang.String) . |
static URL |
setFragment(URL url,
String fragment)
Same as
setRawFragment(java.net.URL, java.lang.String) except that specified fragment is
quoted using URIComponent.quoteFragment(java.lang.String) . |
static URL |
setRawExtension(URL url,
String extension)
Changes the extension of specified URL to specified extension.
|
static URL |
setRawFragment(URL url,
String fragment)
Changes the fragment of specified URL to specified fragment.
|
static URL |
setRawUserInfo(URL url,
String userName,
String password)
Changes the user info of specified URL to specified user info.
|
static URL |
setUserInfo(URL url,
String userName,
String password)
Same as
setRawUserInfo(java.net.URL, java.lang.String, java.lang.String) except that specified user info is
quoted using URIComponent.quoteUserInfo(java.lang.String) . |
static String |
toDisplayForm(URL url)
Same as
java.net.URL.toExternalForm except that returned
string may contain non-ASCII characters and that, if specified URL
contains a password, the characters of this password are replaced by
'*'. |
static String |
toLabel(URL url)
Same as
toDisplayForm(java.net.URL) but file: URLs
not having a fragment are displayed as plain file names. |
static String |
toShortDisplayForm(URL url,
int maxLength)
Same as
toDisplayForm(java.net.URL) except that the returned string is made
shorter than specified length (when possible). |
static String |
toShortLabel(URL url,
int maxLength)
Same as
toLabel(java.net.URL) except that the returned string is made
shorter than specified length (when possible). |
static URL |
urlOrFile(String path)
Equivalent to
urlOrFile(path, false, false, null) . |
static URL |
urlOrFile(String path,
boolean checkAbsolute)
Equivalent to
urlOrFile(path, checkAbsolute, false, null) . |
static URL |
urlOrFile(String path,
boolean checkAbsolute,
boolean allowDir)
Equivalent to
urlOrFile(path, checkAbsolute, allowDir, null) . |
static URL |
urlOrFile(String path,
boolean checkAbsolute,
boolean allowDir,
URL baseURL)
Returns an URL created from specified path.
|
static File |
urlToFile(URL url)
Converts a file: URL to a File.
|
static URI |
urlToURI(URL url)
Similar to
java.net.URL.toURI() except that this utility
will not throw a java.net.URISyntaxException if the URL
spec contains illegal characters such as spaces. |
public static final URL[] EMPTY_LIST
public static final URLUtil.URLComparator COMPARATOR
public static URL createURL(String spec) throws MalformedURLException
new URL(spec)
, except that non-ASCII characters
and other illegal characters such as spaces possibly found in
spec are %HH-encoded.MalformedURLException
URIComponent.encode(java.lang.String)
public static URL createURL(URL context, String spec) throws MalformedURLException
new URL(context, spec)
, except that non-ASCII
characters and other illegal characters such as spaces possibly found
in spec are %HH-encoded.MalformedURLException
URIComponent.encode(java.lang.String)
public static URL createURL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException
new URL(context, spec, handler)
, except that
non-ASCII characters and other illegal characters such as spaces
possibly found in spec are %HH-encoded.MalformedURLException
URIComponent.encode(java.lang.String)
public static URL newURL(String spec) throws MalformedURLException
URL.URL(String)
.MalformedURLException
public static URL newURL(URL context, String spec) throws MalformedURLException
URL.URL(URL, String)
.MalformedURLException
public static URL newURL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException
URL.URL(URL, String, URLStreamHandler)
.MalformedURLException
public static URL newURL(String protocol, String host, String file) throws MalformedURLException
URL.URL(String, String, String)
.MalformedURLException
public static URL newURL(String protocol, String host, int port, String file) throws MalformedURLException
URL.URL(String, String, int, String)
.MalformedURLException
public static URL newURL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException
MalformedURLException
public static boolean isFileURL(URL url)
true
if specified URL is a file: URL,
otherwise returns false
public static boolean isJarURL(URL url)
true
if specified URL is a jar: URL,
otherwise returns false
public static boolean isDataURL(URL url)
true
if specified URL is a data: URL,
otherwise returns false
public static File urlToFile(URL url)
On Windows, this function converts a "file:" URL having a host (other than "localhost") to an UNC filename. For example, it converts "file://foo/bar/gee.txt" to "\\foo\bar\gee.txt".
url
- the URL to be convertednull
if url
cannot be converted to a File (for example, because url
is
not a file: URL)isFileURL(java.net.URL)
,
FileUtil.fileToURL(java.io.File)
,
urlOrFile(java.lang.String)
public static URI urlToURI(URL url)
java.net.URL.toURI()
except that this utility
will not throw a java.net.URISyntaxException
if the URL
spec contains illegal characters such as spaces. In such case, special
efforts are made to nevertheless return a URI equivalent to specified
URL.url
- URL to be convertednull
if this really cannot be
donepublic static URL urlOrFile(String path)
urlOrFile(path, false, false, null)
.public static URL urlOrFile(String path, boolean checkAbsolute)
urlOrFile(path, checkAbsolute, false, null)
.public static URL urlOrFile(String path, boolean checkAbsolute, boolean allowDir)
urlOrFile(path, checkAbsolute, allowDir, null)
.public static URL urlOrFile(String path, boolean checkAbsolute, boolean allowDir, URL baseURL)
FileUtil.fileToURL(java.io.File)
.path
- external form of an URL or the filename of an existing file
or directory.
If the path contains newline characters, everything after the first newline character, including this character, is ignored.
The reason for this is that Web browsers such as Firefox seems to append the title of the Web page after its URL.
checkAbsolute
- if true
, when path is a
filename, path must be absolute or this function will return
null
allowDir
- if true
, when path is a filename,
path is allowed to be not only the path of a file but also the
path of a directorybaseURL
- which base URL to use to resolve path
when its a relative URL. May be null
.null
if specified path cannot be
converted to an URL given specified requirementspublic static boolean sameRoot(URL url1, URL url2)
true
if specified URLs have the same root.getRoot(java.net.URL)
public static URL getRoot(URL url)
Example: returns "http://java.sun.com/" for "http://java.sun.com/docs/index.html".
Example: returns "jar:http://www.foo.com/bar/baz.jar!/" for "jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class".
url
- a hierachical or "jar:" URLpublic static boolean isAncestorOf(URL ancestorURL, URL url)
ancestorURL
- a hierachical or "jar:" URLurl
- a hierachical or "jar:" URLtrue
if ancestorURL is equal to
or is an ancestor directory of url;
false
otherwise.public static URL getParent(URL url)
Examples:
null
for "http://java.sun.com/".
url
- a hierachical or "jar:" URLnull
for root URLs.URIComponent.getRawParentPath(String, boolean)
public static String getPath(URL url)
getRawPath(java.net.URL)
except that the returned value is
decoded using URIComponent.decode(java.lang.String)
.public static String getRawPath(URL url)
Example: returns "/index.html" for "http://www.acme.com/index.html".
Example: returns "/COM/foo/Quux.class" for "jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class".
url
- a hierachical or "jar:" URLnull
if specified URL has no path
(this is not consistent with URL.getPath
which returns the
empty string in such case)public static String getBaseName(URL url)
getRawBaseName(java.net.URL)
except that the returned value is
decoded using URIComponent.decode(java.lang.String)
.public static String getRawBaseName(URL url)
Example: returns "index.html" for "http://www.acme.com/index.html".
Example: returns "png" for "http://www.acme.com/icons/png/".
Example: returns "Quux.class" for "jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class".
url
- a hierachical or "jar:" URLnull
if specified URL has no path
(this is not consistent with URL.getPath
which returns the
empty string in such case)URIComponent.getRawBaseName(java.lang.String)
public static String getExtension(URL url)
getRawExtension(java.net.URL)
except that the returned value is
decoded using URIComponent.decode(java.lang.String)
.public static String getRawExtension(URL url)
Example: returns "html" for "http://www.acme.com/index.html".
Example: returns null
for
"http://www.acme.com/icons/png/".
Example: returns "class" for "jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class".
url
- a hierachical or "jar:" URLnull
if specified URL has no path
(this is not consistent with URL.getPath
which returns the
empty string in such case)URIComponent.getRawExtension(java.lang.String)
public static URL setExtension(URL url, String extension)
setRawExtension(java.net.URL, java.lang.String)
except that specified extension is
quoted using URIComponent.quotePath(java.lang.String)
.public static URL setRawExtension(URL url, String extension)
url
- a hierachical or "jar:" URLextension
- new extension. Assumed to have been quoted using
URIComponent.quotePath(java.lang.String)
. May be null
which means:
remove the extension.Returns same URL if specified URL has no path or its path ends with '/'.
URIComponent.setRawExtension(java.lang.String, java.lang.String)
public static String getFragment(URL url)
URL.getRef
except that the returned value is
decoded using URIComponent.decode(java.lang.String)
.public static URL setFragment(URL url, String fragment)
setRawFragment(java.net.URL, java.lang.String)
except that specified fragment is
quoted using URIComponent.quoteFragment(java.lang.String)
.public static URL setRawFragment(URL url, String fragment)
url
- a hierachical or "jar:" URLfragment
- new fragment. Assumed to have been quoted using URIComponent.quoteFragment(java.lang.String)
. May be null
which means:
remove the fragment.URIComponent.setRawFragment(java.lang.String, java.lang.String)
public static String getUserName(URL url)
getRawUserName(java.net.URL)
except that the returned value is
decoded using URIComponent.decode(java.lang.String)
.public static String getRawUserName(URL url)
null
otherwise.public static String getUserPassword(URL url)
getRawUserPassword(java.net.URL)
except that the returned value is
decoded using URIComponent.decode(java.lang.String)
.public static String getRawUserPassword(URL url)
null
otherwise.public static URL setUserInfo(URL url, String userName, String password)
setRawUserInfo(java.net.URL, java.lang.String, java.lang.String)
except that specified user info is
quoted using URIComponent.quoteUserInfo(java.lang.String)
.public static URL setRawUserInfo(URL url, String userName, String password)
url
- a hierachical or "jar:" URLuserName
- new username. Assumed to have been quoted using URIComponent.quoteUserInfo(java.lang.String)
. May be null
, which means:
remove user info.password
- new password. Assumed to have been quoted using URIComponent.quoteUserInfo(java.lang.String)
. May be null
, which means:
password not specified.public static String getRawRelativePath(URL url, URL base)
More precisely returns relativePath such that new
URL(base, relativePath)
equals url.
url
- a hierarchical or "jar:" URLbase
- another hierarchical or "jar:" URLURL.toExternalForm
if
url or base have no path or if url and
base don't have the same rootURIComponent.getRawRelativePath(java.lang.String, java.lang.String)
public static String toLabel(URL url)
toDisplayForm(java.net.URL)
but file: URLs
not having a fragment are displayed as plain file names.public static String toDisplayForm(URL url)
java.net.URL.toExternalForm
except that returned
string may contain non-ASCII characters and that, if specified URL
contains a password, the characters of this password are replaced by
'*'.
Example: returns ftp://jjc%40acme.com:******@ftp.acme.com/pub/My%20report.doc for ftp://jjc%40acme.com:s%25same@ftp.acme.com/pub/My%20report.doc.
url
- a hierarchical URL possibly having a fragment
and a query stringURL.toExternalForm
if specified
URL is opaque ("jar:" URLs are opaque).public static String toShortLabel(URL url, int maxLength)
toLabel(java.net.URL)
except that the returned string is made
shorter than specified length (when possible). This function is useful
to display the recently opened URLs in the File menu of an
application.public static String toShortDisplayForm(URL url, int maxLength)
toDisplayForm(java.net.URL)
except that the returned string is made
shorter than specified length (when possible). This function is useful
to display the recently opened URLs in the File menu of an
application.
Supports "jar:" URLs, but returns other opaque URLs as is.
public static boolean exists(URL url)
exists(url, true, -1)
.public static boolean exists(URL url, boolean followRedirects, int timeout) throws IOException
This method treats "file:
" URLs as a special, optimized,
case.
url
- the URL to be testedfollowRedirects
- if true
, follow redirections,
("301: Moved Permanently", "302: Temporary Redirect") including
very common http
to https
ones.timeout
- specifies both connect and read timeout values
in milliseconds. 0 means: infinite timeout.
A negative value means: default value.true
if specified URL corresponds to an existing
resource; returns false
otherwise.IOException
- if there is an I/O problempublic static long lastModified(URL url)
lastModified(url, true, -1)
.public static long lastModified(URL url, boolean followRedirects, int timeout) throws IOException
This method treats "file:
" URLs as a special, optimized,
case.
url
- the URL to be testedfollowRedirects
- if true
, follow redirections,
("301: Moved Permanently", "302: Temporary Redirect") including
very common http
to https
ones.timeout
- specifies both connect and read timeout values
in milliseconds. 0 means: infinite timeout.
A negative value means: default value.IOException
- if there is an I/O problempublic static URLConnection checkHttpConnection(URLConnection connection, boolean followRedirects) throws IOException
No effect when specified connection is not a
HttpURLConnection
(e.g. a JarURLConnection
).
connection
- HTTP connection to be checkedfollowRedirects
- if true
, follow redirections,
("301: Moved Permanently", "302: Temporary Redirect") including
very common http
to https
ones.IOException
- when specified HTTP connection returned
a code other than "200 OK". The message of the exception contains
information about what happened.public static byte[] loadBytes(URL url) throws IOException
loadBytes(url, true, -1)
.IOException
public static byte[] loadBytes(URL url, boolean followRedirects, int timeout) throws IOException
url
- the URL of the binary datafollowRedirects
- if true
, follow redirections,
("301: Moved Permanently", "302: Temporary Redirect") including
very common http
to https
ones.timeout
- specifies both connect and read timeout values
in milliseconds. 0 means: infinite timeout.
A negative value means: default value.IOException
- if there is an I/O problempublic static int copyFile(String srcLocation, String dstLocation) throws IllegalArgumentException, IOException
srcLocation
- URL of the source file in string form.
If relative, this location is relative to the current working directory.dstLocation
- URL of the destination file in string form.
If relative, this location is relative to the current working directory.IllegalArgumentException
- if srcLocation
cannot be parsed as an URL or if dstLocation
cannot be parsed as a "file:" URL.IOException
- if an I/O problem occurspublic static String loadString(URL url) throws IOException
loadString(url, null, true, -1)
.IOException
public static String loadString(URL url, String charset) throws IOException
loadString(url, charset, true, -1)
.IOException
public static String loadString(URL url, String charset, boolean followRedirects, int timeout) throws IOException
url
- the URL of the text resourcecharset
- the IANA charset of the text source if known; specifying
null
means detect it using the content type obtained from
the connectionfollowRedirects
- if true
, follow redirections,
("301: Moved Permanently", "302: Temporary Redirect") including
very common http
to https
ones.timeout
- specifies both connect and read timeout values
in milliseconds. 0 means: infinite timeout.
A negative value means: default value.IOException
- if there is an I/O problempublic static String contentTypeToCharset(String contentType)
contentType
- a content type (AKA media type) possibly having a
charset parameternull
otherwisepublic static String contentTypeToMedia(String contentType)
contentType
- the content type to be parsednull
otherwise.public static String normalizeContentType(String contentType, String defaultCharset)
Example: returns text/html;charset=iso-8859-1 for text/html; charset="ISO-8859-1".
contentType
- content type to be normalizeddefaultCharset
- charset to add as a parameter to the content type
when this parameter is absent. May be null
.null
if specified content type is malformed.sameContentType(java.lang.String, java.lang.String, java.lang.String)
public static boolean sameContentType(String ct1, String ct2, String defaultCharset)
Examples:
true
for
text/html; charset=ISO-8859-1 and
text/html;charset="iso-8859-1".
false
for
text/html; charset=ISO-8859-1 and text/html.
ct1
- content type to be testedct2
- content type to be testeddefaultCharset
- charset to add as a parameter to a content type
when this parameter is absent. May be null
.true
if specified content types are identical;
false
otherwisenormalizeContentType(java.lang.String, java.lang.String)
public static URLConnection openConnectionNoCache(URL url) throws IOException
url.openConnection
except that the accessed
resource may not be a cached copy.url
- URL for which an URLConnection must be openedIOException
- if URLConnection cannot be openedopenStreamNoCache(URL)
public static InputStream openStreamNoCache(URL url) throws IOException
url.openStream
except that the accessed
resource may not be a cached copy.url
- URL for which an input stream must be openedIOException
- if the input stream cannot be openedopenConnectionNoCache(URL)
public static URLConnection openConnectionUseCache(URL url) throws IOException
url.openConnection
except that the accessed
resource may be a cached copy.url
- URL for which an URLConnection must be openedIOException
- if URLConnection cannot be openedopenStreamUseCache(URL)
public static InputStream openStreamUseCache(URL url) throws IOException
url.openStream
except that the accessed
resource may be a cached copy.url
- URL for which an input stream must be openedIOException
- if the input stream cannot be openedopenConnectionUseCache(URL)