Example usage:
HostAndPort hp = HostAndPort.fromString("[2001:db8::1]")
.withDefaultPort(80)
.requireBracketsForIPv6();
hp.getHost(); // returns "2001:db8::1"
hp.getPort(); // returns 80
hp.toString(); // returns "[2001:db8::1]:80"
Here are some examples of recognized formats:
getHost() omits brackets
getHost() omits brackets
requireBracketsForIPv6() to prohibit this
Note that this is not an exhaustive list, because these methods are only concerned with brackets, colons, and port numbers. Full validation of the host field (if desired) is the caller's responsibility.
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | hasBracketlessColons
True if the parsed host has colons, but no surrounding brackets. |
| private final String | host
Hostname, IPv4/IPv6 literal, or unvalidated nonsense. |
| private static final int | NO_PORT
Magic value indicating the absence of a port number. |
| private final int | port
Validated port number in the range [0..65535], or NO_PORT |
| private static final long |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | equals(Object
the reference object with which to compare. other)Overrides java. |
| public static HostAndPort | |
| public static HostAndPort | |
| public static HostAndPort | Returns: if parsing was successful, a populated HostAndPort object.the input string to parse. hostPortString)Split a freeform string into a host and port, without strict validation. |
| public String | getHost()
Returns the portion of this |
| private static String[] | Returns: an array with 2 strings: host and port, in that order.the full bracketed host-port specification. Port might not be specified. hostPortString)Parses a bracketed host-port string, throwing IllegalArgumentException if parsing fails. |
| public int | Returns: a validated port number, in the range [0..65535]Get the current port number, failing if no port is defined. |
| public int | getPortOrDefault(int defaultPort)
Returns the current port number, with a default if no port is defined. |
| public int | |
| public boolean | |
| private static boolean | |
| public HostAndPort | Returns: this, to enable chaining of calls.Generate an error if the host might be a non-bracketed IPv6 literal. |
| public String | toString()
Overrides java. |
| public HostAndPort | Returns: a HostAndPort instance, guaranteed to have a defined port.a port number, from [0..65535] defaultPort)Provide a default port if the parsed string contained only a host. |
| hasBracketlessColons | back to summary |
|---|---|
| private final boolean hasBracketlessColons True if the parsed host has colons, but no surrounding brackets. | |
| host | back to summary |
|---|---|
| private final String host Hostname, IPv4/IPv6 literal, or unvalidated nonsense. | |
| NO_PORT | back to summary |
|---|---|
| private static final int NO_PORT Magic value indicating the absence of a port number. | |
| port | back to summary |
|---|---|
| private final int port Validated port number in the range [0..65535], or NO_PORT | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| HostAndPort | back to summary |
|---|---|
| private HostAndPort(String host, int port, boolean hasBracketlessColons) | |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| fromHost | back to summary |
|---|---|
| public static HostAndPort fromHost(String host) Build a HostAndPort instance from a host only. Note Non-bracketed IPv6 literals are allowed. Use
| |
| fromParts | back to summary |
|---|---|
| public static HostAndPort fromParts(String host, int port) Build a HostAndPort instance from separate host and port values. Note Non-bracketed IPv6 literals are allowed. Use
| |
| fromString | back to summary |
|---|---|
| public static HostAndPort fromString(String hostPortString) Split a freeform string into a host and port, without strict validation. Note that the host-only formats will leave the port field undefined. You can use
| |
| getHost | back to summary |
|---|---|
| public String getHost() Returns the portion of this A successful parse does not imply any degree of sanity in this field. For additional
validation, see the
| |
| getHostAndPortFromBracketedHost | back to summary |
|---|---|
| private static String[] getHostAndPortFromBracketedHost(String hostPortString) Parses a bracketed host-port string, throwing IllegalArgumentException if parsing fails.
| |
| getPort | back to summary |
|---|---|
| public int getPort() Get the current port number, failing if no port is defined.
| |
| getPortOrDefault | back to summary |
|---|---|
| public int getPortOrDefault(int defaultPort) Returns the current port number, with a default if no port is defined. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| hasPort | back to summary |
|---|---|
| public boolean hasPort() Return true if this instance has a defined port. | |
| isValidPort | back to summary |
|---|---|
| private static boolean isValidPort(int port) Return true for valid port numbers. | |
| requireBracketsForIPv6 | back to summary |
|---|---|
| public HostAndPort requireBracketsForIPv6() Generate an error if the host might be a non-bracketed IPv6 literal. URI formatting requires that IPv6 literals be surrounded by brackets, like "[2001:db8::1]".
Chain this call after Note that this parser identifies IPv6 literals solely based on the presence of a colon. To
perform actual validation of IP addresses, see the
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Rebuild the host:port string, including brackets if necessary.
| |
| withDefaultPort | back to summary |
|---|---|
| public HostAndPort withDefaultPort(int defaultPort) Provide a default port if the parsed string contained only a host. You can chain this after
| |