Java IP Address Get getIPV6AddrArray(String s)

Here you can find the source of getIPV6AddrArray(String s)

Description

get IPV Addr Array

License

Open Source License

Declaration

private static int[] getIPV6AddrArray(String s) 

Method Source Code


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

public class Main {
    private static int[] getIPV6AddrArray(String s) {
        try {/*from   w w w .java 2  s. co  m*/
            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. getIPV4Address(List addresses)
  2. getIPv4Address(NetworkInterface iface)
  3. getIPV4MainAddress(NetworkInterface ni)
  4. getIPv4MulticastGroup(int hash)
  5. getIPV4NetwprkOrder(String theIp)
  6. getIPV6AllAddresses(NetworkInterface ni)
  7. getIpv6List()
  8. getLanIPAddress()
  9. getLinuxIPAddress()