Java IP Address Get getIPList(String s, String s1)

Here you can find the source of getIPList(String s, String s1)

Description

get IP List

License

Open Source License

Declaration

public static String[] getIPList(String s, String s1) 

Method Source Code


//package com.java2s;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

public class Main {

    public static String[] getIPList(String s, String s1) {
        String[] as = null;//from w  ww  .  j av a  2 s.  c o m
        long l = getAddrLong("255.255.255.255");
        long l1 = getAddrLong(s1);
        if (l1 == l) {
            as = new String[1];
            as[0] = s;
            return as;
        }
        long l2 = getAddrLong(s);
        long l3 = l ^ l1;
        if (l1 == 0L || l2 == 0L || l1 > l || l3 > l) {
            return null;
        }
        if (l3 > 0x10000L) {
            l3 = 65025L;
        }
        if (l3 == 2L) {
            as = new String[1];
            as[0] = convertAddr(l2 + 1L);
            return as;
        }
        if ((l2 + l3) - 1L > l) {
            return null;
        }
        as = new String[(int) l3 - 1];
        for (int i = 0; (long) i < l3 - 1L; i++) {
            as[i] = convertAddr(l2 + 1L + (long) i);
        }
        return as;
    }

    public static long getAddrLong(String s) {
        int[] ai = getAddrArray(s);
        if (ai == null) {
            return 0L;
        }
        long l = 0L;
        for (int i = 0; i < 4; i++) {
            l |= (long) ai[i] << 8 * (3 - i);
        }
        return l;
    }

    public static String convertAddr(long l) {
        int[] ai = new int[4];
        for (int i = 0; i < 4; i++) {
            ai[i] = (int) (l >> 8 * (3 - i) & 255L);
        }
        return ai[0] + "." + ai[1] + "." + ai[2] + "." + ai[3];
    }

    public static int[] getAddrArray(String s) {
        if (s == null)
            return null;
        if (s.indexOf(":") != -1)
            return getIPV6AddrArray(s);
        StringTokenizer stringtokenizer = new StringTokenizer(s, ".");
        if (stringtokenizer.countTokens() != 4)
            return null;
        int[] ai = new int[4];
        try {
            for (int i = 0; i < 4; i++) {
                ai[i] = Integer.parseInt(stringtokenizer.nextToken());
            }
        } catch (NumberFormatException numberformatexception) {
            return null;
        }
        for (int j = 0; j < 4; j++) {
            if (ai[j] < 0 || ai[j] > 255)
                return null;
        }
        return ai;
    }

    private static int[] getIPV6AddrArray(String s) {
        try {
            InetAddress inetaddress = InetAddress.getByName(s);
            s = inetaddress.getHostAddress();
        } catch (UnknownHostException unknownhostexception) {
            System.err.println("unknown ipv6 exception");
        }
        StringTokenizer stringtokenizer = new StringTokenizer(s, ":");
        if (stringtokenizer.countTokens() != 8)
            return null;
        String[] as = new String[8];
        for (int i = 0; i < 8; i++) {
            as[i] = stringtokenizer.nextToken();
        }
        int[] ai = new int[16];
        int j = 0;
        for (int k = 0; k < as.length; k++) {
            for (int l = as[k].length(); l < 4; l++) {
                as[k] = "0" + as[k];
            }
            String s1 = as[k].substring(0, 2);
            String s2 = as[k].substring(2);
            try {
                ai[j++] = Integer.parseInt(s1, 16);
                ai[j++] = Integer.parseInt(s2, 16);
            } catch (NumberFormatException numberformatexception) {
                return null;
            }
        }
        for (int i1 = 0; i1 < 16; i1++) {
            if (ai[i1] < 0 || ai[i1] > 255)
                return null;
        }
        return ai;
    }
}

Related

  1. getIPFromHash(final long ipHash)
  2. getIPFromInterface(String ni)
  3. getIPFromNetworkInterface()
  4. getIpHostnameLocal()
  5. getIPList(String address)
  6. getIPs()
  7. getIPs()
  8. getIPv4Address()
  9. getIPV4Address(List addresses)