public abstract class IPAddress extends java.lang.Object implements java.lang.Comparable<IPAddress>, java.io.Serializable
IPAddress objects are immutable and cannot change values. This also makes them thread-safe.
String creation:
There are several public classes used to customize IP address strings.
For single strings from an address or address section, you use IPAddressSection.StringOptions
or IPv6AddressSection.IPv6StringOptions
along with toNormalizedString(IPAddressSection.StringOptions)
.
Or you use one of the methods like toCanonicalString()
which does the same.
For string collections from an address or address section, use IPv4AddressSection.IPv4StringBuilderOptions
, IPv6AddressSection.IPv6StringBuilderOptions
, IPAddressSection.IPStringBuilderOptions
along with toStringCollection(IPAddressSection.IPStringBuilderOptions)
or toStrings(IPAddressSection.IPStringBuilderOptions)
.
Or you use one of the methods toStandardStringCollection()
, toAllStringCollection()
, toStandardStrings()
, toAllStrings()
which does the same.
Modifier and Type | Class and Description |
---|---|
static class |
IPAddress.IPVersion |
Modifier and Type | Field and Description |
---|---|
static IPAddressComparator |
addressComparator |
static IPAddressConverter |
addressConverter |
static char |
PREFIX_LEN_SEPARATOR |
static char |
RANGE_SEPARATOR |
static java.lang.String |
RANGE_SEPARATOR_STR |
static char |
SEGMENT_SQL_SINGLE_WILDCARD |
static java.lang.String |
SEGMENT_SQL_SINGLE_WILDCARD_STR |
static char |
SEGMENT_SQL_WILDCARD |
static java.lang.String |
SEGMENT_SQL_WILDCARD_STR |
static char |
SEGMENT_WILDCARD |
static java.lang.String |
SEGMENT_WILDCARD_STR |
Modifier and Type | Method and Description |
---|---|
static int |
bitCount(IPAddress.IPVersion version) |
static int |
bitsPerSegment(IPAddress.IPVersion version) |
static int |
byteCount(IPAddress.IPVersion version) |
int |
compareTo(IPAddress other) |
boolean |
contains(IPAddress other) |
boolean |
equals(java.lang.Object o)
Two IPAddress objects are equal if they represent the same set of addresses.
|
static IPAddress |
from(byte[] bytes) |
static IPAddress |
from(byte[] bytes,
byte[] bytes2,
java.lang.Integer prefixLength) |
static IPAddress |
from(byte[] bytes,
java.lang.Integer prefixLength) |
static IPAddress |
from(java.net.InetAddress inetAddress) |
abstract java.lang.Iterable<? extends IPAddress> |
getAddresses() |
abstract int |
getBitCount() |
int |
getBitsPerSegment() |
abstract int |
getByteCount() |
int |
getByteIndex(java.lang.Integer networkPrefixLength) |
byte[] |
getBytes() |
int |
getBytesPerSegment() |
java.math.BigInteger |
getCount()
Gets the count of addresses that this address may represent.
|
java.lang.Integer |
getEquivalentPrefix()
Returns a prefix length for which the range of this address can be specified only using the address lower value and the prefix length
If no such prefix exists, returns null.
|
abstract IPAddressSection |
getHostSection()
Generates the host section of the address.
|
abstract IPAddressSection |
getHostSection(int networkPrefixLength)
Generates the host section of the address.
|
IPAddress.IPVersion |
getIPVersion() |
static IPAddress |
getLocalHost() |
static IPAddress |
getLoopback(IPAddress.IPVersion version) |
abstract IPAddress |
getLower()
If this represents an address with ranging values, returns an address representing the lower values of the range.
|
byte[] |
getLowestBytes()
Gets the bytes for the lowest address in the range represented by this address.
|
java.lang.Integer |
getMaskPrefixLength(boolean network)
If this address is equivalent to the mask for a CIDR prefix, it returns that prefix length.
|
void |
getMatchesSQLClause(java.lang.StringBuilder builder,
java.lang.String sqlExpression)
returns a clause for matching this address.
|
void |
getMatchesSQLClause(java.lang.StringBuilder builder,
java.lang.String sqlExpression,
IPAddressSQLTranslator translator)
returns a clause for matching this address.
|
int |
getMaxSegmentValue() |
int |
getMinPrefix()
Returns the smallest CIDR prefix possible (largest network),
such that this address paired with that prefix represents the exact same range of addresses.
|
abstract IPAddressNetwork |
getNetwork() |
java.lang.Integer |
getNetworkPrefixLength() |
abstract IPAddressSection |
getNetworkSection()
Generates the network section of the address if the address is a CIDR prefix, otherwise it generates the entire address as a prefixed address with prefix matching the address bit length.
|
IPAddressSection |
getNetworkSection(int networkPrefixLength)
Generates the network section of the address.
|
abstract IPAddressSection |
getNetworkSection(int networkPrefixLength,
boolean withPrefixLength)
Generates the network section of the address.
|
IPAddressPart[] |
getParts(IPAddressSection.IPStringBuilderOptions options)
Returns all the ways of breaking this address down into segments, as selected.
|
IPAddressSection |
getSection()
Returns the address as an address section comprising all segments in the address.
|
IPAddressSegment |
getSegment(int index) |
abstract int |
getSegmentCount() |
int |
getSegmentIndex(java.lang.Integer networkPrefixLength) |
java.lang.String[] |
getSegmentStrings() |
static java.lang.String[] |
getStandardLoopbackStrings(IPAddress.IPVersion version) |
abstract IPAddress |
getUpper()
If this represents an address with ranging values, returns an address representing the upper values of the range
If this represents an address with a single value in each segment, returns this.
|
int |
hashCode() |
boolean |
isAnyLocal() |
boolean |
isIPv4() |
abstract boolean |
isIPv4Convertible()
Determines whether this address can be converted to IPv4, if not IPv4 already.
|
boolean |
isIPv6() |
abstract boolean |
isIPv6Convertible()
Determines whether an address can be converted to IPv6, if not IPv6 already.
|
abstract boolean |
isLinkLocal() |
abstract boolean |
isLoopback() |
boolean |
isMaskCompatibleWithRange(IPAddress mask,
java.lang.Integer networkPrefixLength)
Check that the range in each segment resulting from the mask is contiguous, otherwise we cannot represent it.
|
abstract boolean |
isMulticast() |
boolean |
isMultiple() |
boolean |
isMultipleByNetworkPrefix() |
boolean |
isPrefixed() |
boolean |
isRangeEquivalentToPrefix() |
boolean |
isSameAddress(IPAddress other) |
abstract boolean |
isSiteLocal() |
boolean |
isZero() |
abstract java.util.Iterator<? extends IPAddress> |
iterator() |
boolean |
matches(IPAddressString otherString) |
static int |
maxSegmentValue(IPAddress.IPVersion version) |
static IPAddressNetwork |
network(IPAddress.IPVersion version) |
static int |
segmentCount(IPAddress.IPVersion version) |
abstract IPAddress[] |
subtract(IPAddress other)
Subtract the give subnet from this subnet, returning an array of sections for the result (the subnets will not be contiguous so an array is required).
|
IPAddressString |
toAddressString()
Generates an IPAddressString object for this IPAddress object.
|
IPAddressPartStringCollection |
toAllStringCollection() |
java.lang.String[] |
toAllStrings()
Produces almost all possible string variations
|
HostName |
toCanonicalHostName()
Does a reverse name lookup to get the canonical host name.
|
java.lang.String |
toCanonicalString()
This produces a canonical string.
|
java.lang.String |
toCanonicalWildcardString()
This produces a string similar to the canonical string but avoids the CIDR prefix.
|
java.lang.String |
toCompressedString()
This produces the shortest valid string for the address.
|
java.lang.String |
toCompressedWildcardString()
This is similar to toNormalizedWildcardString, avoiding the CIDR prefix, but with compression as well.
|
java.lang.String |
toConvertedString()
Returns a mixed string if it represents a convertible IPv4 address, returns the normalized string otherwise.
|
static java.lang.String |
toDelimitedSQLStrs(java.lang.String[] strs) |
java.lang.String |
toFullString()
This produces a string with no compressed segments and all segments of full length,
which is 4 characters for IPv6 segments and 3 characters for IPv4 segments.
|
java.lang.String |
toHexString(boolean withPrefix)
Writes this address as hexadecimal, with or without a preceding 0x prefix
|
HostName |
toHostName()
If this address was resolved from a host, returns that host.
|
java.net.InetAddress |
toInetAddress() |
IPv4Address |
toIPv4()
If this address is IPv4, or can be converted to IPv4, returns that
IPv4Address . |
IPv6Address |
toIPv6() |
java.lang.String |
toNetworkPrefixLengthString()
Returns a string with a CIDR network prefix length if this address has a network prefix length.
|
java.lang.String |
toNormalizedString()
The normalized string returned by this method is consistent with java.net.Inet4Address and java.net.Inet6Address.
|
java.lang.String |
toNormalizedString(IPAddressSection.StringOptions params)
Constructs a string representing this address according to the given parameters
|
java.lang.String |
toNormalizedWildcardString()
This produces a string similar to the normalized string but avoids the CIDR prefix.
|
IPAddress |
toPrefixedEquivalent()
Returns the equivalent CIDR address for which the range of addresses represented
is specified using just a single value and a prefix length in the returned section.
|
IPAddress |
toPrefixedMin()
Constructs an equivalent address with the smallest CIDR prefix possible (largest network),
such that the address represents the exact same range of addresses.
|
abstract java.lang.String |
toReverseDNSLookupString()
Generates the reverse DNS lookup string
For 8.255.4.4 it is 4.4.255.8.in-addr.arpa
For 2001:db8::567:89ab it is b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa
|
java.lang.String |
toSQLWildcardString()
This is the same as the string from toNormalizedWildcardString except that
it uses IPAddress.SEGMENT_SQL_WILDCARD instead of IPAddress.SEGMENT_WILDCARD and also uses IPAddress.SEGMENT_SQL_SINGLE_WILDCARD
|
IPAddressPartStringCollection |
toStandardStringCollection() |
java.lang.String[] |
toStandardStrings()
Returns at most a few dozen string representations:
-mixed (1:2:3:4:5:6:1.2.3.4)
-full compressions (a:0:b:c:d:0:e:f or a::b:c:d:0:e:f or a:0:b:c:d::e:f)
-full leading zeros (000a:0000:000b:000c:000d:0000:000e:000f)
-all uppercase and all lowercase (a::a can be A::A)
-combinations thereof
|
java.lang.String |
toString() |
IPAddressPartStringCollection |
toStringCollection(IPAddressSection.IPStringBuilderOptions options) |
java.lang.String[] |
toStrings(IPAddressSection.IPStringBuilderOptions options)
Rather than using toAllStrings or StandardStrings,
you can use this method to customize the list of strings produced for this address
|
abstract IPAddress |
toSubnet(int networkPrefixLength)
Creates a subnet address using the given CIDR prefix bits.
|
abstract IPAddress |
toSubnet(IPAddress mask)
Creates a subnet address using the given mask.
|
abstract IPAddress |
toSubnet(IPAddress mask,
java.lang.Integer networkPrefixLength)
Creates a subnet address using the given mask.
|
java.lang.String |
toSubnetString()
Produces a consistent subnet string that looks like 1.2.*.* or 1:2::/16
In the case of IPv4, this means that wildcards are used instead of a network prefix when a network prefix has been supplied.
|
IPAddress |
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,
unless the network portion has no bits in which case the same address is returned.
|
IPAddress |
toSupernet(java.lang.Integer prefixLengthDecrement)
Return an address for the network encompassing this address.
|
abstract java.lang.String |
toUNCHostName()
Generates the Microsoft UNC path component for this address
|
public static final char RANGE_SEPARATOR
public static final java.lang.String RANGE_SEPARATOR_STR
public static final char SEGMENT_WILDCARD
public static final java.lang.String SEGMENT_WILDCARD_STR
public static final char SEGMENT_SQL_WILDCARD
public static final java.lang.String SEGMENT_SQL_WILDCARD_STR
public static final char SEGMENT_SQL_SINGLE_WILDCARD
public static final java.lang.String SEGMENT_SQL_SINGLE_WILDCARD_STR
public static final char PREFIX_LEN_SEPARATOR
public static final IPAddressConverter addressConverter
public static final IPAddressComparator addressComparator
public HostName toHostName()
public HostName toCanonicalHostName()
public static IPAddress from(byte[] bytes)
public static IPAddress from(byte[] bytes, java.lang.Integer prefixLength)
public static IPAddress from(byte[] bytes, byte[] bytes2, java.lang.Integer prefixLength)
public abstract IPAddressNetwork getNetwork()
public static IPAddressNetwork network(IPAddress.IPVersion version)
public static IPAddress getLoopback(IPAddress.IPVersion version)
public static IPAddress getLocalHost() throws java.net.UnknownHostException
java.net.UnknownHostException
public static java.lang.String[] getStandardLoopbackStrings(IPAddress.IPVersion version)
public IPAddressSection getSection()
public IPAddressPart[] getParts(IPAddressSection.IPStringBuilderOptions options)
public int getMaxSegmentValue()
public static int maxSegmentValue(IPAddress.IPVersion version)
public int getBytesPerSegment()
public int getBitsPerSegment()
public static int bitsPerSegment(IPAddress.IPVersion version)
public abstract int getByteCount()
public static int byteCount(IPAddress.IPVersion version)
public abstract int getSegmentCount()
public int getSegmentIndex(java.lang.Integer networkPrefixLength)
public int getByteIndex(java.lang.Integer networkPrefixLength)
public static int segmentCount(IPAddress.IPVersion version)
public abstract int getBitCount()
public static int bitCount(IPAddress.IPVersion version)
public boolean isMultipleByNetworkPrefix()
public boolean isMultiple()
public boolean isPrefixed()
public java.lang.Integer getNetworkPrefixLength()
public IPAddressSegment getSegment(int index)
public java.math.BigInteger getCount()
public abstract IPAddress getLower()
public abstract IPAddress getUpper()
public abstract java.util.Iterator<? extends IPAddress> iterator()
public abstract java.lang.Iterable<? extends IPAddress> getAddresses()
public boolean isIPv4()
public boolean isIPv6()
public IPAddress.IPVersion getIPVersion()
public IPv4Address toIPv4()
IPv4Address
. Otherwise, returns null.isIPv4Convertible()
public IPv6Address toIPv6()
IPv6Address
. Otherwise, returns null.public abstract boolean isIPv4Convertible()
toIPv4()
to match the conversion.
This method returns true for all IPv4 addresses.public abstract boolean isIPv6Convertible()
toIPv6()
to match the conversion.
This method returns true for all IPv6 addresses.public abstract boolean isLinkLocal()
InetAddress.isLinkLocalAddress()
public abstract boolean isSiteLocal()
InetAddress.isSiteLocalAddress()
public abstract boolean isMulticast()
InetAddress.isMulticastAddress()
public boolean isAnyLocal()
InetAddress.isAnyLocalAddress()
public abstract boolean isLoopback()
InetAddress.isLoopbackAddress()
public byte[] getBytes()
IPAddressTypeException
- if this address does not map to a single address.
If you want to get subnet bytes or mask bytes, call getLowestBytespublic byte[] getLowestBytes()
public java.net.InetAddress toInetAddress()
IPAddressTypeException
- if this address does not map to a single address, ie it is a subnetpublic boolean isZero()
public int hashCode()
hashCode
in class java.lang.Object
public int compareTo(IPAddress other)
compareTo
in interface java.lang.Comparable<IPAddress>
public boolean matches(IPAddressString otherString)
public boolean isSameAddress(IPAddress other)
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public boolean contains(IPAddress other)
other
- public abstract IPAddress[] subtract(IPAddress other)
other
- IPAddressTypeException
- if the two sections are not comparablepublic static IPAddress from(java.net.InetAddress inetAddress)
public java.lang.String[] getSegmentStrings()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toCanonicalString()
public java.lang.String toFullString()
public java.lang.String toNormalizedString()
public java.lang.String toCompressedString()
public java.lang.String toSubnetString()
public java.lang.String toNormalizedWildcardString()
public java.lang.String toCanonicalWildcardString()
public java.lang.String toCompressedWildcardString()
public java.lang.String toSQLWildcardString()
public java.lang.String toNetworkPrefixLengthString()
public java.lang.String toConvertedString()
public abstract java.lang.String toUNCHostName()
public abstract java.lang.String toReverseDNSLookupString()
public java.lang.String toHexString(boolean withPrefix)
public java.lang.String toNormalizedString(IPAddressSection.StringOptions params)
params
- the parameters for the address stringpublic java.lang.String[] toStandardStrings()
public java.lang.String[] toAllStrings()
Use this method with care... a single IPv6 address can have thousands of string representations.
Examples:
Variations included in this method:
toStandardStrings()
public java.lang.String[] toStrings(IPAddressSection.IPStringBuilderOptions options)
public IPAddressPartStringCollection toStandardStringCollection()
public IPAddressPartStringCollection toAllStringCollection()
public IPAddressPartStringCollection toStringCollection(IPAddressSection.IPStringBuilderOptions options)
public IPAddressString toAddressString()
IPAddressString.getAddress()
In general, users are intended to create IPAddress objects from IPAddressString objects,
while the reverse direction is generally not all that useful.
However, the reverse direction can be useful under certain circumstances.
Not all IPAddressString objects can be converted to IPAddress objects,
as is the case with IPAddressString objects corresponding to the types IPType.INVALID and IPType.EMPTY.
Not all IPAddressString objects can be converted to IPAddress objects without specifying the IP version,
as is the case with IPAddressString objects corresponding to the types IPType.PREFIX and IPType.ALL.
So in the event you wish to store a collection of IPAddress objects with a collection of IPAddressString objects,
and not all the IPAddressString objects can be converted to IPAddress objects, then you may wish to use a collection
of only IPAddressString objects, in which case this method is useful.public static java.lang.String toDelimitedSQLStrs(java.lang.String[] strs)
public boolean isRangeEquivalentToPrefix()
public int getMinPrefix()
IPAddressDivision.getMaskPrefixLength(boolean)
public java.lang.Integer getEquivalentPrefix()
public IPAddress toPrefixedEquivalent()
public IPAddress toPrefixedMin()
public java.lang.Integer getMaskPrefixLength(boolean network)
network
- whether to check if we are a network mask or a host maskpublic boolean isMaskCompatibleWithRange(IPAddress mask, java.lang.Integer networkPrefixLength)
mask
- networkPrefixLength
- public abstract IPAddress toSubnet(IPAddress mask) throws IPAddressTypeException
isMaskCompatibleWithRange(IPAddress, Integer)
IPAddressTypeException
public abstract IPAddress toSubnet(IPAddress mask, java.lang.Integer networkPrefixLength) throws IPAddressTypeException
isMaskCompatibleWithRange(IPAddress, Integer)
IPAddressTypeException
public abstract IPAddress toSubnet(int networkPrefixLength)
public abstract IPAddressSection getNetworkSection(int networkPrefixLength, boolean withPrefixLength)
networkPrefixLength
- withPrefixLength
- whether the resulting section will have networkPrefixLength as the associated prefix length or notpublic IPAddressSection getNetworkSection(int networkPrefixLength)
networkPrefixLength
- public abstract IPAddressSection getNetworkSection()
public abstract IPAddressSection getHostSection(int networkPrefixLength)
networkPrefixLength
- public abstract IPAddressSection getHostSection()
public IPAddress 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 IPAddress toSupernet()
public void getMatchesSQLClause(java.lang.StringBuilder builder, java.lang.String sqlExpression)
builder
- sqlExpression
- public void getMatchesSQLClause(java.lang.StringBuilder builder, java.lang.String sqlExpression, IPAddressSQLTranslator translator)
builder
- sqlExpression
- translator
-