Java Utililty Methods InetAddress

List of utility methods to do InetAddress

Description

The list of methods to do InetAddress are organized into topic(s).

Method

StringformatAddress(InetAddress inet)
Formats input address.
if (inet == null) {
    throw new NullPointerException();
if (inet instanceof Inet4Address) {
    return inet.getHostAddress();
} else if (inet instanceof Inet6Address) {
    byte[] byteRepresentation = inet.getAddress();
    int[] hexRepresentation = new int[IPV6_LEN];
...
StringformatHostAddress(final InetAddress localHost)
format Host Address
if (localHost instanceof Inet6Address) {
    return "[" + localHost.getHostAddress() + "]";
} else {
    return localHost.getHostAddress();
booleangreaterThan(InetAddress inetAddress1, InetAddress inetAddress2)
Compares two IP addresses.
byte[] address1 = inetAddress1.getAddress();
byte[] address2 = inetAddress2.getAddress();
for (int i = 0; i < address1.length; i++) {
    if ((address1[i] & 0xFF) > (address2[i] & 0xFF))
        return true;
    else if ((address1[i] & 0xFF) < (address2[i] & 0xFF))
        break;
return false;
booleanhasIp(InetAddress ip)
has Ip
if (ip == null)
    return false;
NetworkInterface nic = NetworkInterface.getByInetAddress(ip);
return (nic != null && nic.isUp());
booleanhasLocalScope(InetAddress addr)
Returns true when the given address is a local address.
if (addr == null)
    throw new IllegalArgumentException("Must not be null!");
return (addr.isAnyLocalAddress() || addr.isLinkLocalAddress() || addr.isLoopbackAddress());
InetAddressincrement(InetAddress address)
Increments an IP address by 1.
try {
    byte[] newAddress = address.getAddress();
    for (int i = newAddress.length - 1; i >= 0; i--) {
        if (++newAddress[i] != 0x00)
            break;
    return InetAddress.getByAddress(newAddress);
} catch (UnknownHostException e) {
...
intinetAddressCompare(final InetAddress address1, final InetAddress address2)
inet Address Compare
if (address1 == null || address2 == null) {
    return -1;
if (address1.equals(address2)) {
    return 0;
final byte[] ab1 = address1.getAddress();
final byte[] ab2 = address2.getAddress();
...
ComparatorinetAddressesComparator(final boolean sameHost)
Returns comparator that sorts remote node addresses.
return new Comparator<InetSocketAddress>() {
    @Override
    public int compare(InetSocketAddress addr1, InetSocketAddress addr2) {
        if (addr1.isUnresolved() && addr2.isUnresolved())
            return 0;
        if (addr1.isUnresolved() || addr2.isUnresolved())
            return addr1.isUnresolved() ? 1 : -1;
        boolean addr1Loopback = addr1.getAddress().isLoopbackAddress();
...
intinetAddressesCompare(final InetAddress[] addresses1, final InetAddress[] addresses2)
inet Addresses Compare
if (Arrays.equals(addresses1, addresses2)) {
    return 0;
if (addresses1 == null || addresses2 == null) {
    return -1;
if (addresses1.length > addresses2.length) {
    return 1;
...
InetAddressinetAddressFromThriftString(String ipAddress)
inet Address From Thrift String
return InetAddress.getByName(ipAddress);