Java InetAddress Check isPrivateIp(final InetAddress address)

Here you can find the source of isPrivateIp(final InetAddress address)

Description

Determines if a given IP address is private.

License

Open Source License

Parameter

Parameter Description
address address to test

Return

true if private, otherwise false

Declaration

public static boolean isPrivateIp(final InetAddress address) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.UnknownHostException;

import java.util.Objects;

public class Main {
    /**/*w ww. j av  a2 s .c om*/
     * Determines if a given IP address is private.
     *
     * @param ip IP address as {@link String} to test
     * @return true if private, otherwise false
     * @throws UnknownHostException thrown when the passed string can't be parsed for an IP
     */
    public static boolean isPrivateIp(final String ip) throws UnknownHostException {
        final InetAddress address = InetAddress.getByName(ip);
        return isPrivateIp(address);
    }

    /**
     * Determines if a given IP address is private.
     *
     * @param address address to test
     * @return true if private, otherwise false
     */
    public static boolean isPrivateIp(final InetAddress address) {
        Objects.requireNonNull("IP address must be present");
        return address.isAnyLocalAddress() || address.isLoopbackAddress() || address.isLinkLocalAddress()
                || address.isSiteLocalAddress();
    }
}

Related

  1. isPortFree(int port, InetAddress addr)
  2. isPortInUse(InetAddress address, int port, int count)
  3. isPortOpen(InetAddress address, int port)
  4. isPrivate(InetAddress addr)
  5. isPrivate(InetAddress adr)
  6. isPrivateIp(InetAddress addr)
  7. isPrivateIP(InetAddress ip)
  8. isPrivateNetworkAddress(final InetAddress inetAddress)
  9. isPrivateSubnet(InetAddress address)