public final class IpAddress extends CacheableDataType implements Comparable<IpAddress>
Useful predicates allow the consumer to test for properties of the
represented IP address; for example, isLoopback()
.
All constructors for this class are private. Creating instances of
IpAddress
is done via the static methods on the class.
Instances of this class are immutable, making them inherently threadsafe.
This class overrides equals(java.lang.Object)
and hashCode()
so that instances
play nicely with the Collection classes.
This class implements the Comparable
interface to ensure that
a sorted list of addresses is presented in an intuitive order.
Modifier and Type | Class and Description |
---|---|
static class |
IpAddress.AddressClass
Specifies the IP address class.
|
static class |
IpAddress.Family
Specifies the IP address families.
|
Modifier and Type | Field and Description |
---|---|
static IpAddress |
BROADCAST_IPv4
The (limited) broadcast address (IPv4), that is "255.255.255.255".
|
static String |
DELIMITER_V4
The field delimiter in IPv4 addresses (i.e. a dot).
|
static String |
DELIMITER_V6
The field delimiter in IPv6 addresses (i.e. a colon).
|
static int |
IP_V4_ADDR_SIZE
Number of bytes for IPv4 address.
|
static int |
IP_V6_ADDR_SIZE
Number of bytes for IPv6 address.
|
static IpAddress |
LOOPBACK_IPv4
The loopback address (IPv4); that is, 127.0.0.1.
|
static IpAddress |
LOOPBACK_IPv6
The loopback address (IPv6); that is, "::1".
|
static IpAddress |
UNDETERMINED_IPv4
The undetermined address (IPv4); that is, "0.0.0.0".
|
static IpAddress |
UNDETERMINED_IPv6
The undetermined address (IPv6), that is "::".
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(IpAddress o)
Implements the Comparable interface, to return addresses
in natural order.
|
boolean |
equals(Object o) |
IpAddress.AddressClass |
getAddressClass()
Returns the address class.
|
IpAddress.Family |
getFamily()
Returns the family this address belongs to.
|
String |
getMulticastName()
Returns a friendly name for this IP address, if it is a multicast
address, and if the name exists.
|
int |
hashCode() |
void |
intoBuffer(ByteBuffer b)
Writes the bytes that represent the IP address directly into
ByteBuffer at the buffer's current position. |
static IpAddress |
ip(byte[] bytes)
Convenience method that simply delegates to
valueOf(byte[]) . |
static IpAddress |
ip(InetAddress ia)
Convenience method that simply delegates
to
valueOf(java.net.InetAddress) . |
static IpAddress |
ip(String s)
Convenience method that simply delegates to
valueOf(String) . |
boolean |
isBroadcast()
Returns true if the address represented is a "broadcast" address.
|
boolean |
isLinkLocal()
Returns true if this address is a "link local" address.
|
boolean |
isLoopback()
Returns true if the address represented is a "loopback" address.
|
boolean |
isMulticast()
Returns true if the address represented is a "multicast" address.
|
boolean |
isReserved()
Returns true if the address is "reserved".
|
boolean |
isUndetermined()
Returns true if the address represented is the "undetermined" address.
|
byte[] |
toByteArray()
Returns a newly allocated byte array containing bytes that represent
the IP address.
|
String |
toFullString()
If IPv6, returns the address with all fields zero-filled.
|
InetAddress |
toInetAddress()
Returns an InetAddress instance equivalent to this IP address.
|
String |
toShortString()
If IPv6, returns the shortened form of this address.
|
String |
toString() |
String |
toStringWithPort(int portNumber)
Returns this IP address as a string that includes the specified port
number.
|
static IpAddress |
valueFrom(ByteBuffer buffer,
boolean v6)
Reads bytes from the specified byte buffer and creates an IP address
instance.
|
static IpAddress |
valueOf(byte[] bytes)
Returns an IP address instance that represents the value of the
IP address defined by the specified address bytes.
|
static IpAddress |
valueOf(InetAddress ia)
Returns an IP address instance that represents the value of the
IP address defined by the specified
InetAddress
instance. |
static IpAddress |
valueOf(String s)
Returns an IP address instance that represents the value of the
IP address defined by the specified string.
|
static int |
valueOfPort(String address)
Returns the value of the port number from an address string
(if one is present).
|
getRefQ, keyFromBytes
public static final int IP_V4_ADDR_SIZE
public static final int IP_V6_ADDR_SIZE
public static final String DELIMITER_V4
public static final String DELIMITER_V6
public static final IpAddress LOOPBACK_IPv4
public static final IpAddress LOOPBACK_IPv6
public static final IpAddress UNDETERMINED_IPv4
public static final IpAddress UNDETERMINED_IPv6
public static final IpAddress BROADCAST_IPv4
public String toShortString()
toString()
.public String toFullString()
toString()
.public String toStringWithPort(int portNumber)
Examples:
15.43.37.2:8080
"
[FEDC:0:1:0:0:0:AB:23]:8080
"
portNumber
- the port number to appendIllegalArgumentException
- if portNumber is < 0 or > 65535public byte[] toByteArray()
public void intoBuffer(ByteBuffer b)
ByteBuffer
at the buffer's current position. This will be an
array of length 4 for an IPv4 address, or an array of length 16 for an
IPv6 address. Note that the address bytes are in network byte order;
that is, the highest order byte of the address is at index 0.b
- the byte buffer to write intopublic InetAddress toInetAddress()
InetAddress.getByAddress(java.lang.String, byte[])
public IpAddress.Family getFamily()
public IpAddress.AddressClass getAddressClass()
public String getMulticastName()
A value of "(Unknown)" will be returned if no known mapping exists.
public boolean isMulticast()
224.0.0.0
- 239.255.255.255
for IPv4 (Address class D).
TODO - include logic for IPv6
public boolean isBroadcast()
255.255.255.255
for IPv4.
TODO - include logic for IPv6
public boolean isReserved()
0.0.0.0
- 0.255.255.255
for IPv4.
TODO - include logic for IPv6
public boolean isLoopback()
127.0.0.0
- 127.255.255.255
for IPv4;
::1
for IPv6).public boolean isUndetermined()
0.0.0.0
for IPv4; ::
for IPv6 ).public boolean isLinkLocal()
169.254.*.*
for IPv4; fe80::*:*:*:*
for IPv6 ).public int compareTo(IpAddress o)
compareTo
in interface Comparable<IpAddress>
o
- the other IP addressComparable.compareTo(T)
public static IpAddress valueOf(byte[] bytes)
If the bytes array is of length 4, it is interpreted as an IPv4 address. If the bytes array is of length 16, it is interpreted as an IPv6 address. Other array lengths will throw an exception.
bytes
- the address bytesNullPointerException
- if the parameter is nullIllegalArgumentException
- if bytes is an unsupported lengthpublic static IpAddress ip(byte[] bytes)
valueOf(byte[])
.bytes
- the address bytesNullPointerException
- if the parameter is nullIllegalArgumentException
- if bytes is an unsupported lengthpublic static IpAddress valueOf(String s)
Acceptable formats are:
"n.n.n.n"
) --
where 'n' is from 0 to 255"n.n.n.n:p"
) -- where 'n' is from 0 to 255, and 'p' is
from 0 to 65535 "x:x:x:x:x:x:x:x"
) -- where 'x' is from 0000 to FFFF.
Leading zeros may be dropped. "FF:BAD::3"
) "[2001:db8::1428:57ab]"
)"[2001:db8::1428:57ab]:443"
) s
- the string representation of the IP addressNullPointerException
- if s is nullIllegalArgumentException
- if s is not an acceptable formatpublic static IpAddress ip(String s)
valueOf(String)
.
Note that code can be written more concisely by using a static import
of this method; for example, the following two statements are
equivalent:
IpAddress ip = IpAddress.valueOf("15.255.124.7"); DnsName a = dns("foo.hp.com");
s
- the string representation of the IP addressNullPointerException
- if s is nullIllegalArgumentException
- if address is not an acceptable formatpublic static IpAddress valueOf(InetAddress ia)
InetAddress
instance.ia
- the InetAddressNullPointerException
- if the parameter is nullpublic static IpAddress ip(InetAddress ia)
valueOf(java.net.InetAddress)
.ia
- the InetAddressNullPointerException
- if the parameter is nullpublic static IpAddress valueFrom(ByteBuffer buffer, boolean v6)
v6
is true 16 bytes will be read; otherwise 4 bytes
will be read.buffer
- the byte buffer from which to read bytesv6
- true if reading an IPv6 address; false for IPv4BufferUnderflowException
- if the buffer does not have sufficient
bytes remainingpublic static int valueOfPort(String address)
Some examples:
"127.0.0.1:8080"
will return 8080
"127.0.0.1"
will return -1
(no port number)"[2001:db8::1428:57ab]:443"
will return 443
"[2001:db8::1428:57ab]"
will return -1
(no port number)address
- the address stringNullPointerException
- if address string is nullIllegalArgumentException
- if the string is badly formatted or
the port number is < 0 or > 65535Copyright © 2015. All Rights Reserved.