Java InetAddress Check isPrivate(InetAddress addr)

Here you can find the source of isPrivate(InetAddress addr)

Description

is Private

License

Open Source License

Declaration

private static boolean isPrivate(InetAddress addr) 

Method Source Code


//package com.java2s;

import java.net.InetAddress;

public class Main {
    private static final int[][] PRIVATE_ADDRS = { { 10 }, { 192, 168 }, { 172, 16 }, { 172, 17 }, { 172, 18 },
            { 172, 19 }, { 172, 20 }, { 172, 21 }, { 172, 22 }, { 172, 23 }, { 172, 24 }, { 172, 25 }, { 172, 26 },
            { 172, 27 }, { 172, 28 }, { 172, 29 }, { 172, 30 }, { 172, 31 }, };

    private static boolean isPrivate(InetAddress addr) {
        byte[] addrb = addr.getAddress();
        for (int i = PRIVATE_ADDRS.length; --i >= 0;) {
            boolean valid = true;
            for (int j = 0; j < PRIVATE_ADDRS[i].length; j++)
                if (PRIVATE_ADDRS[i][j] != (addrb[j] & 0xFF)) {
                    valid = false;/*  w  w w  .j  a va 2  s .c  om*/
                    break;
                }
            if (valid)
                return true;
        }
        return false;
    }
}

Related

  1. isMulticastAddress(InetAddress ipAddr)
  2. isOnNetwork(InetAddress host, InetAddress network, byte[] mask)
  3. isPortFree(int port, InetAddress addr)
  4. isPortInUse(InetAddress address, int port, int count)
  5. isPortOpen(InetAddress address, int port)
  6. isPrivate(InetAddress adr)
  7. isPrivateIp(final InetAddress address)
  8. isPrivateIp(InetAddress addr)
  9. isPrivateIP(InetAddress ip)