public class IPAddressString extends java.lang.Object implements HostIdentifierString, java.lang.Comparable<IPAddressString>
This supports a much wider range of address string formats than InetAddress.getByName. It 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 work for IPv6, whether network prefixes, masks, ranges or wildcards. Similarly, all the the above subnet variations 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 former interpretation
This behaviour can be controlled by IPAddressStringParameters.Builder.getIPv4AddressParametersBuilder()
and
IPv4AddressStringParameters.Builder.allowLeadingZeros(boolean)
Some additional formats:
If you have an address in which segments have been delimited with commas, such as "1,2.3.4,5.6", you can parse this with parseDelimitedSegments(String)
which gives an iterator of strings. For "1,2.3.4,5.6" you will iterate through "1.3.4.6", "1.3.5.6", "2.3.4.6" and "2.3.5.6".
You can count the number of elements in such an iterator with countDelimitedAddresses(String)
.
Each string can then be used to construct an IPAddressString.
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(AddressStringException
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 a subnet in which the range of values in a segment of the subnet are not sequential, for which getAddress()
throws IncompatibleAddressException
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.
SEGMENT_VALUE_DELIMITER
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 |
---|---|
IPAddressString |
adjustPrefixBySegment(boolean nextSegment) |
IPAddressString |
adjustPrefixLength(int adjustment) |
int |
compareTo(IPAddressString other) |
java.lang.String |
convertToPrefixLength()
Converts this address to a prefix length
|
static int |
countDelimitedAddresses(java.lang.String str)
Given a string with comma delimiters to denote segment elements, this method will count the possible combinations.
|
boolean |
equals(java.lang.Object o)
Two IPAddressString objects are equal if they represent the same set of addresses.
|
IPAddress |
getAddress()
If this represents an ip address, returns that address.
|
IPAddress |
getAddress(IPAddress.IPVersion version)
Similar to
#toAddress(IPVersion) , but returns null rather than throwing an exception with the address is invalid or does not match the supplied version. |
IPAddress |
getHostAddress()
If this address string was constructed from a host address with prefix length,
then this provides just the host address, rather than the address
provided by
getAddress() that incorporates the prefix. |
IPAddress.IPVersion |
getIPVersion() |
java.lang.Integer |
getNetworkPrefixLength() |
IPAddressStringParameters |
getValidationOptions() |
int |
hashCode() |
boolean |
isAllAddresses() |
boolean |
isBase85IPv6()
If this address string represents an IPv6 address, returns whether the string was base 85
|
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()
If this address string represents an IPv6 address, returns whether the lower 4 bytes were represented as IPv4
|
boolean |
isPrefixed() |
boolean |
isPrefixOnly() |
boolean |
isValid() |
boolean |
isZero() |
static java.util.Iterator<java.lang.String> |
parseDelimitedSegments(java.lang.String str)
Given a string with comma delimiters to denote segment elements, this method will provide an iterator to iterate through the possible combinations.
|
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. |
IPAddress |
toHostAddress()
If this address string was constructed from a host address with prefix,
then this provides just the host address, rather than the address with the prefix
provided by
toAddress() that incorporates the prefix. |
java.lang.String |
toNormalizedString()
provides a normalized String representation for the host identified by this HostIdentifierString instance
|
java.lang.String |
toString()
Gives us the original string provided to the constructor.
|
void |
validate()
Validates that this string is a valid address, and if not, throws an exception with a descriptive message indicating why it is not.
|
void |
validateIPv4()
Validates that 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 that 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 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.
public IPAddressString(java.lang.String addr, IPAddressStringParameters valOptions)
addr
- the address in string format
This constructor allows you to alter the default validation options.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 boolean isBase85IPv6()
public IPAddress.IPVersion getIPVersion()
public boolean isLoopback()
InetAddress.isLoopbackAddress()
public boolean isZero()
public void validateIPv4() throws AddressStringException
AddressStringException
public void validateIPv6() throws AddressStringException
AddressStringException
public void validate() throws AddressStringException
validate
in interface HostIdentifierString
AddressStringException
public static int validateNetworkPrefixLength(IPAddress.IPVersion ipVersion, java.lang.CharSequence networkPrefixLength) throws PrefixLenException
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"IncompatibleAddressException
- if invalid with an appropriate messagePrefixLenException
public static void validateNetworkPrefix(IPAddress.IPVersion ipVersion, int networkPrefixLength, boolean allowPrefixesBeyondAddressSize) throws PrefixLenException
PrefixLenException
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 getHostAddress()
getAddress()
that incorporates the prefix.
Otherwise this returns the same object as getAddress()
.
This method returns null for invalid formats, the equivalent method toHostAddress()
throws exceptions for invalid formats.
AddressNetwork.getPrefixConfiguration()
public IPAddress getAddress(IPAddress.IPVersion version)
#toAddress(IPVersion)
, but returns null rather than throwing an exception with the address is invalid or does not match the supplied version.public IPAddress getAddress()
This method will return null for invalid formats. Use toAddress()
for an equivalent method that throws exceptions for invalid formats.
If you have a prefix address and you wish to get only the host without the prefix, use getHostAddress()
getAddress
in interface HostIdentifierString
public IPAddress toHostAddress() throws AddressStringException, IncompatibleAddressException
toAddress()
that incorporates the prefix.
Otherwise this returns the same object as toAddress()
This method throws exceptions for invalid formats, the equivalent method getHostAddress()
will simply return null in such cases.AddressStringException
IncompatibleAddressException
public IPAddress toAddress(IPAddress.IPVersion version) throws AddressStringException, IncompatibleAddressException
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 AddressStringException
.
version
- the address version that this address should represent.AddressStringException
IncompatibleAddressException
- 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 AddressStringException, IncompatibleAddressException
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 only 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 AddressStringException
.
An equivalent method that does not throw exception for invalid formats is getAddress()
If you have a prefixed address and you wish to get only the host rather than the address with the prefix, use toHostAddress()
As long as this object represents a valid address (but not necessarily a specific address), this method does not throw.
toAddress
in interface HostIdentifierString
AddressStringException
- if the address format is invalidIncompatibleAddressException
- if a valid address string representing multiple addresses cannot be represented
This happens only for masks inconsistent with the associated address ranges, or ranges in ipv4 mixed segments that cannot be joined into ipv6 segmentspublic IPAddressString adjustPrefixBySegment(boolean nextSegment)
public IPAddressString adjustPrefixLength(int adjustment)
public static int countDelimitedAddresses(java.lang.String str)
For example, given "1,2.3.4,5.6" this method will return 4 for the possible combinations: "1.3.4.6", "1.3.5.6", "2.3.4.6" and "2.3.5.6"
str
- public static java.util.Iterator<java.lang.String> parseDelimitedSegments(java.lang.String str)
For example, given "1,2.3.4,5.6" this will iterate through "1.3.4.6", "1.3.5.6", "2.3.4.6" and "2.3.5.6"
Another example: "1-2,3.4.5.6" will iterate through "1-2.4.5.6" and "1-3.4.5.6".
This method will not validate strings. Each string produced can be validated using an instance of IPAddressString.
str
- public java.lang.String convertToPrefixLength() throws AddressStringException
AddressStringException
- if the address is invalidpublic java.lang.String toNormalizedString()
HostIdentifierString
toNormalizedString
in interface HostIdentifierString
public java.lang.String toString()
getAddress()
/toAddress()
and then use string methods on the address object.toString
in class java.lang.Object