@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; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int startOffset = 0; int endOffset = ipAddress.length(); if (endOffset < 2) { return false; } if (ipAddress.charAt(0) == '[') { if (ipAddress.charAt(endOffset - 1) != ']') { return false; } startOffset = 1; endOffset --; } int percentIdx = ipAddress.indexOf('%', startOffset); if (percentIdx >= 0) { endOffset = percentIdx; } for (int i = startOffset; i < endOffset; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(startOffset) != ':' && ipAddress.charAt(1 + startOffset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == startOffset && (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; default: 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 (word.length() == 0 && ipAddress.charAt(length - 1 - startOffset) == ':' && ipAddress.charAt(length - 2 - startOffset) != ':') { return false; } } return true; }
NetUtil { public static boolean isValidIpV6Address(String ipAddress) { int length = ipAddress.length(); boolean doubleColon = false; int numberOfColons = 0; int numberOfPeriods = 0; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int startOffset = 0; int endOffset = ipAddress.length(); if (endOffset < 2) { return false; } if (ipAddress.charAt(0) == '[') { if (ipAddress.charAt(endOffset - 1) != ']') { return false; } startOffset = 1; endOffset --; } int percentIdx = ipAddress.indexOf('%', startOffset); if (percentIdx >= 0) { endOffset = percentIdx; } for (int i = startOffset; i < endOffset; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(startOffset) != ':' && ipAddress.charAt(1 + startOffset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == startOffset && (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; default: 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 (word.length() == 0 && ipAddress.charAt(length - 1 - startOffset) == ':' && ipAddress.charAt(length - 2 - startOffset) != ':') { return false; } } return true; } }
NetUtil { public static boolean isValidIpV6Address(String ipAddress) { int length = ipAddress.length(); boolean doubleColon = false; int numberOfColons = 0; int numberOfPeriods = 0; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int startOffset = 0; int endOffset = ipAddress.length(); if (endOffset < 2) { return false; } if (ipAddress.charAt(0) == '[') { if (ipAddress.charAt(endOffset - 1) != ']') { return false; } startOffset = 1; endOffset --; } int percentIdx = ipAddress.indexOf('%', startOffset); if (percentIdx >= 0) { endOffset = percentIdx; } for (int i = startOffset; i < endOffset; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(startOffset) != ':' && ipAddress.charAt(1 + startOffset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == startOffset && (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; default: 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 (word.length() == 0 && ipAddress.charAt(length - 1 - startOffset) == ':' && ipAddress.charAt(length - 2 - startOffset) != ':') { 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; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int startOffset = 0; int endOffset = ipAddress.length(); if (endOffset < 2) { return false; } if (ipAddress.charAt(0) == '[') { if (ipAddress.charAt(endOffset - 1) != ']') { return false; } startOffset = 1; endOffset --; } int percentIdx = ipAddress.indexOf('%', startOffset); if (percentIdx >= 0) { endOffset = percentIdx; } for (int i = startOffset; i < endOffset; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(startOffset) != ':' && ipAddress.charAt(1 + startOffset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == startOffset && (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; default: 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 (word.length() == 0 && ipAddress.charAt(length - 1 - startOffset) == ':' && ipAddress.charAt(length - 2 - startOffset) != ':') { return false; } } return true; } private  NetUtil(); static boolean isIpV4StackPreferred(); static byte[] createByteArrayFromIpAddressString(String ipAddressString); static boolean isValidIpV6Address(String ipAddress); @Deprecated static boolean isValidIp4Word(String word); static boolean isValidIpV4Address(String value); static Inet6Address getByName(CharSequence ip); static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped); static String toAddressString(InetAddress ip); static String toAddressString(InetAddress ip, boolean ipv4Mapped); }
NetUtil { public static boolean isValidIpV6Address(String ipAddress) { int length = ipAddress.length(); boolean doubleColon = false; int numberOfColons = 0; int numberOfPeriods = 0; StringBuilder word = new StringBuilder(); char c = 0; char prevChar; int startOffset = 0; int endOffset = ipAddress.length(); if (endOffset < 2) { return false; } if (ipAddress.charAt(0) == '[') { if (ipAddress.charAt(endOffset - 1) != ']') { return false; } startOffset = 1; endOffset --; } int percentIdx = ipAddress.indexOf('%', startOffset); if (percentIdx >= 0) { endOffset = percentIdx; } for (int i = startOffset; i < endOffset; i ++) { prevChar = c; c = ipAddress.charAt(i); switch (c) { case '.': numberOfPeriods ++; if (numberOfPeriods > 3) { return false; } if (!isValidIp4Word(word.toString())) { return false; } if (numberOfColons != 6 && !doubleColon) { return false; } if (numberOfColons == 7 && ipAddress.charAt(startOffset) != ':' && ipAddress.charAt(1 + startOffset) != ':') { return false; } word.delete(0, word.length()); break; case ':': if (i == startOffset && (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; default: 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 (word.length() == 0 && ipAddress.charAt(length - 1 - startOffset) == ':' && ipAddress.charAt(length - 2 - startOffset) != ':') { return false; } } return true; } private  NetUtil(); static boolean isIpV4StackPreferred(); static byte[] createByteArrayFromIpAddressString(String ipAddressString); static boolean isValidIpV6Address(String ipAddress); @Deprecated static boolean isValidIp4Word(String word); static boolean isValidIpV4Address(String value); static Inet6Address getByName(CharSequence ip); static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped); static String toAddressString(InetAddress ip); static String toAddressString(InetAddress ip, boolean ipv4Mapped); static final Inet4Address LOCALHOST4; static final Inet6Address LOCALHOST6; static final InetAddress LOCALHOST; static final NetworkInterface LOOPBACK_IF; static final int SOMAXCONN; }