Modifier and Type | Field and Description |
---|---|
static String |
REGEX
A regular expression pattern that matches URL strings.
|
Modifier and Type | Method and Description |
---|---|
static URL |
canonicalize(URL url)
Returns the canonical version of the specified
URL , where redundant
names such as "." and ".." are dereferenced and removed
from the path. |
static URL |
create(String str)
Creates a URL by parsing the given string.
|
static String |
decode(String s)
Decodes a
application/x-www-form-urlencoded string. |
static String |
decode(String s,
Charset charset)
Decodes an
application/x-www-form-urlencoded string using a
specific Charset . |
static String |
decode(String s,
String enc)
Decodes an
application/x-www-form-urlencoded string using a
specific encoding scheme. |
static String |
decodePath(String path)
Returns the URL-decoded path string.
|
static String |
encode(String s)
Translates a string into
application/x-www-form-urlencoded format. |
static String |
encode(String s,
Charset charset)
Translates a string into
application/x-www-form-urlencoded format
using a specific Charset . |
static String |
encode(String s,
String enc)
Translates a string into
application/x-www-form-urlencoded format
using a specific encoding scheme. |
static String |
encodePath(String path)
Returns the URL-encoded path string.
|
static boolean |
exists(URL url)
Tests whether the specified
URL references a resource that exists. |
static URL |
getCanonicalParent(URL url)
|
static String |
getJarPath(URL url)
Returns the path portion of the resource referenced inside a Jar of the
specified
url "Jar URL" (if url is "Jar URL" resembling the
"jar:<url>...!..." semantics), or null if url is
not a "Jar URL". |
static URL |
getJarURL(URL url)
Returns an
URL representing the location of the Jar in url ,
if url is "Jar URL" resembling the "jar:<url>...!..."
semantics; or null if url is not a "Jar URL". |
static long |
getLastModified(URL url)
Returns the last modified timestamp of the resource at the specified
URL . |
static String |
getName(URL url)
Returns the name of the file or directory denoted by the specified
URL . |
static URL |
getParent(URL url)
Returns the URL representing the parent of the specified
URL , or
null if url is null or does not name a parent directory. |
static String |
getShortName(URL url)
Returns the short name of the file or directory denoted by the specified
URL . |
static boolean |
isJar(URL url)
Tests whether the specified
URL references a Jar resource,
otherwise false . |
static boolean |
isLocal(URL url)
Tests whether the specified
URL represents a location that is
either a local file with protocol "jar:file:" , or a local Jar file
with protocol "jar:file:" . |
static boolean |
isLocalFile(URL url)
Tests whether the specified
URL represents a file path. |
static boolean |
isLocalJarFile(URL url)
Tests whether the specified
URL represents a location that is a
local Jar file with protocol "jar:file:" . |
static URL |
toCanonicalURL(String absolutePath)
|
static URL |
toCanonicalURL(String basedir,
String path)
|
static URL |
toCanonicalURL(URL baseURL,
String path)
|
static URL[] |
toURL(File... files)
Converts an array of
File objects into an array of URL
objects. |
static URL |
toURL(String absolutePath)
Returns an
URL created from the specified string, or null
if the specified string is null. |
static URL |
toURL(String basedir,
String path)
|
static URL |
toURL(URL baseURL,
String path)
|
public static final String REGEX
public static URL create(String str)
This convenience factory method works as if by invoking the
URL.URL(String)
constructor; any MalformedURLException
thrown by the constructor is caught and wrapped in a new
IllegalArgumentException
object, which is then thrown.
This method is provided for use in situations where it is known that the
given string is a legal URL, for example for URL constants declared within
in a program, and so it would be considered a programming error for the
string not to parse as such. The constructors, which throw
MalformedURLException
directly, should be used situations where a
URL is being constructed from user input or from some other source that may
be prone to errors.
str
- The string to be parsed into a URL.NullPointerException
- If str
is null
.IllegalArgumentException
- If the given string declares a protocol
that could not be found in a specification string, or if the
string could not be parsed.public static URL[] toURL(File... files)
File
objects into an array of URL
objects. File
objects that are null
will be null
in
the resulting URL[]
array.files
- The array of File
objects.URL
objects.IllegalArgumentException
- If a protocol handler for the URL could
not be found, or if some other error occurred while constructing
the URL.NullPointerException
- If files
is null.public static boolean isLocalFile(URL url)
URL
represents a file path. An URL is
considered a file if its protocol is "file" (case-insensitive), and its
host value is empty or equal to "localhost"
.url
- The URL
.true
if the specified URL
represents a file path;
otherwise false
.NullPointerException
- If url
is null.public static boolean isLocalJarFile(URL url)
URL
represents a location that is a
local Jar file with protocol "jar:file:"
.
The compound protocol definition is unwrapped in order to determine if the
root resource locator in the URL is local. This method then uses
isLocalFile(URL)
to check whether url
is local.
url
- The URL
to test.true
if the specified URL
represents a location
that is local; otherwise false
.NullPointerException
- If url
is null.public static boolean isLocal(URL url)
URL
represents a location that is
either a local file with protocol "jar:file:"
, or a local Jar file
with protocol "jar:file:"
.
URLs with compound protocol definitions, such as "jar:file:"
are
first unwrapped in order to digest the root resource locator in the URL.
This method then uses isLocalFile(URL)
to check whether
url
is local.
url
- The URL
to test.true
if the specified URL
represents a location
that is either a local file with protocol "jar:file:"
, or a
local Jar file with protocol "jar:file:"
; otherwise
false
.NullPointerException
- If url
is null.public static URL toURL(String absolutePath) throws MalformedURLException
URL
created from the specified string, or null
if the specified string is null.
":/"
, the resulting
URL
is created with URL.URL(String)
.'/'
,
then it is prepended to the path.
This method assumes the specified string is an absolute path, and detects
Windows paths using Paths.isAbsoluteLocalWindows(String)
.
absolutePath
- The string from which to create an URL
.URL
created from the specified string, or null
if the specified string is null.MalformedURLException
- If a protocol is specified but is unknown, or
the spec is null, or the parsed URL fails to comply with the
specific syntax of the associated protocol.Paths.isAbsoluteLocalWindows(String)
public static URL toCanonicalURL(String absolutePath) throws MalformedURLException
URL
created from the specified string, or
null
if the specified string is null (".."
and "."
path names are dereferenced in a canonical URL
, and redundant
'/'
path separators are removed).
":/"
, the resulting
URL
is created with URL.URL(String)
.'/'
,
then it is prepended to the path.
This method assumes the specified string is an absolute path, and detects
Windows paths using Paths.isAbsoluteLocalWindows(String)
.
absolutePath
- The string from which to create an URL
.URL
created from the specified string, or
null
if the specified string is null (".."
and
"."
path names are dereferenced in a canonical URL
,
and redundant '/'
path separators are removed).MalformedURLException
- If a protocol is specified but is unknown, or
the spec is null, or the parsed URL fails to comply with the
specific syntax of the associated protocol.canonicalize(URL)
,
Paths.isAbsoluteLocalWindows(String)
public static URL toURL(String basedir, String path) throws MalformedURLException
basedir
- The base directory of the path in the resulting URL
.path
- The child path off of basedir
in the resulting
URL
.URL
created from the specified basedir
parent
directory, and path
child path.MalformedURLException
- If a protocol is specified but is unknown, or
the spec is null, or the parsed URL fails to comply with the
specific syntax of the associated protocol.NullPointerException
- If basedir
or path
is null.toURL(String)
,
Paths.newPath(String,String)
,
Paths.isAbsoluteLocalWindows(String)
public static URL toCanonicalURL(String basedir, String path) throws MalformedURLException
URL
created from the specified basedir
parent directory, and path
child path (".."
and "."
path names are dereferenced in a canonical URL
, and redundant
'/'
path separators are removed).basedir
- The base directory of the path in the resulting URL
.path
- The child path off of basedir
in the resulting
URL
.URL
created from the specified basedir
parent directory, and path
child path (".."
and
"."
path names are dereferenced in a canonical URL
,
and redundant '/'
path separators are removed).MalformedURLException
- If a protocol is specified but is unknown, or
the spec is null, or the parsed URL fails to comply with the
specific syntax of the associated protocol.NullPointerException
- If basedir
or path
is null.toURL(String)
,
canonicalize(URL)
,
Paths.newPath(String,String)
,
Paths.isAbsoluteLocalWindows(String)
public static URL toURL(URL baseURL, String path) throws MalformedURLException
URL
created from the specified baseURL
parent
URL, and path
child path.
If the specified baseURL
contains a query string, this method will
throw a IllegalArgumentException
.
baseURL
- The base URL of the path in the resulting URL
.path
- The child path off of baseURL
in the resulting
URL
.URL
created from the specified baseURL
parent
directory, and path
child path.MalformedURLException
- If a protocol is specified but is unknown, or
the spec is null, or the parsed URL fails to comply with the
specific syntax of the associated protocol.IllegalArgumentException
- If the specified baseURL
contains
a query string.NullPointerException
- If baseURL
or path
is null.public static URL toCanonicalURL(URL baseURL, String path) throws MalformedURLException
URL
created from the specified baseURL
parent URL, and path
child path (".."
and "."
path
names are dereferenced in a canonical URL
, and redundant
'/'
path separators are removed).
If the specified baseURL
contains a query string, this method will
throw a IllegalArgumentException
.
baseURL
- The base URL of the path in the resulting URL
.path
- The child path off of baseURL
in the resulting
URL
.URL
created from the specified baseURL
parent directory, and path
child path (".."
and
"."
path names are dereferenced in a canonical URL
,
and redundant '/'
path separators are removed).MalformedURLException
- If a protocol is specified but is unknown, or
the spec is null, or the parsed URL fails to comply with the
specific syntax of the associated protocol.IllegalArgumentException
- If the specified baseURL
contains
a query string.NullPointerException
- If baseURL
or path
is null.public static URL canonicalize(URL url) throws MalformedURLException
URL
, where redundant
names such as "."
and ".."
are dereferenced and removed
from the path.url
- The URL
.URL
, where redundant
names such as "."
and ".."
are dereferenced and
removed from the path.MalformedURLException
- If a protocol is specified but is unknown, or
the spec is null, or the parsed URL fails to comply with the
specific syntax of the associated protocol.public static boolean exists(URL url)
URL
references a resource that exists.
This method performs the following tests to check for the existence of the
resource at the specified URL
:
URL
is "file"
, this
method converts the URL
to a File
and delegates to
File.exists()
.true
, and
otherwise false
.url
- The URL
to test.true
if the specified URL
references a resource
that exists; otherwise false
.NullPointerException
- If url
is null.public static boolean isJar(URL url)
URL
references a Jar resource,
otherwise false
.url
- The URL
to test.true
if the specified URL
references a Jar
resource; otherwise false
.NullPointerException
- If url
is null.public static URL getJarURL(URL url)
URL
representing the location of the Jar in url
,
if url
is "Jar URL" resembling the "jar:<url>...!..."
semantics; or null
if url
is not a "Jar URL".url
- The URL
.URL
representing the location of the Jar in url
,
if url
is "Jar URL" resembling the
"jar:<url>...!..."
semantics; or null
if
url
is not a "Jar URL".NullPointerException
- If url
is null.public static String getJarPath(URL url)
url
"Jar URL" (if url
is "Jar URL" resembling the
"jar:<url>...!..."
semantics), or null
if url
is
not a "Jar URL".url
- The URL
.url
"Jar URL" (if url
is "Jar URL"
resembling the "jar:<url>...!..."
semantics), or
null
if url
is not a "Jar URL".NullPointerException
- If url
is null.public static String getName(URL url)
URL
. This is just the last name in the name sequence of
url
. If the name sequence of url
is empty, then the empty
string is returned.url
- The URL
.URL
, or the empty string if the name sequence of
url
is empty.NullPointerException
- If url
is null.public static String getShortName(URL url)
URL
. This is just the last name in the name sequence of
url
, with its dot-extension removed if present. If the name
sequence of url
is empty, then the empty string is returned.url
- The URL
.URL
, or the empty string if the name sequence of
url
is empty.NullPointerException
- If url
is null.public static long getLastModified(URL url) throws IOException
URL
. This function works for URLs that point to local files,
resources in jars, and resources behind HTTP/HTTPS connections. For all
other types of URLs, this function throws IllegalArgumentException
.url
- The URL
.URL
.IllegalArgumentException
- If url
does not specify the
"file"
, "http"
, or "https"
protocols.IOException
- If an I/O error has occurred.NullPointerException
- If url
is null.public static URL getParent(URL url)
URL
, or
null
if url
is null or does not name a parent directory.url
- The URL
.URL
, or
null
if url
is null or does not name a parent
directory.Paths.getParent(String)
public static URL getCanonicalParent(URL url)
URL
, or null
if url
is null or does not name a
parent directory (".."
and "."
path names are dereferenced
in a canonical URL
)).url
- The URL
.URL
, or
null
if url
is null or does not name a parent
directory (".."
and "."
path names are dereferenced
in a canonical URL
, and redundant '/'
path
separators are removed).Paths.getParent(String)
public static String encode(String s)
application/x-www-form-urlencoded
format.
This method uses UTF-8 as the character encoding.s
- String
to be translated.String
.public static String encode(String s, String enc)
application/x-www-form-urlencoded
format
using a specific encoding scheme.
This method behaves the same as URLEncoder.encode(String,String)
except that, instead of throwing a UnsupportedEncodingException
if
the named encoding is not supported, this method will instead will instead
throw a UnsupportedOperationException
.
This method is intended purely to remove the need to catch the
UnsupportedEncodingException
if using
URLEncoder.encode(String,String)
directly.
s
- String
to be translated.enc
- The name of a supported character encoding.String
.UnsupportedOperationException
- If the named encoding is not
supported.NullPointerException
- If s
or enc
is null.decode(String,String)
public static String encode(String s, Charset charset)
application/x-www-form-urlencoded
format
using a specific Charset
. This method uses the supplied charset to
obtain the bytes for unsafe characters.
Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilities.
This method is intended purely to remove the need to catch the
UnsupportedEncodingException
if using
URLEncoder.encode(String,String)
directly.
s
- String
to be translated.charset
- The given Charset
.String
.UnsupportedOperationException
- If the named encoding is not
supported.NullPointerException
- If s
or charset
is null.decode(String,Charset)
public static String decode(String s)
application/x-www-form-urlencoded
string. This method
uses UTF-8 as the character encoding.s
- The String
to decode.String
.IllegalArgumentException
- If the implementation encounters illegal
path separators.NullPointerException
- If s
is null.public static String decode(String s, Charset charset)
application/x-www-form-urlencoded
string using a
specific Charset
. The supplied charset is used to determine what
characters are represented by any consecutive sequences of the form
"%xy
".
Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilities.
s
- The String
to decode.charset
- The given Charset
.String
.NullPointerException
- If s
or charset
is null.IllegalArgumentException
- If the implementation encounters illegal
characters.encode(String,Charset)
public static String decode(String s, String enc)
application/x-www-form-urlencoded
string using a
specific encoding scheme.
This method behaves the same as decode(String,Charset)
except
that it will look up the charset using the
given encoding name.
s
- The String
to decode.enc
- The name of a supported encoding.String
.NullPointerException
- If s
or charset
is null.IllegalArgumentException
- If the implementation encounters illegal
characters, or if the given charset name is null.IllegalCharsetNameException
- If the given charset name is illegal.UnsupportedCharsetException
- If no support for the named charset is
available in this instance of the Java virtual machine.encode(String,String)
public static String encodePath(String path)
URL path segments may contain '+'
symbols which should not be
decoded into ' '
. This method delegates to URLEncoder, then
replaces '+'
with "%20"
.
path
- The path to decode.public static String decodePath(String path)
URL path segments may contain '+'
symbols which should not be
decoded into ' '
. This method replaces '+'
with
"%2B"
and delegates to URLDecoder.
path
- The path to decode.Copyright © 2019 LibJ. All rights reserved.