NetUtils { public static boolean isIPv6AddressValid(String cidr) { if (cidr == null) { return false; } String[] values = cidr.split("/"); try { InetAddress addr = InetAddress.getByName(values[0]); if (!(addr instanceof Inet6Address)) { return false; } } catch (UnknownHostException ex) { return false; } if (values.length >= 2) { int prefix = Integer.parseInt(values[1]); if (prefix < 0 || prefix > 128) { return false; } } return true; } private  NetUtils(); static int byteArray4ToInt(byte[] ba); static long byteArray6ToLong(byte[] ba); static byte[] longToByteArray6(long addr); static byte[] intToByteArray4(int value); static InetAddress getInetAddress(int address); static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6); static int getSubnetMaskLength(byte[] subnetMask); static int getSubnetMaskLength(InetAddress subnetMask); static InetAddress getSubnetPrefix(InetAddress ip, int maskLen); static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask,
            InetAddress filterMask); static boolean isZeroMAC(byte[] mac); static boolean isBroadcastMACAddr(byte[] macAddress); static boolean isUnicastMACAddr(byte[] macAddress); static boolean isMulticastMACAddr(byte[] macAddress); static boolean isAny(InetAddress ip); static boolean fieldsConflict(int field1, int field2); static InetAddress parseInetAddress(String addressString); static boolean isIPv4AddressValid(String cidr); static boolean isIPv6AddressValid(String cidr); static boolean isIPAddressValid(String cidr); static int getUnsignedByte(byte value); static int getUnsignedShort(short value); static InetAddress gethighestIP(boolean v6); static byte[] getBroadcastMACAddr(); static final int NUM_BITS_IN_A_BYTE; static final int MAC_ADDR_LENGTH_IN_BYTES; static final int MAC_ADDR_LENGTH_IN_WORDS; }
@Test public void testIsIPv6Valid() throws UnknownHostException { assertTrue(NetUtils.isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:fe9d:f156")); assertTrue(NetUtils.isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d:f156")); assertTrue(NetUtils.isIPv6AddressValid("fe80::204:61ff:fe9d:f156")); assertTrue(NetUtils.isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:254.157.241.86")); assertTrue(NetUtils.isIPv6AddressValid("fe80:0:0:0:0204:61ff:254.157.241.86")); assertTrue(NetUtils.isIPv6AddressValid("fe80::204:61ff:254.157.241.86")); assertTrue(NetUtils.isIPv6AddressValid("2001::")); assertTrue(NetUtils.isIPv6AddressValid("::1")); assertTrue(NetUtils.isIPv6AddressValid("fe80::")); assertFalse(NetUtils.isIPv6AddressValid("abcd")); assertFalse(NetUtils.isIPv6AddressValid("1")); assertFalse(NetUtils.isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d")); assertFalse(NetUtils.isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d")); assertFalse(NetUtils.isIPv6AddressValid("192.168.1.1")); assertFalse(NetUtils.isIPv6AddressValid("2001:0000:1234:0000:10001:C1C0:ABCD:0876")); assertFalse(NetUtils.isIPv6AddressValid("20010:0000:1234:0000:10001:C1C0:ABCD:0876")); assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/60")); assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/64")); assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/0")); assertTrue(NetUtils.isIPv6AddressValid("::1/128")); assertFalse(NetUtils.isIPv6AddressValid("124.15.6.89/60")); assertFalse(NetUtils.isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/130")); assertFalse(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/-5")); assertFalse(NetUtils.isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/64")); assertFalse(NetUtils.isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/-1")); }