Class URLUtil
- java.lang.Object
-
- com.xmlmind.util.URLUtil
-
public final class URLUtil extends Object
A collection of utility functions (static methods) operating on URLs.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 "/".
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classURLUtil.URLComparatorCompares two URLs by theirexternal forms.
-
Field Summary
Fields Modifier and Type Field Description static URLUtil.URLComparatorCOMPARATORA ready-to-use URLComparator.static URL[]EMPTY_LISTA ready-to-use empty list of URLs.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static URLConnectioncheckHttpConnection(URLConnection connection, boolean followRedirects)Establish specified HTTP connection and check whether it returns a "200 OK".static StringcontentTypeToCharset(String contentType)Returns the value of the charset parameter possibly found in specified content type.static StringcontentTypeToMedia(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 intcopyFile(String srcLocation, String dstLocation)Copies the contents of specified URL to specified "file:" URL.static URLcreateURL(String spec)Same asnew URL(spec), except that non-ASCII characters and other illegal characters such as spaces possibly found inspecare %HH-encoded.static URLcreateURL(URL context, String spec)Same asnew URL(context, spec), except that non-ASCII characters and other illegal characters such as spaces possibly found inspecare %HH-encoded.static URLcreateURL(URL context, String spec, URLStreamHandler handler)Same asnew URL(context, spec, handler), except that non-ASCII characters and other illegal characters such as spaces possibly found inspecare %HH-encoded.static booleanexists(URL url)Equivalent toexists(url, true, -1).static booleanexists(URL url, boolean followRedirects, int timeout)Tests whether specified URL corresponds to an existing resource.static StringgetBaseName(URL url)Same asgetRawBaseName(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).static StringgetExtension(URL url)Same asgetRawExtension(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).static StringgetFragment(URL url)Same asURL.getRefexcept that the returned value is decoded usingURIComponent.decode(java.lang.String).static URLgetParent(URL url)Returns the parent of specified URL, if any.static StringgetPath(URL url)Same asgetRawPath(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).static StringgetRawBaseName(URL url)Returns the raw (that is, possibly containing %HH escapes) basename part of the path, if specified URL has a path.static StringgetRawExtension(URL url)Returns the raw (that is, possibly containing %HH escapes) extension of the path, if specified URL has a path.static StringgetRawPath(URL url)Returns the raw (that is, possibly containing %HH escapes) path, if specified URL has a path.static StringgetRawRelativePath(URL url, URL base)Returns the path of specified URL relative to specified base URL.static StringgetRawUserName(URL url)Returns the raw (that is, possibly containing %HH escapes) user name, if a user info is found in specified URL.static StringgetRawUserPassword(URL url)Returns the raw (that is, possibly containing %HH escapes) user password, if a user info is found in specified URL.static URLgetRoot(URL url)Returns the root of specified URL.static StringgetUserName(URL url)Same asgetRawUserName(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).static StringgetUserPassword(URL url)Same asgetRawUserPassword(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).static booleanisAncestorOf(URL ancestorURL, URL url)Tests whether first specified URL is an ancestor of second specified URL.static booleanisDataURL(URL url)Returnstrueif specified URL is adata:URL, otherwise returnsfalsestatic booleanisFileURL(URL url)Returnstrueif specified URL is afile:URL, otherwise returnsfalsestatic booleanisJarURL(URL url)Returnstrueif specified URL is ajar:URL, otherwise returnsfalsestatic longlastModified(URL url)Equivalent tolastModified(url, true, -1).static longlastModified(URL url, boolean followRedirects, int timeout)Returns the date of the resource having specified URL.static byte[]loadBytes(URL url)Equivalent toloadBytes(url, true, -1).static byte[]loadBytes(URL url, boolean followRedirects, int timeout)Loads the content of an URL containing binary data.static StringloadString(URL url)Equivalent toloadString(url, null, true, -1).static StringloadString(URL url, String charset)Equivalent toloadString(url, charset, true, -1).static StringloadString(URL url, String charset, boolean followRedirects, int timeout)Loads the content of an URL containing text.static URLnewURL(String spec)A cover forURL(String).static URLnewURL(String protocol, String host, int port, String file)A cover forURL(String, String, int, String).static URLnewURL(String protocol, String host, int port, String file, URLStreamHandler handler)A cover forURL(String, String, int, String, URLStreamHandler).static URLnewURL(String protocol, String host, String file)A cover forURL(String, String, String).static URLnewURL(URL context, String spec)A cover forURL(URL, String).static URLnewURL(URL context, String spec, URLStreamHandler handler)A cover forURL(URL, String, URLStreamHandler).static StringnormalizeContentType(String contentType, String defaultCharset)Returns a normalized string form for specified content type.static URLConnectionopenConnectionNoCache(URL url)Similar tourl.openConnectionexcept that the accessed resource may not be a cached copy.static URLConnectionopenConnectionUseCache(URL url)Similar tourl.openConnectionexcept that the accessed resource may be a cached copy.static InputStreamopenStreamNoCache(URL url)Similar tourl.openStreamexcept that the accessed resource may not be a cached copy.static InputStreamopenStreamUseCache(URL url)Similar tourl.openStreamexcept that the accessed resource may be a cached copy.static booleansameContentType(String ct1, String ct2, String defaultCharset)Tests whether specified content types are identical.static booleansameRoot(URL url1, URL url2)Returnstrueif specified URLs have the same root.static URLsetExtension(URL url, String extension)Same assetRawExtension(java.net.URL, java.lang.String)except that specified extension is quoted usingURIComponent.quotePath(java.lang.String).static URLsetFragment(URL url, String fragment)Same assetRawFragment(java.net.URL, java.lang.String)except that specified fragment is quoted usingURIComponent.quoteFragment(java.lang.String).static URLsetRawExtension(URL url, String extension)Changes the extension of specified URL to specified extension.static URLsetRawFragment(URL url, String fragment)Changes the fragment of specified URL to specified fragment.static URLsetRawUserInfo(URL url, String userName, String password)Changes the user info of specified URL to specified user info.static URLsetUserInfo(URL url, String userName, String password)Same assetRawUserInfo(java.net.URL, java.lang.String, java.lang.String)except that specified user info is quoted usingURIComponent.quoteUserInfo(java.lang.String).static StringtoDisplayForm(URL url)Same asjava.net.URL.toExternalFormexcept 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 StringtoLabel(URL url)Same astoDisplayForm(java.net.URL)butfile:URLs not having a fragment are displayed as plain file names.static StringtoShortDisplayForm(URL url, int maxLength)Same astoDisplayForm(java.net.URL)except that the returned string is made shorter than specified length (when possible).static StringtoShortLabel(URL url, int maxLength)Same astoLabel(java.net.URL)except that the returned string is made shorter than specified length (when possible).static URLurlOrFile(String path)Equivalent tourlOrFile(path, false, false, null).static URLurlOrFile(String path, boolean checkAbsolute)Equivalent tourlOrFile(path, checkAbsolute, false, null).static URLurlOrFile(String path, boolean checkAbsolute, boolean allowDir)Equivalent tourlOrFile(path, checkAbsolute, allowDir, null).static URLurlOrFile(String path, boolean checkAbsolute, boolean allowDir, URL baseURL)Returns an URL created from specified path.static FileurlToFile(URL url)Converts afile:URL to a File.static URIurlToURI(URL url)Similar tojava.net.URL.toURI()except that this utility will not throw ajava.net.URISyntaxExceptionif the URL spec contains illegal characters such as spaces.
-
-
-
Field Detail
-
EMPTY_LIST
public static final URL[] EMPTY_LIST
A ready-to-use empty list of URLs.
-
COMPARATOR
public static final URLUtil.URLComparator COMPARATOR
A ready-to-use URLComparator.
-
-
Method Detail
-
createURL
public static URL createURL(String spec) throws MalformedURLException
Same asnew URL(spec), except that non-ASCII characters and other illegal characters such as spaces possibly found inspecare %HH-encoded.- Throws:
MalformedURLException- See Also:
URIComponent.encode(java.lang.String)
-
createURL
public static URL createURL(URL context, String spec) throws MalformedURLException
Same asnew URL(context, spec), except that non-ASCII characters and other illegal characters such as spaces possibly found inspecare %HH-encoded.- Throws:
MalformedURLException- See Also:
URIComponent.encode(java.lang.String)
-
createURL
public static URL createURL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException
Same asnew URL(context, spec, handler), except that non-ASCII characters and other illegal characters such as spaces possibly found inspecare %HH-encoded.- Throws:
MalformedURLException- See Also:
URIComponent.encode(java.lang.String)
-
newURL
public static URL newURL(String spec) throws MalformedURLException
A cover forURL(String).- Throws:
MalformedURLException
-
newURL
public static URL newURL(URL context, String spec) throws MalformedURLException
A cover forURL(URL, String).- Throws:
MalformedURLException
-
newURL
public static URL newURL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException
A cover forURL(URL, String, URLStreamHandler).- Throws:
MalformedURLException
-
newURL
public static URL newURL(String protocol, String host, String file) throws MalformedURLException
A cover forURL(String, String, String).- Throws:
MalformedURLException
-
newURL
public static URL newURL(String protocol, String host, int port, String file) throws MalformedURLException
A cover forURL(String, String, int, String).- Throws:
MalformedURLException
-
newURL
public static URL newURL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException
A cover forURL(String, String, int, String, URLStreamHandler).- Throws:
MalformedURLException
-
isFileURL
public static boolean isFileURL(URL url)
Returnstrueif specified URL is afile:URL, otherwise returnsfalse
-
isJarURL
public static boolean isJarURL(URL url)
Returnstrueif specified URL is ajar:URL, otherwise returnsfalse
-
isDataURL
public static boolean isDataURL(URL url)
Returnstrueif specified URL is adata:URL, otherwise returnsfalse
-
urlToFile
public static File urlToFile(URL url)
Converts afile:URL to a File.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".- Parameters:
url- the URL to be converted- Returns:
- an absolute File or
nullifurlcannot be converted to a File (for example, becauseurlis not afile:URL) - See Also:
isFileURL(java.net.URL),FileUtil.fileToURL(java.io.File),urlOrFile(java.lang.String)
-
urlToURI
public static URI urlToURI(URL url)
Similar tojava.net.URL.toURI()except that this utility will not throw ajava.net.URISyntaxExceptionif 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.- Parameters:
url- URL to be converted- Returns:
- converted URI or
nullif this really cannot be done
-
urlOrFile
public static URL urlOrFile(String path)
Equivalent tourlOrFile(path, false, false, null).
-
urlOrFile
public static URL urlOrFile(String path, boolean checkAbsolute)
Equivalent tourlOrFile(path, checkAbsolute, false, null).
-
urlOrFile
public static URL urlOrFile(String path, boolean checkAbsolute, boolean allowDir)
Equivalent tourlOrFile(path, checkAbsolute, allowDir, null).
-
urlOrFile
public static URL urlOrFile(String path, boolean checkAbsolute, boolean allowDir, URL baseURL)
Returns an URL created from specified path. First, this convenience function attempts to convert specified path to an URL. If this fails, specified path is considered to be the name of an existing file or directory. If this filename conforms to specified requirements (checkAbsolute,allowDir), it is converted to an URL usingFileUtil.fileToURL(java.io.File).- Parameters:
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- iftrue, whenpathis a filename,pathmust be absolute or this function will returnnullallowDir- iftrue, whenpathis a filename,pathis allowed to be not only the path of a file but also the path of a directorybaseURL- which base URL to use to resolvepathwhen its a relative URL. May benull.- Returns:
- an URL or
nullif specified path cannot be converted to an URL given specified requirements
-
sameRoot
public static boolean sameRoot(URL url1, URL url2)
Returnstrueif specified URLs have the same root.- See Also:
getRoot(java.net.URL)
-
getRoot
public static URL getRoot(URL url)
Returns the root of specified 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".
- Parameters:
url- a hierachical or "jar:" URL- Returns:
- root of specified URL
-
isAncestorOf
public static boolean isAncestorOf(URL ancestorURL, URL url)
Tests whether first specified URL is an ancestor of second specified URL.- Parameters:
ancestorURL- a hierachical or "jar:" URLurl- a hierachical or "jar:" URL- Returns:
trueifancestorURLis equal to or is an ancestor directory ofurl;falseotherwise.
-
getParent
public static URL getParent(URL url)
Returns the parent of specified URL, if any. Returned URL has a path which ends with '/'.Examples:
- Returns "http://java.sun.com/docs/" for "http://java.sun.com/docs/index.html".
- Returns
nullfor "http://java.sun.com/". - Returns "jar:http://www.foo.com/bar/baz.jar!/COM/foo/" for "jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class".
- Parameters:
url- a hierachical or "jar:" URL- Returns:
- parent of specified URL or
nullfor root URLs. - See Also:
URIComponent.getRawParentPath(String, boolean)
-
getPath
public static String getPath(URL url)
Same asgetRawPath(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).
-
getRawPath
public static String getRawPath(URL url)
Returns the raw (that is, possibly containing %HH escapes) path, if specified URL has a path.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".
- Parameters:
url- a hierachical or "jar:" URL- Returns:
- the raw path or
nullif specified URL has no path (this is not consistent withURL.getPathwhich returns the empty string in such case)
-
getBaseName
public static String getBaseName(URL url)
Same asgetRawBaseName(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).
-
getRawBaseName
public 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.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".
- Parameters:
url- a hierachical or "jar:" URL- Returns:
- basename or
nullif specified URL has no path (this is not consistent withURL.getPathwhich returns the empty string in such case) - See Also:
URIComponent.getRawBaseName(java.lang.String)
-
getExtension
public static String getExtension(URL url)
Same asgetRawExtension(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).
-
getRawExtension
public static String getRawExtension(URL url)
Returns the raw (that is, possibly containing %HH escapes) extension of the path, if specified URL has a path. The extension does not include a leading dot '.'.Example: returns "html" for "http://www.acme.com/index.html".
Example: returns
nullfor "http://www.acme.com/icons/png/".Example: returns "class" for "jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class".
- Parameters:
url- a hierachical or "jar:" URL- Returns:
- extension or
nullif specified URL has no path (this is not consistent withURL.getPathwhich returns the empty string in such case) - See Also:
URIComponent.getRawExtension(java.lang.String)
-
setExtension
public static URL setExtension(URL url, String extension)
Same assetRawExtension(java.net.URL, java.lang.String)except that specified extension is quoted usingURIComponent.quotePath(java.lang.String).
-
setRawExtension
public static URL setRawExtension(URL url, String extension)
Changes the extension of specified URL to specified extension.- Parameters:
url- a hierachical or "jar:" URLextension- new extension. Assumed to have been quoted usingURIComponent.quotePath(java.lang.String). May benullwhich means: remove the extension.- Returns:
- an URL identical to
urlexcept that its extension has been changed or removed.Returns same URL if specified URL has no path or its path ends with '/'.
- See Also:
URIComponent.setRawExtension(java.lang.String, java.lang.String)
-
getFragment
public static String getFragment(URL url)
Same asURL.getRefexcept that the returned value is decoded usingURIComponent.decode(java.lang.String).
-
setFragment
public static URL setFragment(URL url, String fragment)
Same assetRawFragment(java.net.URL, java.lang.String)except that specified fragment is quoted usingURIComponent.quoteFragment(java.lang.String).
-
setRawFragment
public static URL setRawFragment(URL url, String fragment)
Changes the fragment of specified URL to specified fragment.- Parameters:
url- a hierachical or "jar:" URLfragment- new fragment. Assumed to have been quoted usingURIComponent.quoteFragment(java.lang.String). May benullwhich means: remove the fragment.- Returns:
- an URL identical to
urlexcept that its fragment has been changed or removed. Returnsurlas is if specified URL has no path. - See Also:
URIComponent.setRawFragment(java.lang.String, java.lang.String)
-
getUserName
public static String getUserName(URL url)
Same asgetRawUserName(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).
-
getRawUserName
public 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. Returnsnullotherwise.
-
getUserPassword
public static String getUserPassword(URL url)
Same asgetRawUserPassword(java.net.URL)except that the returned value is decoded usingURIComponent.decode(java.lang.String).
-
getRawUserPassword
public 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. Returnsnullotherwise.
-
setUserInfo
public static URL setUserInfo(URL url, String userName, String password)
Same assetRawUserInfo(java.net.URL, java.lang.String, java.lang.String)except that specified user info is quoted usingURIComponent.quoteUserInfo(java.lang.String).
-
setRawUserInfo
public static URL setRawUserInfo(URL url, String userName, String password)
Changes the user info of specified URL to specified user info.- Parameters:
url- a hierachical or "jar:" URLuserName- new username. Assumed to have been quoted usingURIComponent.quoteUserInfo(java.lang.String). May benull, which means: remove user info.password- new password. Assumed to have been quoted usingURIComponent.quoteUserInfo(java.lang.String). May benull, which means: password not specified.- Returns:
- an URL identical to
urlexcept that its user info has been changed or removed.
-
getRawRelativePath
public static String getRawRelativePath(URL url, URL base)
Returns the path of specified URL relative to specified base URL.More precisely returns
relativePathsuch thatnew URL(base, relativePath)equalsurl.- Parameters:
url- a hierarchical or "jar:" URLbase- another hierarchical or "jar:" URL- Returns:
- a relative path possibly followed by the query and fragment
components of
urlorURL.toExternalFormifurlorbasehave no path or ifurlandbasedon't have the same root - See Also:
URIComponent.getRawRelativePath(java.lang.String, java.lang.String)
-
toLabel
public static String toLabel(URL url)
Same astoDisplayForm(java.net.URL)butfile:URLs not having a fragment are displayed as plain file names.
-
toDisplayForm
public static String toDisplayForm(URL url)
Same asjava.net.URL.toExternalFormexcept 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%40xx.com:******@ftp.xx.com/pub/My%20report.docforftp://jjc%40xx.com:s%25same@ftp.xx.com/pub/My%20report.doc.- Parameters:
url- a hierarchical URL possibly having a fragment and a query string- Returns:
- display form or
URL.toExternalFormif specified URL is opaque ("jar:" URLs are opaque).
-
toShortLabel
public static String toShortLabel(URL url, int maxLength)
Same astoLabel(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.
-
toShortDisplayForm
public static String toShortDisplayForm(URL url, int maxLength)
Same astoDisplayForm(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.
-
exists
public static boolean exists(URL url)
Equivalent toexists(url, true, -1).
-
exists
public static boolean exists(URL url, boolean followRedirects, int timeout) throws IOException
Tests whether specified URL corresponds to an existing resource.This method treats "
file:" URLs as a special, optimized, case.- Parameters:
url- the URL to be testedfollowRedirects- iftrue, follow redirections, ("301: Moved Permanently", "302: Temporary Redirect") including very commonhttptohttpsones.timeout- specifies both connect and read timeout values in milliseconds. 0 means: infinite timeout. A negative value means: default value.- Returns:
trueif specified URL corresponds to an existing resource; returnsfalseotherwise.- Throws:
IOException- if there is an I/O problem
-
lastModified
public static long lastModified(URL url)
Equivalent tolastModified(url, true, -1).
-
lastModified
public static long lastModified(URL url, boolean followRedirects, int timeout) throws IOException
Returns the date of the resource having specified URL.This method treats "
file:" URLs as a special, optimized, case.- Parameters:
url- the URL to be testedfollowRedirects- iftrue, follow redirections, ("301: Moved Permanently", "302: Temporary Redirect") including very commonhttptohttpsones.timeout- specifies both connect and read timeout values in milliseconds. 0 means: infinite timeout. A negative value means: default value.- Returns:
- A number of milliseconds since January 1, 1970 GMT. If specified URL does not exist or if this date is unknown, a number which is negative or null.
- Throws:
IOException- if there is an I/O problem
-
checkHttpConnection
public static URLConnection checkHttpConnection(URLConnection connection, boolean followRedirects) throws IOException
Establish specified HTTP connection and check whether it returns a "200 OK".No effect when specified connection is not a
HttpURLConnection(e.g. aJarURLConnection).- Parameters:
connection- HTTP connection to be checkedfollowRedirects- iftrue, follow redirections, ("301: Moved Permanently", "302: Temporary Redirect") including very commonhttptohttpsones.- Throws:
IOException- when specified HTTP connection returned a code other than "200 OK". The message of the exception contains information about what happened.
-
loadBytes
public static byte[] loadBytes(URL url) throws IOException
Equivalent toloadBytes(url, true, -1).- Throws:
IOException
-
loadBytes
public static byte[] loadBytes(URL url, boolean followRedirects, int timeout) throws IOException
Loads the content of an URL containing binary data.- Parameters:
url- the URL of the binary datafollowRedirects- iftrue, follow redirections, ("301: Moved Permanently", "302: Temporary Redirect") including very commonhttptohttpsones.timeout- specifies both connect and read timeout values in milliseconds. 0 means: infinite timeout. A negative value means: default value.- Returns:
- the loaded bytes
- Throws:
IOException- if there is an I/O problem
-
copyFile
public static int copyFile(String srcLocation, String dstLocation) throws IllegalArgumentException, IOException
Copies the contents of specified URL to specified "file:" URL.- Parameters:
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.- Throws:
IllegalArgumentException- ifsrcLocationcannot be parsed as an URL or ifdstLocationcannot be parsed as a "file:" URL.IOException- if an I/O problem occurs
-
loadString
public static String loadString(URL url) throws IOException
Equivalent toloadString(url, null, true, -1).- Throws:
IOException
-
loadString
public static String loadString(URL url, String charset) throws IOException
Equivalent toloadString(url, charset, true, -1).- Throws:
IOException
-
loadString
public static String loadString(URL url, String charset, boolean followRedirects, int timeout) throws IOException
Loads the content of an URL containing text.- Parameters:
url- the URL of the text resourcecharset- the IANA charset of the text source if known; specifyingnullmeans detect it using the content type obtained from the connectionfollowRedirects- iftrue, follow redirections, ("301: Moved Permanently", "302: Temporary Redirect") including very commonhttptohttpsones.timeout- specifies both connect and read timeout values in milliseconds. 0 means: infinite timeout. A negative value means: default value.- Returns:
- the loaded String
- Throws:
IOException- if there is an I/O problem
-
contentTypeToCharset
public static String contentTypeToCharset(String contentType)
Returns the value of the charset parameter possibly found in specified content type. For example, returns "utf-8", when passed "text/html; charset=UTF-8"- Parameters:
contentType- a content type (AKA media type) possibly having a charset parameter- Returns:
- value of the charset parameter (lower case) if any;
nullotherwise
-
contentTypeToMedia
public 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").- Parameters:
contentType- the content type to be parsed- Returns:
- the media type (lower case) if parsing was successful;
nullotherwise.
-
normalizeContentType
public static String normalizeContentType(String contentType, String defaultCharset)
Returns a normalized string form for specified content type.Example: returns
text/html;charset=iso-8859-1fortext/html; charset="ISO-8859-1".- Parameters:
contentType- content type to be normalizeddefaultCharset- charset to add as a parameter to the content type when this parameter is absent. May benull.- Returns:
- normalized string form for specified content type;
nullif specified content type is malformed. - See Also:
sameContentType(java.lang.String, java.lang.String, java.lang.String)
-
sameContentType
public static boolean sameContentType(String ct1, String ct2, String defaultCharset)
Tests whether specified content types are identical.Examples:
- Returns
truefortext/html; charset=ISO-8859-1andtext/html;charset="iso-8859-1". - Returns
falsefortext/html; charset=ISO-8859-1andtext/html.
- Parameters:
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 benull.- Returns:
trueif specified content types are identical;falseotherwise- See Also:
normalizeContentType(java.lang.String, java.lang.String)
- Returns
-
openConnectionNoCache
public static URLConnection openConnectionNoCache(URL url) throws IOException
Similar tourl.openConnectionexcept that the accessed resource may not be a cached copy.- Parameters:
url- URL for which an URLConnection must be opened- Throws:
IOException- if URLConnection cannot be opened- See Also:
openStreamNoCache(URL)
-
openStreamNoCache
public static InputStream openStreamNoCache(URL url) throws IOException
Similar tourl.openStreamexcept that the accessed resource may not be a cached copy.- Parameters:
url- URL for which an input stream must be opened- Returns:
- opened input stream
- Throws:
IOException- if the input stream cannot be opened- See Also:
openConnectionNoCache(URL)
-
openConnectionUseCache
public static URLConnection openConnectionUseCache(URL url) throws IOException
Similar tourl.openConnectionexcept that the accessed resource may be a cached copy.- Parameters:
url- URL for which an URLConnection must be opened- Throws:
IOException- if URLConnection cannot be opened- See Also:
openStreamUseCache(URL)
-
openStreamUseCache
public static InputStream openStreamUseCache(URL url) throws IOException
Similar tourl.openStreamexcept that the accessed resource may be a cached copy.- Parameters:
url- URL for which an input stream must be opened- Returns:
- opened input stream
- Throws:
IOException- if the input stream cannot be opened- See Also:
openConnectionUseCache(URL)
-
-