public class IPAddressString extends java.lang.Object implements java.lang.Comparable<IPAddressString>, java.io.Serializable
This supports a much wider range of address string formats than InetAddress.getByName, supports subnet formats, provides specific error messages, and allows more specific configuration.
You can control all of the supported formats using IPAddressStringParameters.Builder
to build a parameters instance of IPAddressStringParameters
.
When not using the constructor that takes a IPAddressStringParameters
, a default instance of IPAddressStringParameters
is used that is generally permissive.
Subnets are supported:
You can combine these variations, such as 1.*.2-3.4/255.255.255.0
IPv6 is fully supported:
All of the above subnet variations also work for IPv6, whether network prefixes, masks, ranges or wildcards. Similarly, all the the above subnet variations also work for any supported IPv4 format, such as the standard dotted-decimal IPv4 format as well as the inet_aton formats listed below.
This class support all address formats of the C routine inet_pton and the Java method java.net.InetAddress.getByName. This class supports all IPv4 address formats of the C routine inet_aton as follows:
Note that there is ambiguity when supporting both inet_aton octal and dotted-decimal leading zeros, like 010.010.010.010 which can be interpreted as octal or decimal, thus it can be either 8.8.8.8 or 10.10.10.10, with the default behaviour using the latter interpretation
This behaviour can be controlled by IPAddressStringParameters.Builder.getIPv4AddressParametersBuilder()
and
IPv4AddressStringParameters.Builder.allowLeadingZeros(boolean)
Some additional formats:
Not supported:
IPv6 dotted decimal: 1.2.3.4.1.2.3.4.1.2.3.4.1.2.3.4
IPv6 base 85: RFC 1924
IPAddress
address = new IPAddressString
("1.2.3.4").getAddress()
;
If your application takes user input IP addresses, you can validate with:
try {
IPAddress
address = new IPAddressString("1.2.3.4").toAddress()
;
} catch(IPAddressStringException
e) {
//e.getMessage() provides description of validation failure
}
Most address strings can be converted to an IPAddress object using getAddress()
or toAddress()
. In most cases the IP version is determined by the string itself.
There are a few exceptions, cases in which the version is unknown or ambiguous, for which getAddress()
returns null:
#getAddress(IPVersion)
to get an address.#getAddress(IPVersion)
to get an address representing either all IPv4 or all IPv6 addresses.#getAddress(IPVersion)
to get the loopback version of your choice.
The other exception is subnets in which the range of values in a segment of the subnet are not sequential, for which getAddress()
throws IPAddressTypeException because there is no single IPAddress value, there would be many.
An IPAddress instance requires that all segments can be represented as a range of values.
There are only two unusual circumstances when this can occur:
This class is thread-safe. In fact, IPAddressString objects are immutable. An IPAddressString object represents a single IP address representation that cannot be changed after construction. Some of the derived state is created upon demand and cached, such as the derived IPAddress instances.
Modifier and Type | Field and Description |
---|---|
static IPAddressString |
ALL_ADDRESSES |
static IPAddressString |
EMPTY_ADDRESS |
Constructor and Description |
---|
IPAddressString(java.lang.String addr)
Constructs an IPAddressString instance using the given String instance.
|
IPAddressString(java.lang.String addr,
IPAddressStringParameters valOptions) |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(IPAddressString other) |
java.lang.String |
convertToPrefixLength()
Converts this address to a prefix length
|
boolean |
equals(java.lang.Object o)
Two IPAddressString objects are equal if they represent the same set of addresses.
|
IPAddress |
getAddress() |
IPAddress |
getAddress(IPAddress.IPVersion version) |
IPAddress.IPVersion |
getIPVersion() |
java.lang.Integer |
getNetworkPrefixLength() |
IPAddressStringParameters |
getValidationOptions() |
int |
hashCode() |
boolean |
isAllAddresses() |
boolean |
isEmpty()
Returns true if the address is empty (zero-length).
|
boolean |
isIPAddress() |
boolean |
isIPv4()
Returns true if the address is IPv4 (with or without a network prefix, with or without wildcard segments).
|
boolean |
isIPv6()
Returns true if the address is IPv6 (with or without a network prefix, with or without wildcard segments).
|
boolean |
isLoopback() |
boolean |
isMixedIPv6() |
boolean |
isPrefixed() |
boolean |
isPrefixOnly() |
boolean |
isValid() |
boolean |
isZero() |
IPAddress |
toAddress()
Produces the
IPAddress corresponding to this IPAddressString. |
IPAddress |
toAddress(IPAddress.IPVersion version)
Produces the
IPAddress of the specified address version corresponding to this IPAddressString. |
java.lang.String |
toNormalizedString()
provides a unique normalized String representation for the host identified by this HostIdentifierString instance
|
java.lang.String |
toString()
Gives us the original string provided to the constructor.
|
IPAddressString |
toSupernet()
Return an address for the network encompassing this address,
with the network portion of the returned address extending to the furthest segment boundary
located entirely within but not matching the network portion of this address.
|
IPAddressString |
toSupernet(java.lang.Integer prefixLengthDecrement)
Return an address for the network encompassing this address.
|
void |
validate()
Validates this string is a valid address, and if not, throws an exception with a descriptive message indicating why it is not.
|
void |
validateIPv4()
Validates this string is a valid IPv4 address, and if not, throws an exception with a descriptive message indicating why it is not.
|
void |
validateIPv6()
Validates this string is a valid IPv6 address, and if not, throws an exception with a descriptive message indicating why it is not.
|
static void |
validateNetworkPrefix(IPAddress.IPVersion ipVersion,
int networkPrefixLength,
boolean allowPrefixesBeyondAddressSize) |
static int |
validateNetworkPrefixLength(IPAddress.IPVersion ipVersion,
java.lang.CharSequence networkPrefixLength)
Validates that the string has the format "/x" for a valid prefix length x.
|
public static final IPAddressString EMPTY_ADDRESS
public static final IPAddressString ALL_ADDRESSES
public IPAddressString(java.lang.String addr)
addr
- the address in string format, either IPv4 like a.b.c.d or IPv6 like a:b:c:d:e:f:g:h or a:b:c:d:e:f:h.i.j.k or a::b or some other valid IPv4 or IPv6 form.
IPv6 addresses are allowed to terminate with a scope id which starts with a % symbol.
Both types of addresses can terminate with a network prefix value like a.b.c.d/24 or ::/24
Optionally, you can specify just a network prefix value like /24, which represents the associated masks 255.255.255.0/24 or ffff:ff00::/24.
Both IPv4 and IPv6 addresses can terminate with a mask instead of a prefix length, like a.b.c.d/255.0.0.0 or ::/ffff:: If a terminating mask is equivalent to a network prefix, then it will be the same as specifying the prefix, so a.b.c.d/16 is the same as a.b.c.d/255.255.0.0 If a terminating mask is not equivalent to a network prefix, then the mask will simply be applied to the address to produce a single address.
You can also alter the addresses to include ranges using the wildcards * and -, such as 1.*.1-2.3, although this behaviour is not allowed by default, you must provide your own IPAddressStringParameters for this, or you can use DEFAULT_WILDCARD_OPTIONS or DEFAULT_WILDCARD_AND_RANGE_OPTIONS as the validation options supplied to the constructor.
public IPAddressString(java.lang.String addr, IPAddressStringParameters valOptions)
addr
- the address in string format
This constructor allows you to alter the default validation options.
For example, you can alter the validation options to allow ranges using the wildcards * and -, such as 1.*.1-2.3. Wildcards are not allowed in trailing masks.public IPAddressStringParameters getValidationOptions()
public boolean isPrefixed()
public java.lang.Integer getNetworkPrefixLength()
public boolean isValid()
public boolean isIPAddress()
public boolean isAllAddresses()
public boolean isPrefixOnly()
public boolean isEmpty()
public boolean isIPv4()
public boolean isIPv6()
public boolean isMixedIPv6()
public IPAddress.IPVersion getIPVersion()
public boolean isLoopback()
InetAddress.isLoopbackAddress()
public boolean isZero()
public void validateIPv4() throws IPAddressStringException
IPAddressStringException
public void validateIPv6() throws IPAddressStringException
IPAddressStringException
public void validate() throws IPAddressStringException
IPAddressStringException
public static int validateNetworkPrefixLength(IPAddress.IPVersion ipVersion, java.lang.CharSequence networkPrefixLength) throws IPAddressTypeException
ipVersion
- IPv4, IPv6, or null if you do not know in which case it will be assumed that it can be eithernetworkPrefixLength
- the network prefix length integer as a string, eg "24"IPAddressTypeException
- if invalid with an appropriate messagepublic static void validateNetworkPrefix(IPAddress.IPVersion ipVersion, int networkPrefixLength, boolean allowPrefixesBeyondAddressSize) throws IPAddressTypeException
IPAddressTypeException
public int hashCode()
hashCode
in class java.lang.Object
public int compareTo(IPAddressString other)
compareTo
in interface java.lang.Comparable<IPAddressString>
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public IPAddress getAddress(IPAddress.IPVersion version)
public IPAddress getAddress()
public IPAddress toAddress(IPAddress.IPVersion version) throws IPAddressStringException, IPAddressTypeException
IPAddress
of the specified address version corresponding to this IPAddressString.
In most cases the string indicates the address version and calling toAddress()
is sufficient, with a few exceptions.
When this object represents only a network prefix length, specifying the address version allows the conversion to take place to the associated mask for that prefix length.
When this object represents all addresses, specifying the address version allows the conversion to take place to the associated representation of all IPv4 or all IPv6 addresses.
When this object represents the empty string and that string is interpreted as a loopback, then it returns the corresponding loopback address. If empty strings are not interpreted as loopback, null is returned.
When this object represents an ipv4 or ipv6 address, it returns that address if and only if that address matches the provided version.
If the string used to construct this object is an invalid format, or a format that does not match the provided version, then this method throws IPAddressException.
version
- the address version that this address should represent.IPAddressStringException
IPAddressTypeException
- address in proper format cannot be converted to an address: for masks inconsistent with associated address range, or ipv4 mixed segments that cannot be joined into ipv6 segmentspublic IPAddress toAddress() throws IPAddressStringException, IPAddressTypeException
IPAddress
corresponding to this IPAddressString. If this object does not represent a specific IPAddress or a ranged IPAddress, null is returned,
which may be the case if this object represents a network prefix or if it represents the empty address string.
If the string used to construct this object is not a known format (empty string, address, range of addresses, or prefix) then this method throws IPAddressException.
As long as this object represents a valid address (but not necessarily a specific address), this method does not throw.IPAddressStringException
- if the address format is invalidIPAddressTypeException
- address in proper format cannot be converted to an address: for masks inconsistent with associated address range, or ipv4 mixed segments that cannot be joined into ipv6 segmentspublic IPAddressString toSupernet(java.lang.Integer prefixLengthDecrement)
prefixLengthDecrement
- the number to reduce the network bits in order to create a larger network.
If null, then this method has the same behaviour as toSupernet()public IPAddressString toSupernet()
ALL_ADDRESSES
is returned.
If this object is equal to ALL_ADDRESSES
then null is returned.public java.lang.String convertToPrefixLength() throws IPAddressStringException
IPAddressStringException
- if the address is invalidpublic java.lang.String toNormalizedString()
public java.lang.String toString()
getAddress()
/toAddress()
and then use string methods on the address object.toString
in class java.lang.Object