@Test public void testIsValidIpV6Address() { for (String host : validIpV6Hosts.keySet()) { assertTrue(NetUtil.isValidIpV6Address(host)); } for (String host : invalidIpV6Hosts.keySet()) { assertFalse(NetUtil.isValidIpV6Address(host)); } }
public static boolean isValidIpV6Address(String ipAddress) { int length = ipAddress.length(); boolean doubleColon = false; int numberOfColons = 0; int numberOfPeriods = 0; int numberOfPercent = 0; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int offset = 0; if (length < 2) { return false; } for (int i = 0; i < length; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '[': if (i != 0) { return false; } if (ipAddress.charAt(length - 1) != ']') { return false; } offset = 1; if (length < 4) { return false; } break; case ']': if (i != length - 1) { return false; } if (ipAddress.charAt(0) != '[') { return false; } break; case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(offset) != ':' && ipAddress.charAt(1 + offset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == offset && (ipAddress.length() <= i || ipAddress.charAt(i + 1) != ':')) { return false; } numberOfColons ++; if (numberOfColons > 7) { return false; } if (numberOfPeriods > 0) { return false; } if (prevChar == ':') { if (doubleColon) { return false; } doubleColon = true; } word.delete(0, word.length()); break; case '%': if (numberOfColons == 0) { return false; } numberOfPercent ++; if (i + 1 >= length) { return false; } try { Integer.parseInt(ipAddress.substring(i + 1)); } catch (NumberFormatException e) { return false; } break; default: if (numberOfPercent == 0) { if (word != null && word.length() > 3) { return false; } if (!isValidHexChar(c)) { return false; } } word.append(c); } } if (numberOfPeriods > 0) { if (numberOfPeriods != 3 || !(isValidIp4Word(word.toString()) && numberOfColons < 7)) { return false; } } else { if (numberOfColons != 7 && !doubleColon) { return false; } if (numberOfPercent == 0) { if (word.length() == 0 && ipAddress.charAt(length - 1 - offset) == ':' && ipAddress.charAt(length - 2 - offset) != ':') { return false; } } } return true; }
NetUtil { public static boolean isValidIpV6Address(String ipAddress) { int length = ipAddress.length(); boolean doubleColon = false; int numberOfColons = 0; int numberOfPeriods = 0; int numberOfPercent = 0; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int offset = 0; if (length < 2) { return false; } for (int i = 0; i < length; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '[': if (i != 0) { return false; } if (ipAddress.charAt(length - 1) != ']') { return false; } offset = 1; if (length < 4) { return false; } break; case ']': if (i != length - 1) { return false; } if (ipAddress.charAt(0) != '[') { return false; } break; case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(offset) != ':' && ipAddress.charAt(1 + offset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == offset && (ipAddress.length() <= i || ipAddress.charAt(i + 1) != ':')) { return false; } numberOfColons ++; if (numberOfColons > 7) { return false; } if (numberOfPeriods > 0) { return false; } if (prevChar == ':') { if (doubleColon) { return false; } doubleColon = true; } word.delete(0, word.length()); break; case '%': if (numberOfColons == 0) { return false; } numberOfPercent ++; if (i + 1 >= length) { return false; } try { Integer.parseInt(ipAddress.substring(i + 1)); } catch (NumberFormatException e) { return false; } break; default: if (numberOfPercent == 0) { if (word != null && word.length() > 3) { return false; } if (!isValidHexChar(c)) { return false; } } word.append(c); } } if (numberOfPeriods > 0) { if (numberOfPeriods != 3 || !(isValidIp4Word(word.toString()) && numberOfColons < 7)) { return false; } } else { if (numberOfColons != 7 && !doubleColon) { return false; } if (numberOfPercent == 0) { if (word.length() == 0 && ipAddress.charAt(length - 1 - offset) == ':' && ipAddress.charAt(length - 2 - offset) != ':') { return false; } } } return true; } }
NetUtil { public static boolean isValidIpV6Address(String ipAddress) { int length = ipAddress.length(); boolean doubleColon = false; int numberOfColons = 0; int numberOfPeriods = 0; int numberOfPercent = 0; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int offset = 0; if (length < 2) { return false; } for (int i = 0; i < length; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '[': if (i != 0) { return false; } if (ipAddress.charAt(length - 1) != ']') { return false; } offset = 1; if (length < 4) { return false; } break; case ']': if (i != length - 1) { return false; } if (ipAddress.charAt(0) != '[') { return false; } break; case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(offset) != ':' && ipAddress.charAt(1 + offset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == offset && (ipAddress.length() <= i || ipAddress.charAt(i + 1) != ':')) { return false; } numberOfColons ++; if (numberOfColons > 7) { return false; } if (numberOfPeriods > 0) { return false; } if (prevChar == ':') { if (doubleColon) { return false; } doubleColon = true; } word.delete(0, word.length()); break; case '%': if (numberOfColons == 0) { return false; } numberOfPercent ++; if (i + 1 >= length) { return false; } try { Integer.parseInt(ipAddress.substring(i + 1)); } catch (NumberFormatException e) { return false; } break; default: if (numberOfPercent == 0) { if (word != null && word.length() > 3) { return false; } if (!isValidHexChar(c)) { return false; } } word.append(c); } } if (numberOfPeriods > 0) { if (numberOfPeriods != 3 || !(isValidIp4Word(word.toString()) && numberOfColons < 7)) { return false; } } else { if (numberOfColons != 7 && !doubleColon) { return false; } if (numberOfPercent == 0) { if (word.length() == 0 && ipAddress.charAt(length - 1 - offset) == ':' && ipAddress.charAt(length - 2 - offset) != ':') { return false; } } } return true; } private  NetUtil(); }
NetUtil { public static boolean isValidIpV6Address(String ipAddress) { int length = ipAddress.length(); boolean doubleColon = false; int numberOfColons = 0; int numberOfPeriods = 0; int numberOfPercent = 0; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int offset = 0; if (length < 2) { return false; } for (int i = 0; i < length; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '[': if (i != 0) { return false; } if (ipAddress.charAt(length - 1) != ']') { return false; } offset = 1; if (length < 4) { return false; } break; case ']': if (i != length - 1) { return false; } if (ipAddress.charAt(0) != '[') { return false; } break; case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(offset) != ':' && ipAddress.charAt(1 + offset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == offset && (ipAddress.length() <= i || ipAddress.charAt(i + 1) != ':')) { return false; } numberOfColons ++; if (numberOfColons > 7) { return false; } if (numberOfPeriods > 0) { return false; } if (prevChar == ':') { if (doubleColon) { return false; } doubleColon = true; } word.delete(0, word.length()); break; case '%': if (numberOfColons == 0) { return false; } numberOfPercent ++; if (i + 1 >= length) { return false; } try { Integer.parseInt(ipAddress.substring(i + 1)); } catch (NumberFormatException e) { return false; } break; default: if (numberOfPercent == 0) { if (word != null && word.length() > 3) { return false; } if (!isValidHexChar(c)) { return false; } } word.append(c); } } if (numberOfPeriods > 0) { if (numberOfPeriods != 3 || !(isValidIp4Word(word.toString()) && numberOfColons < 7)) { return false; } } else { if (numberOfColons != 7 && !doubleColon) { return false; } if (numberOfPercent == 0) { if (word.length() == 0 && ipAddress.charAt(length - 1 - offset) == ':' && ipAddress.charAt(length - 2 - offset) != ':') { return false; } } } return true; } private  NetUtil(); static byte[] createByteArrayFromIpAddressString(String ipAddressString); static boolean isValidIpV6Address(String ipAddress); static boolean isValidIp4Word(String word); static boolean isValidIpV4Address(String value); }
NetUtil { public static boolean isValidIpV6Address(String ipAddress) { int length = ipAddress.length(); boolean doubleColon = false; int numberOfColons = 0; int numberOfPeriods = 0; int numberOfPercent = 0; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int offset = 0; if (length < 2) { return false; } for (int i = 0; i < length; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '[': if (i != 0) { return false; } if (ipAddress.charAt(length - 1) != ']') { return false; } offset = 1; if (length < 4) { return false; } break; case ']': if (i != length - 1) { return false; } if (ipAddress.charAt(0) != '[') { return false; } break; case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(offset) != ':' && ipAddress.charAt(1 + offset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == offset && (ipAddress.length() <= i || ipAddress.charAt(i + 1) != ':')) { return false; } numberOfColons ++; if (numberOfColons > 7) { return false; } if (numberOfPeriods > 0) { return false; } if (prevChar == ':') { if (doubleColon) { return false; } doubleColon = true; } word.delete(0, word.length()); break; case '%': if (numberOfColons == 0) { return false; } numberOfPercent ++; if (i + 1 >= length) { return false; } try { Integer.parseInt(ipAddress.substring(i + 1)); } catch (NumberFormatException e) { return false; } break; default: if (numberOfPercent == 0) { if (word != null && word.length() > 3) { return false; } if (!isValidHexChar(c)) { return false; } } word.append(c); } } if (numberOfPeriods > 0) { if (numberOfPeriods != 3 || !(isValidIp4Word(word.toString()) && numberOfColons < 7)) { return false; } } else { if (numberOfColons != 7 && !doubleColon) { return false; } if (numberOfPercent == 0) { if (word.length() == 0 && ipAddress.charAt(length - 1 - offset) == ':' && ipAddress.charAt(length - 2 - offset) != ':') { return false; } } } return true; } private  NetUtil(); static byte[] createByteArrayFromIpAddressString(String ipAddressString); static boolean isValidIpV6Address(String ipAddress); static boolean isValidIp4Word(String word); static boolean isValidIpV4Address(String value); static final Inet4Address LOCALHOST4; static final Inet6Address LOCALHOST6; static final InetAddress LOCALHOST; static final NetworkInterface LOOPBACK_IF; static final int SOMAXCONN; }