public abstract class IPAddress extends Address
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.IPStringOptions
or IPv6AddressSection.IPv6StringOptions
along with toNormalizedString(IPAddressSection.IPStringOptions)
.
Or you use one of the methods like Address.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 |
Address.SegmentValueProvider
Modifier and Type | Field and Description |
---|---|
static IPAddressConverter |
addressConverter |
static char |
PREFIX_LEN_SEPARATOR |
addressComparator, ALTERNATIVE_RANGE_SEPARATOR, ALTERNATIVE_RANGE_SEPARATOR_STR, ALTERNATIVE_SEGMENT_WILDCARD_STR, HEX_PREFIX, OCTAL_PREFIX, RANGE_SEPARATOR, RANGE_SEPARATOR_STR, SEGMENT_SQL_SINGLE_WILDCARD, SEGMENT_SQL_SINGLE_WILDCARD_STR, SEGMENT_SQL_WILDCARD, SEGMENT_SQL_WILDCARD_STR, SEGMENT_WILDCARD, SEGMENT_WILDCARD_STR
Modifier and Type | Method and Description |
---|---|
abstract IPAddress |
adjustPrefixBySegment(boolean nextSegment)
Increases or decreases prefix length to the next segment boundary.
|
abstract IPAddress |
adjustPrefixLength(int adjustment)
Increases or decreases prefix length by the given increment.
|
abstract IPAddress |
applyPrefixLength(int networkPrefixLength)
Applies the given prefix length to create a new address.
|
static int |
bitCount(IPAddress.IPVersion version) |
static int |
bitsPerSegment(IPAddress.IPVersion version) |
abstract IPAddress |
bitwiseOr(IPAddress mask)
Does the bitwise disjunction with this address.
|
abstract IPAddress |
bitwiseOrNetwork(IPAddress mask,
int networkPrefixLength)
Does the bitwise disjunction with this address.
|
static int |
byteCount(IPAddress.IPVersion version) |
boolean |
contains(Address other) |
boolean |
contains(IPAddress other) |
static IPAddress |
from(byte[] bytes) |
static IPAddress |
from(byte[] bytes,
java.lang.Integer prefixLength) |
static IPAddress |
from(java.net.InetAddress inetAddress) |
static IPAddress |
from(IPAddress.IPVersion version,
Address.SegmentValueProvider lowerValueProvider,
Address.SegmentValueProvider upperValueProvider,
java.lang.Integer prefixLength) |
int |
getBitsPerSegment() |
int |
getByteCount()
returns the number of bytes in each of the address components represented by this instance
|
int |
getBytesPerSegment() |
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() |
abstract java.lang.Iterable<? extends IPAddress> |
getIterable()
Useful for using an instance in a "for-each loop".
|
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.
|
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() |
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.
|
abstract 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.
|
IPAddressStringDivisionSeries[] |
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.
|
IPAddressSection |
getSection(int index)
Gets the subsection from the series starting from the given index
|
IPAddressSection |
getSection(int index,
int endIndex)
Gets the subsection from the series starting from the given index and ending just before the give endIndex
|
IPAddressSegment |
getSegment(int index) |
IPAddressSegment[] |
getSegments() |
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.
|
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() |
boolean |
isLocal() |
abstract boolean |
isLoopback() |
boolean |
isMultipleByNetworkPrefix() |
boolean |
isSameAddress(IPAddress other) |
abstract boolean |
isSiteLocal() |
abstract java.util.Iterator<? extends IPAddress> |
iterator()
An address component can represent a single segment, address, or section, or it can represent multiple,
typically a subnet or range of segment, address, or section values.
|
abstract IPAddress |
mask(IPAddress mask)
Applies the given mask to all addresses represented by this IPAddress.
|
abstract IPAddress |
maskNetwork(IPAddress mask,
int networkPrefixLength)
Applies the given mask up until the given prefix length to all addresses represented by this IPAddress.
|
boolean |
matches(IPAddressString otherString) |
static int |
maxSegmentValue(IPAddress.IPVersion version) |
static IPAddressNetwork |
network(IPAddress.IPVersion version) |
abstract IPAddress |
removePrefixLength()
Removes the prefix.
|
abstract IPAddress |
removePrefixLength(boolean zeroed) |
abstract IPAddress |
reverseBits(boolean perByte)
Returns a new IPAddress which has the bits reversed.
|
abstract IPAddress |
reverseBytes()
Returns a new segment series with the bytes reversed.
|
abstract IPAddress |
reverseBytesPerSegment()
Returns a new segment series with the bytes reversed within each segment.
|
abstract IPAddress |
reverseSegments()
Returns a new segment series with the segments reversed.
|
static int |
segmentCount(IPAddress.IPVersion version) |
java.util.Iterator<? extends IPAddressSegment[]> |
segmentsIterator() |
abstract IPAddress |
setPrefixLength(int prefixLength)
Sets the prefix length.
|
abstract IPAddress |
setPrefixLength(int prefixLength,
boolean zeroed) |
abstract IPAddress[] |
subtract(IPAddress other)
Subtract the give subnet from this subnet, returning an array of subnets 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
|
java.lang.String |
toBinaryString()
Writes this address as a single binary value with always the exact same number of characters
If this section represents a range of values outside of the network prefix length, then this is printed as a range of two hex values.
|
HostName |
toCanonicalHostName()
Does a reverse name lookup to get the canonical host name.
|
java.lang.String |
toCanonicalWildcardString()
This produces a string similar to the canonical string but avoids the CIDR prefix.
|
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.
|
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() |
IPAddress |
toMinPrefixedEquivalent()
Constructs an equivalent address with the smallest CIDR prefix possible (largest network),
such that the address represents the exact same range of addresses.
|
java.lang.String |
toNormalizedString(IPAddressSection.IPStringOptions 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.
|
java.lang.String |
toOctalString(boolean with0Prefix)
Writes this address as a single octal value with always the exact same number of characters, with or without a preceding 0 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.
|
java.lang.String |
toPrefixLengthString()
Returns a string with a CIDR network prefix length if this address has a network prefix length.
|
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
|
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
|
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.
|
abstract java.lang.String |
toUNCHostName()
Generates the Microsoft UNC path component for this address
|
compareTo, equals, getBitCount, getBytes, getBytes, getCount, getDivision, getDivisionCount, getEquivalentPrefix, getMinPrefix, getPrefixLength, getSegmentCount, getSegments, getSegments, getUpperBytes, getUpperBytes, hashCode, isFullRange, isMore, isMulticast, isMultiple, isMultipleByPrefix, isPrefixed, isRangeEquivalentToPrefix, isSameAddress, isZero, toCanonicalString, toCompressedString, toHexString, toNormalizedString, toString
public static final char PREFIX_LEN_SEPARATOR
public static final IPAddressConverter addressConverter
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(IPAddress.IPVersion version, Address.SegmentValueProvider lowerValueProvider, Address.SegmentValueProvider upperValueProvider, 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()
getSection
in class Address
public IPAddressSection getSection(int index)
AddressSegmentSeries
getSection
in interface AddressSegmentSeries
getSection
in class Address
public IPAddressSection getSection(int index, int endIndex)
AddressSegmentSeries
getSection
in interface AddressSegmentSeries
getSection
in class Address
public IPAddressStringDivisionSeries[] getParts(IPAddressSection.IPStringBuilderOptions options)
public int getMaxSegmentValue()
getMaxSegmentValue
in class Address
public static int maxSegmentValue(IPAddress.IPVersion version)
public int getBytesPerSegment()
public int getBitsPerSegment()
public static int bitsPerSegment(IPAddress.IPVersion version)
public int getByteCount()
AddressComponent
getByteCount
in interface AddressComponent
getByteCount
in class Address
public static int byteCount(IPAddress.IPVersion version)
public static int segmentCount(IPAddress.IPVersion version)
public static int bitCount(IPAddress.IPVersion version)
public boolean isMultipleByNetworkPrefix()
public java.lang.Integer getNetworkPrefixLength()
public IPAddressSegment getSegment(int index)
getSegment
in interface AddressSegmentSeries
getSegment
in class Address
public IPAddressSegment[] getSegments()
getSegments
in interface AddressSegmentSeries
getSegments
in class Address
public abstract IPAddress getLower()
getLower
in interface AddressComponent
getLower
in interface AddressSegmentSeries
getLower
in class Address
public abstract IPAddress getUpper()
getUpper
in interface AddressComponent
getUpper
in interface AddressSegmentSeries
getUpper
in class Address
public abstract IPAddress reverseBits(boolean perByte)
reverseBits
in interface AddressComponent
reverseBits
in interface AddressSegmentSeries
reverseBits
in class Address
perByte
- if true, only the bits in each byte are reversed, if false, then all bits in the address are reversedpublic abstract IPAddress reverseBytes()
AddressSegmentSeries
reverseBytes
in interface AddressComponent
reverseBytes
in interface AddressSegmentSeries
reverseBytes
in class Address
public abstract IPAddress reverseBytesPerSegment()
AddressSegmentSeries
reverseBytesPerSegment
in interface AddressSegmentSeries
reverseBytesPerSegment
in class Address
public abstract IPAddress reverseSegments()
AddressSegmentSeries
reverseSegments
in interface AddressSegmentSeries
reverseSegments
in class Address
public abstract java.util.Iterator<? extends IPAddress> iterator()
AddressComponent
AddressItem.isMultiple()
to determine if this instance represents multiple.
This method iterates through the individual elements.iterator
in interface AddressComponent
iterator
in interface AddressSegmentSeries
iterator
in class Address
public java.util.Iterator<? extends IPAddressSegment[]> segmentsIterator()
segmentsIterator
in interface AddressSegmentSeries
segmentsIterator
in class Address
public abstract java.lang.Iterable<? extends IPAddress> getIterable()
AddressComponent
AddressComponent.iterator()
directly.getIterable
in interface AddressComponent
getIterable
in interface AddressSegmentSeries
getIterable
in class Address
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 boolean isAnyLocal()
InetAddress.isAnyLocalAddress()
public abstract boolean isLoopback()
InetAddress.isLoopbackAddress()
public java.net.InetAddress toInetAddress()
AddressTypeException
- if this address does not map to a single address, ie it is a subnetpublic boolean matches(IPAddressString otherString)
public boolean isSameAddress(IPAddress other)
public boolean contains(IPAddress other)
other
- public abstract IPAddress[] subtract(IPAddress other)
other
- AddressTypeException
- if the two sections are not comparablepublic static IPAddress from(java.net.InetAddress inetAddress)
public java.lang.String[] getSegmentStrings()
public java.lang.String toFullString()
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 toPrefixLengthString()
public java.lang.String toConvertedString()
public abstract java.lang.String toUNCHostName()
public java.lang.String toReverseDNSLookupString()
public java.lang.String toBinaryString()
public java.lang.String toOctalString(boolean with0Prefix)
public java.lang.String toNormalizedString(IPAddressSection.IPStringOptions 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.toAddressString
in class Address
public static java.lang.String toDelimitedSQLStrs(java.lang.String[] strs)
public IPAddress toPrefixedEquivalent()
public IPAddress toMinPrefixedEquivalent()
public java.lang.Integer getMaskPrefixLength(boolean network)
network
- whether to check if we are a network mask or a host maskpublic abstract IPAddress mask(IPAddress mask) throws AddressTypeException
AddressTypeException
is thrown.AddressTypeException
public abstract IPAddress maskNetwork(IPAddress mask, int networkPrefixLength) throws AddressTypeException
AddressTypeException
is thrown.AddressTypeException
public abstract IPAddress applyPrefixLength(int networkPrefixLength)
applyPrefixLength
in interface AddressSegmentSeries
applyPrefixLength
in class Address
public abstract IPAddress bitwiseOr(IPAddress mask) throws AddressTypeException
mask
- AddressTypeException
public abstract IPAddress bitwiseOrNetwork(IPAddress mask, int networkPrefixLength) throws AddressTypeException
mask
- networkPrefixLength
- the new prefix length for the addressAddressTypeException
public abstract IPAddressSection getNetworkSection(int networkPrefixLength, boolean withPrefixLength)
networkPrefixLength
- withPrefixLength
- whether the resulting section will have networkPrefixLength as the associated prefix length or notpublic abstract IPAddressSection getNetworkSection(int networkPrefixLength)
networkPrefixLength
- public abstract IPAddressSection getNetworkSection()
public abstract IPAddressSection getHostSection(int networkPrefixLength)
networkPrefixLength
- public abstract IPAddressSection getHostSection()
public abstract IPAddress removePrefixLength()
AddressSegmentSeries
removePrefixLength
in interface AddressSegmentSeries
removePrefixLength
in class Address
public abstract IPAddress removePrefixLength(boolean zeroed)
public abstract IPAddress adjustPrefixBySegment(boolean nextSegment)
AddressSegmentSeries
adjustPrefixBySegment
in interface AddressSegmentSeries
adjustPrefixBySegment
in class Address
public abstract IPAddress adjustPrefixLength(int adjustment)
AddressSegmentSeries
adjustPrefixLength
in interface AddressSegmentSeries
adjustPrefixLength
in class Address
public abstract IPAddress setPrefixLength(int prefixLength)
AddressSegmentSeries
setPrefixLength
in interface AddressSegmentSeries
setPrefixLength
in class Address
public abstract IPAddress setPrefixLength(int prefixLength, boolean zeroed)
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
-