public class HostName extends Object implements HostIdentifierString, Comparable<HostName>
IPAddressString
.
All manners of domain names are supported. When adding a prefix length or mask to a host name string, it is to denote the subnet of the resolved address.
Validation is done separately from DNS resolution to avoid unnecessary DNS lookups.
See rfc 3513, 2181, 952, 1035, 1034, 1123, 5890 or the list of rfcs for IPAddress. For IPv6 addresses in host, see rfc 2732 specifying [] notation and 3986 and 4038 (combining IPv6 [] with prefix or zone) and SMTP rfc 2821 for alternative uses of [] for both IPv4 and IPv6
Modifier and Type | Field and Description |
---|---|
static HostNameParameters |
DEFAULT_VALIDATION_OPTIONS |
static char |
IPV6_END_BRACKET |
static char |
IPV6_START_BRACKET |
static char |
LABEL_SEPARATOR |
static char |
PORT_SEPARATOR |
SEGMENT_VALUE_DELIMITER
Constructor and Description |
---|
HostName(InetAddress inetAddr)
Constructs a host name from an IP address.
|
HostName(InetAddress inetAddr,
Integer prefixLength)
Constructs a host name from an address with prefix length, which can be null.
|
HostName(InetAddress inetAddr,
IPAddressStringParameters addressOptions)
Constructs a host name from an IP address, allowing control over conversion to an IPAddress instance.
|
HostName(InetSocketAddress inetSocketAddr)
Constructs a host name from an InetSocketAddress.
|
HostName(InterfaceAddress interfaceAddr)
Constructs a host name from an InterfaceAddress.
|
HostName(IPAddress addr)
Constructs a host name from an IP address.
|
HostName(IPAddress addr,
int port)
Constructs a host name from an IP address and a port.
|
HostName(String host)
Constructs a host name instance from the given string.
|
HostName(String host,
HostNameParameters options)
Similar to
HostName(String) , but allows you to control which elements are allowed and which are not,
using the given options. |
Modifier and Type | Method and Description |
---|---|
IPAddress |
asAddress()
If this represents an ip address, returns that address.
|
IPAddress |
asAddress(IPAddress.IPVersion version)
Deprecated.
|
IPAddressString |
asAddressString()
If this represents an ip address or represents any valid IPAddressString, returns the corresponding address string.
|
InetAddress |
asInetAddress()
Similar to
toInetAddress() but does not throw, instead returns null whenever not a valid address. |
InetSocketAddress |
asInetSocketAddress()
Returns the InetSocketAddress for this host.
|
InetSocketAddress |
asInetSocketAddress(Function<String,Integer> serviceMapper)
Returns the InetSocketAddress for this host.
|
int |
compareTo(HostName other) |
boolean |
equals(Object o)
Returns true if the given object is a host name and
matches(HostName) this one. |
IPAddress |
getAddress()
If this represents an ip address, returns that address.
|
AddressStringException |
getAddressStringException()
Returns the exception thrown for invalid ipv6 literal or invalid reverse DNS hosts.
|
String |
getHost()
Returns the host string normalized but without port, service, prefix or mask.
|
IPAddress |
getMask()
If a mask was provided with this host name, this returns the resulting mask value.
|
Integer |
getNetworkPrefixLength()
If a prefix length was supplied, either as part of an address or as part of a domain (in which case the prefix applies to any resolved address),
then returns that prefix length.
|
String[] |
getNormalizedLabels()
Returns an array of normalized strings for this host name instance.
|
Integer |
getPort()
If a port was supplied, returns the port, otherwise returns null
|
String |
getService()
If a service name was supplied, returns the service name, otherwise returns null
|
HostNameParameters |
getValidationOptions()
Supplies the validation options used to validate this host name, whether the default or the one supplied with
HostName(String, HostNameParameters) |
int |
hashCode() |
boolean |
isAddress()
Returns whether this host name is a string representing an valid specific IP address or subnet.
|
boolean |
isAddress(IPAddress.IPVersion version)
Deprecated.
|
boolean |
isAddressString()
Returns whether this host name is a string representing an IP address or subnet.
|
boolean |
isAllAddresses()
Whether the address represents the set all all valid IP addresses (as opposed to an empty string, a specific address, a prefix length, or an invalid format).
|
boolean |
isEmpty()
Returns true if the address is empty (zero-length).
|
boolean |
isLocalHost()
Returns whether this host is "localhost"
|
boolean |
isLoopback()
Returns whether this host has the loopback address, such as
[::1] (aka [0:0:0:0:0:0:0:1]) or 127.0.0.1
Also see
isSelf() |
boolean |
isPrefixOnly()
Whether the address represents a valid IP address network prefix (as opposed to an empty string, an address with or without a prefix, or an invalid format).
|
boolean |
isReverseDNS()
Returns whether this host name is a reverse DNS string host name.
|
boolean |
isSelf()
Returns whether this represents a host or address representing the same host.
|
boolean |
isUNCIPv6Literal()
Returns whether this host name is an Uniform Naming Convention IPv6 literal host name.
|
boolean |
isValid()
Returns whether this represents a valid host name or address format.
|
boolean |
matches(HostName host)
Returns whether the given host matches this one.
|
boolean |
resolvesToSelf()
Returns whether this represents, or resolves to,
a host or address representing the same host.
|
IPAddress |
toAddress()
If this represents an IP address, returns that address.
|
IPAddress[] |
toAllAddresses()
Similar to
toAddress() , however in the case where the host is resolved,
this method returns all resolved addresses rather than the primary resolved address. |
InetAddress |
toInetAddress()
Returns the InetAddress associated with this host.
|
String |
toNormalizedString()
Provides a normalized string which is lowercase for host strings, and which is a normalized string for addresses.
|
String |
toString()
Returns the string used to construct the object.
|
void |
validate()
Validates that this string is a valid host name or IP address, and if not, throws an exception with a descriptive message indicating why it is not.
|
public static final char LABEL_SEPARATOR
public static final char IPV6_START_BRACKET
public static final char IPV6_END_BRACKET
public static final char PORT_SEPARATOR
public static final HostNameParameters DEFAULT_VALIDATION_OPTIONS
public HostName(IPAddress addr)
addr
- public HostName(IPAddress addr, int port)
addr
- public HostName(InetSocketAddress inetSocketAddr)
inetSocketAddr
- public HostName(InetAddress inetAddr, Integer prefixLength)
inetAddr
- public HostName(InterfaceAddress interfaceAddr)
interfaceAddr
- public HostName(InetAddress inetAddr)
inetAddr
- public HostName(InetAddress inetAddr, IPAddressStringParameters addressOptions)
inetAddr
- public HostName(String host)
IPAddressString
format is supported.host
- public HostName(String host, HostNameParameters options)
HostName(String)
, but allows you to control which elements are allowed and which are not,
using the given options. The default options used by HostName(String)
are permissive.host
- options
- public HostNameParameters getValidationOptions()
HostName(String, HostNameParameters)
public void validate() throws HostNameException
validate
in interface HostIdentifierString
HostNameException
public boolean isValid()
public boolean resolvesToSelf()
public boolean isSelf()
isLocalHost()
and isLoopback()
public boolean isLocalHost()
public boolean isLoopback()
isSelf()
public InetAddress toInetAddress() throws HostNameException, UnknownHostException
HostNameException
- when validation failsUnknownHostException
- when resolve failspublic String toNormalizedString()
toNormalizedString
in interface HostIdentifierString
public boolean equals(Object o)
matches(HostName)
this one.public String[] getNormalizedLabels()
toNormalizedString()
Ports, service name strings, prefix lengths, and masks are all omitted from the returned array.public String getHost()
public boolean matches(HostName host)
host
- public int compareTo(HostName other)
compareTo
in interface Comparable<HostName>
@Deprecated public boolean isAddress(IPAddress.IPVersion version)
public boolean isAddress()
public boolean isAddressString()
public boolean isAllAddresses()
public boolean isPrefixOnly()
public boolean isEmpty()
public Integer getPort()
public String getService()
public AddressStringException getAddressStringException()
public boolean isUNCIPv6Literal()
public boolean isReverseDNS()
public IPAddressString asAddressString()
toAddress()
or getAddress()
to get the resolved address.public IPAddress asAddress()
This does not resolve addresses or return resolved addresses.
Call toAddress()
or getAddress()
to get the resolved address.
In cases such as IPv6 literals and reverse DNS hosts, you can check the relevant methods isIpv6Literal or isReverseDNS, in which case this method should return the associated address. If this method returns null then an exception occurred when producing the associated address, and that exception is available from getAddressStringException.
@Deprecated public IPAddress asAddress(IPAddress.IPVersion version)
toAddress()
or getAddress()
to get the resolved address.public Integer getNetworkPrefixLength()
public IPAddress getMask()
public InetAddress asInetAddress()
toInetAddress()
but does not throw, instead returns null whenever not a valid address.
This method does not resolve hosts. For that, call toAddress()
and then IPAddress.toInetAddress()
public InetSocketAddress asInetSocketAddress(Function<String,Integer> serviceMapper)
If there is on associated port, then this returns null.
Note that host name strings are not resolved when using this method.
serviceMapper
- maps service name strings to ports.
Returns null when a service string has no mapping, otherwise returns the port for a given service.
You can use a project like netdb to provide a service mapper lambda, https://github.com/jnr/jnr-netdbpublic InetSocketAddress asInetSocketAddress()
If there is on associated port, then this returns null.
Note that host name strings are not resolved when using this method.
public IPAddress[] toAllAddresses() throws UnknownHostException, HostNameException
toAddress()
, however in the case where the host is resolved,
this method returns all resolved addresses rather than the primary resolved address.
In the cases where toAddress()
returns null, this returns an empty array,
and in the cases where this host represents an IP address, this method returns an array of length 1 with that address.UnknownHostException
HostNameException
public IPAddress toAddress() throws UnknownHostException, HostNameException
getAddress()
will simply return null rather than throw those exceptions.
If you wish to get the represented address and avoid DNS resolution, use asAddress()
or asAddressString()
toAddress
in interface HostIdentifierString
UnknownHostException
HostNameException
public IPAddress getAddress()
asAddress()
or asAddressString()
getAddress
in interface HostIdentifierString
public String toString()
toString
in interface HostIdentifierString
toString
in class Object