This class provides a robust and exception-free way to build, parse, and handle URLs for HTTP and HTTPS protocols. It
avoids checked exceptions by returning null or throwing an IllegalArgumentException for invalid URLs.
Instances are immutable and support encoding and decoding of URL components.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe character set for encoding form data.static final StringThe character set for encoding fragments.static final StringThe character set for encoding URI fragments.static final StringThe character set for encoding passwords.static final StringThe character set for encoding path segments.static final StringThe character set for encoding URI path segments.static final StringThe character set for encoding query components.static final StringThe character set for encoding URI query components.static final StringThe character set for re-encoding query components.static final StringThe character set for encoding query parameters.static final StringThe character set for encoding usernames. -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringcanonicalize(String input, int pos, int limit, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) Canonicalizes a string by encoding characters from a given set.static Stringcanonicalize(String input, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly) Canonicalizes a string with default UTF-8 encoding.static Stringcanonicalize(String input, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) Canonicalizes a string with default UTF-8 encoding.static voidcanonicalize(org.miaixz.bus.core.io.buffer.Buffer out, String input, int pos, int limit, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) Canonicalizes a string into a buffer.static intdefaultPort(String scheme) Returns the default port for a given scheme.Returns the encoded fragment.Returns the encoded password.Returns the encoded path.Returns the list of encoded path segments.Returns the encoded query string.Returns the encoded username.booleanCompares this URL to another object for equality.fragment()Returns the decoded fragment.static UnoUrlBuilds aUnoUrlinstance from a URL string.static UnoUrlBuilds aUnoUrlinstance from aURIobject.static UnoUrlBuilds aUnoUrlinstance from aURLobject.inthashCode()Computes the hash code for this URL.host()Returns the hostname.booleanisHttps()Returnstrueif this URL uses the HTTPS scheme.Creates a new builder initialized with the components of this URL.newBuilder(String link) Creates a new builder for a relative link.static UnoUrlParses a URL string into aUnoUrlinstance.password()Returns the decoded password.Returns the list of decoded path segments.intpathSize()Returns the number of path segments.static StringpercentDecode(String encoded, boolean plusIsSpace) Decodes a percent-encoded string.static StringpercentDecode(String encoded, int pos, int limit, boolean plusIsSpace) Decodes a percent-encoded string within a specified range.static voidpercentDecode(org.miaixz.bus.core.io.buffer.Buffer out, String encoded, int pos, int limit, boolean plusIsSpace) Decodes a percent-encoded string into a buffer.static booleanpercentEncoded(String encoded, int pos, int limit) Checks if a string is percent-encoded.intport()Returns the port number.query()Returns the decoded query string.queryParameter(String name) Returns the first value for the given query parameter name.queryParameterName(int index) Returns the name of the query parameter at the given index.Returns a set of all query parameter names.queryParameterValue(int index) Returns the value of the query parameter at the given index.queryParameterValues(String name) Returns all values for the given query parameter name.intReturns the number of query parameters.redact()Returns a new URL with sensitive information redacted.Resolves a relative link against this URL.scheme()Returns the scheme of this URL.toString()Returns the string representation of this URL.uri()Converts thisUnoUrlto aURIobject.url()Converts thisUnoUrlto aURLobject.username()Returns the decoded username.
-
Field Details
-
USERNAME_ENCODE_SET
The character set for encoding usernames.- See Also:
-
PASSWORD_ENCODE_SET
The character set for encoding passwords.- See Also:
-
PATH_SEGMENT_ENCODE_SET
The character set for encoding path segments.- See Also:
-
PATH_SEGMENT_ENCODE_SET_URI
The character set for encoding URI path segments.- See Also:
-
QUERY_ENCODE_SET
The character set for encoding query parameters.- See Also:
-
QUERY_COMPONENT_REENCODE_SET
The character set for re-encoding query components.- See Also:
-
QUERY_COMPONENT_ENCODE_SET
The character set for encoding query components.- See Also:
-
QUERY_COMPONENT_ENCODE_SET_URI
The character set for encoding URI query components.- See Also:
-
FORM_ENCODE_SET
The character set for encoding form data.- See Also:
-
FRAGMENT_ENCODE_SET
The character set for encoding fragments.- See Also:
-
FRAGMENT_ENCODE_SET_URI
The character set for encoding URI fragments.- See Also:
-
-
Method Details
-
defaultPort
Returns the default port for a given scheme.Returns 80 for "http", 443 for "https", and -1 for other schemes.
- Parameters:
scheme- The scheme name.- Returns:
- The default port number.
-
parse
Parses a URL string into aUnoUrlinstance.Returns a
UnoUrlinstance if the URL is well-formed, or null otherwise.- Parameters:
url- The URL string.- Returns:
- A
UnoUrlinstance or null.
-
get
Builds aUnoUrlinstance from a URL string.Throws an
IllegalArgumentExceptionif the URL is not well-formed.- Parameters:
url- The URL string.- Returns:
- A
UnoUrlinstance. - Throws:
IllegalArgumentException- if the URL is not well-formed.
-
get
Builds aUnoUrlinstance from aURLobject.Only supports http and https schemes. Returns null for other schemes.
- Parameters:
url- TheURLobject.- Returns:
- A
UnoUrlinstance or null.
-
get
Builds aUnoUrlinstance from aURIobject.- Parameters:
uri- TheURIobject.- Returns:
- A
UnoUrlinstance or null.
-
percentDecode
Decodes a percent-encoded string.- Parameters:
encoded- The encoded string.plusIsSpace- Whether to decode plus signs as spaces.- Returns:
- The decoded string.
-
percentDecode
Decodes a percent-encoded string within a specified range.- Parameters:
encoded- The encoded string.pos- The starting position.limit- The ending position.plusIsSpace- Whether to decode plus signs as spaces.- Returns:
- The decoded string.
-
percentDecode
public static void percentDecode(org.miaixz.bus.core.io.buffer.Buffer out, String encoded, int pos, int limit, boolean plusIsSpace) Decodes a percent-encoded string into a buffer.- Parameters:
out- The output buffer.encoded- The encoded string.pos- The starting position.limit- The ending position.plusIsSpace- Whether to decode plus signs as spaces.
-
percentEncoded
Checks if a string is percent-encoded.- Parameters:
encoded- The encoded string.pos- The starting position.limit- The ending position.- Returns:
trueif the string is percent-encoded.
-
canonicalize
public static String canonicalize(String input, int pos, int limit, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) Canonicalizes a string by encoding characters from a given set.- Parameters:
input- The input string.pos- The starting position.limit- The ending position.encodeSet- The set of characters to encode.alreadyEncoded- Whether the string is already encoded.strict- Whether to use strict encoding.plusIsSpace- Whether to encode plus signs as spaces.asciiOnly- Whether to limit to ASCII characters.charset- The character set to use (null for UTF-8).- Returns:
- The canonicalized string.
-
canonicalize
public static void canonicalize(org.miaixz.bus.core.io.buffer.Buffer out, String input, int pos, int limit, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) Canonicalizes a string into a buffer.- Parameters:
out- The output buffer.input- The input string.pos- The starting position.limit- The ending position.encodeSet- The set of characters to encode.alreadyEncoded- Whether the string is already encoded.strict- Whether to use strict encoding.plusIsSpace- Whether to encode plus signs as spaces.asciiOnly- Whether to limit to ASCII characters.charset- The character set to use (null for UTF-8).
-
canonicalize
public static String canonicalize(String input, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly, Charset charset) Canonicalizes a string with default UTF-8 encoding.- Parameters:
input- The input string.encodeSet- The set of characters to encode.alreadyEncoded- Whether the string is already encoded.strict- Whether to use strict encoding.plusIsSpace- Whether to encode plus signs as spaces.asciiOnly- Whether to limit to ASCII characters.charset- The character set to use (null for UTF-8).- Returns:
- The canonicalized string.
-
canonicalize
public static String canonicalize(String input, String encodeSet, boolean alreadyEncoded, boolean strict, boolean plusIsSpace, boolean asciiOnly) Canonicalizes a string with default UTF-8 encoding.- Parameters:
input- The input string.encodeSet- The set of characters to encode.alreadyEncoded- Whether the string is already encoded.strict- Whether to use strict encoding.plusIsSpace- Whether to encode plus signs as spaces.asciiOnly- Whether to limit to ASCII characters.- Returns:
- The canonicalized string.
-
url
Converts thisUnoUrlto aURLobject.- Returns:
- The
URLobject. - Throws:
RuntimeException- if the URL is malformed.
-
uri
Converts thisUnoUrlto aURIobject.Note:
URIis more strict thanUnoUrland may escape or remove certain characters (like whitespace in fragments). It is recommended to avoid usingURIdirectly to prevent differences in server interpretation.- Returns:
- The
URIobject. - Throws:
RuntimeException- if the URI syntax is invalid.
-
scheme
Returns the scheme of this URL.- Returns:
- The scheme (http or https).
-
isHttps
public boolean isHttps()Returnstrueif this URL uses the HTTPS scheme.- Returns:
trueif the scheme is HTTPS.
-
encodedUsername
Returns the encoded username.- Returns:
- The encoded username, or an empty string if not set.
-
username
Returns the decoded username.- Returns:
- The decoded username.
-
encodedPassword
Returns the encoded password.- Returns:
- The encoded password, or an empty string if not set.
-
password
Returns the decoded password.- Returns:
- The decoded password.
-
host
Returns the hostname.- Returns:
- The hostname, which can be a regular hostname, an IPv4 address, an IPv6 address, or an encoded IDN.
-
port
public int port()Returns the port number.- Returns:
- The port number.
-
pathSize
public int pathSize()Returns the number of path segments.- Returns:
- The number of path segments.
-
encodedPath
Returns the encoded path.- Returns:
- The encoded path of the URL.
-
encodedPathSegments
Returns the list of encoded path segments.- Returns:
- The list of encoded path segments.
-
pathSegments
Returns the list of decoded path segments.- Returns:
- The list of decoded path segments.
-
encodedQuery
Returns the encoded query string.- Returns:
- The encoded query string, or null if no query is present.
-
query
Returns the decoded query string.- Returns:
- The decoded query string, or null if no query is present.
-
querySize
public int querySize()Returns the number of query parameters.- Returns:
- The number of query parameters.
-
queryParameter
Returns the first value for the given query parameter name.- Parameters:
name- The name of the query parameter.- Returns:
- The value of the query parameter, or null if not found.
-
queryParameterNames
Returns a set of all query parameter names.- Returns:
- An unmodifiable set of query parameter names.
-
queryParameterValues
Returns all values for the given query parameter name.- Parameters:
name- The name of the query parameter.- Returns:
- An unmodifiable list of query parameter values.
-
queryParameterName
Returns the name of the query parameter at the given index.- Parameters:
index- The index of the query parameter.- Returns:
- The name of the query parameter.
- Throws:
IndexOutOfBoundsException- if the index is out of range.
-
queryParameterValue
Returns the value of the query parameter at the given index.- Parameters:
index- The index of the query parameter.- Returns:
- The value of the query parameter.
- Throws:
IndexOutOfBoundsException- if the index is out of range.
-
encodedFragment
Returns the encoded fragment.- Returns:
- The encoded fragment, or null if no fragment is present.
-
fragment
Returns the decoded fragment.- Returns:
- The decoded fragment, or null if no fragment is present.
-
redact
Returns a new URL with sensitive information redacted.- Returns:
- A new URL string with the username and password removed.
-
resolve
Resolves a relative link against this URL.- Parameters:
link- The relative link.- Returns:
- The resolved
UnoUrlinstance, or null if the link is invalid.
-
newBuilder
Creates a new builder initialized with the components of this URL.- Returns:
- A new builder instance.
-
newBuilder
Creates a new builder for a relative link.- Parameters:
link- The relative link.- Returns:
- A new builder instance, or null if the link is invalid.
-
equals
Compares this URL to another object for equality. -
hashCode
public int hashCode()Computes the hash code for this URL. -
toString
Returns the string representation of this URL.
-