@Test public void testIsValidIpV6Address() { for (String host : validIpV6Hosts.keySet()) { assertTrue(host, isValidIpV6Address(host)); if (host.charAt(0) != '[' && !host.contains("%")) { assertNotNull(host, getByName(host, true)); String hostMod = '[' + host + ']'; assertTrue(hostMod, isValidIpV6Address(hostMod)); hostMod = host + '%'; assertTrue(hostMod, isValidIpV6Address(hostMod)); hostMod = host + "%eth1"; assertTrue(hostMod, isValidIpV6Address(hostMod)); hostMod = '[' + host + "%]"; assertTrue(hostMod, isValidIpV6Address(hostMod)); hostMod = '[' + host + "%1]"; assertTrue(hostMod, isValidIpV6Address(hostMod)); hostMod = '[' + host + "]%"; assertFalse(hostMod, isValidIpV6Address(hostMod)); hostMod = '[' + host + "]%1"; assertFalse(hostMod, isValidIpV6Address(hostMod)); } } for (String host : invalidIpV6Hosts.keySet()) { assertFalse(host, isValidIpV6Address(host)); assertNull(host, getByName(host)); String hostMod = '[' + host + ']'; assertFalse(hostMod, isValidIpV6Address(hostMod)); hostMod = host + '%'; assertFalse(hostMod, isValidIpV6Address(hostMod)); hostMod = host + "%eth1"; assertFalse(hostMod, isValidIpV6Address(hostMod)); hostMod = '[' + host + "%]"; assertFalse(hostMod, isValidIpV6Address(hostMod)); hostMod = '[' + host + "%1]"; assertFalse(hostMod, isValidIpV6Address(hostMod)); hostMod = '[' + host + "]%"; assertFalse(hostMod, isValidIpV6Address(hostMod)); hostMod = '[' + host + "]%1"; assertFalse(hostMod, isValidIpV6Address(hostMod)); hostMod = host + ']'; assertFalse(hostMod, isValidIpV6Address(hostMod)); hostMod = '[' + host; assertFalse(hostMod, isValidIpV6Address(hostMod)); } }
public static boolean isValidIpV6Address(String ip) { int end = ip.length(); if (end < 2) { return false; } int start; char c = ip.charAt(0); if (c == '[') { end--; if (ip.charAt(end) != ']') { return false; } start = 1; c = ip.charAt(1); } else { start = 0; } int colons; int compressBegin; if (c == ':') { if (ip.charAt(start + 1) != ':') { return false; } colons = 2; compressBegin = start; start += 2; } else { colons = 0; compressBegin = -1; } int wordLen = 0; loop: for (int i = start; i < end; i++) { c = ip.charAt(i); if (isValidHexChar(c)) { if (wordLen < 4) { wordLen++; continue; } return false; } switch (c) { case ':': if (colons > 7) { return false; } if (ip.charAt(i - 1) == ':') { if (compressBegin >= 0) { return false; } compressBegin = i - 1; } else { wordLen = 0; } colons++; break; case '.': if (compressBegin < 0 && colons != 6 || (colons == 7 && compressBegin >= start || colons > 7)) { return false; } int ipv4Start = i - wordLen; int j = ipv4Start - 2; if (isValidIPv4MappedChar(ip.charAt(j))) { if (!isValidIPv4MappedChar(ip.charAt(j - 1)) || !isValidIPv4MappedChar(ip.charAt(j - 2)) || !isValidIPv4MappedChar(ip.charAt(j - 3))) { return false; } j -= 5; } for (; j >= start; --j) { char tmpChar = ip.charAt(j); if (tmpChar != '0' && tmpChar != ':') { return false; } } int ipv4End = ip.indexOf('%', ipv4Start + 7); if (ipv4End < 0) { ipv4End = end; } return isValidIpV4Address(ip, ipv4Start, ipv4End); case '%': end = i; break loop; default: return false; } } if (compressBegin < 0) { return colons == 7 && wordLen > 0; } return compressBegin + 2 == end || wordLen > 0 && (colons < 8 || compressBegin <= start); }
NetUtil { public static boolean isValidIpV6Address(String ip) { int end = ip.length(); if (end < 2) { return false; } int start; char c = ip.charAt(0); if (c == '[') { end--; if (ip.charAt(end) != ']') { return false; } start = 1; c = ip.charAt(1); } else { start = 0; } int colons; int compressBegin; if (c == ':') { if (ip.charAt(start + 1) != ':') { return false; } colons = 2; compressBegin = start; start += 2; } else { colons = 0; compressBegin = -1; } int wordLen = 0; loop: for (int i = start; i < end; i++) { c = ip.charAt(i); if (isValidHexChar(c)) { if (wordLen < 4) { wordLen++; continue; } return false; } switch (c) { case ':': if (colons > 7) { return false; } if (ip.charAt(i - 1) == ':') { if (compressBegin >= 0) { return false; } compressBegin = i - 1; } else { wordLen = 0; } colons++; break; case '.': if (compressBegin < 0 && colons != 6 || (colons == 7 && compressBegin >= start || colons > 7)) { return false; } int ipv4Start = i - wordLen; int j = ipv4Start - 2; if (isValidIPv4MappedChar(ip.charAt(j))) { if (!isValidIPv4MappedChar(ip.charAt(j - 1)) || !isValidIPv4MappedChar(ip.charAt(j - 2)) || !isValidIPv4MappedChar(ip.charAt(j - 3))) { return false; } j -= 5; } for (; j >= start; --j) { char tmpChar = ip.charAt(j); if (tmpChar != '0' && tmpChar != ':') { return false; } } int ipv4End = ip.indexOf('%', ipv4Start + 7); if (ipv4End < 0) { ipv4End = end; } return isValidIpV4Address(ip, ipv4Start, ipv4End); case '%': end = i; break loop; default: return false; } } if (compressBegin < 0) { return colons == 7 && wordLen > 0; } return compressBegin + 2 == end || wordLen > 0 && (colons < 8 || compressBegin <= start); } }
NetUtil { public static boolean isValidIpV6Address(String ip) { int end = ip.length(); if (end < 2) { return false; } int start; char c = ip.charAt(0); if (c == '[') { end--; if (ip.charAt(end) != ']') { return false; } start = 1; c = ip.charAt(1); } else { start = 0; } int colons; int compressBegin; if (c == ':') { if (ip.charAt(start + 1) != ':') { return false; } colons = 2; compressBegin = start; start += 2; } else { colons = 0; compressBegin = -1; } int wordLen = 0; loop: for (int i = start; i < end; i++) { c = ip.charAt(i); if (isValidHexChar(c)) { if (wordLen < 4) { wordLen++; continue; } return false; } switch (c) { case ':': if (colons > 7) { return false; } if (ip.charAt(i - 1) == ':') { if (compressBegin >= 0) { return false; } compressBegin = i - 1; } else { wordLen = 0; } colons++; break; case '.': if (compressBegin < 0 && colons != 6 || (colons == 7 && compressBegin >= start || colons > 7)) { return false; } int ipv4Start = i - wordLen; int j = ipv4Start - 2; if (isValidIPv4MappedChar(ip.charAt(j))) { if (!isValidIPv4MappedChar(ip.charAt(j - 1)) || !isValidIPv4MappedChar(ip.charAt(j - 2)) || !isValidIPv4MappedChar(ip.charAt(j - 3))) { return false; } j -= 5; } for (; j >= start; --j) { char tmpChar = ip.charAt(j); if (tmpChar != '0' && tmpChar != ':') { return false; } } int ipv4End = ip.indexOf('%', ipv4Start + 7); if (ipv4End < 0) { ipv4End = end; } return isValidIpV4Address(ip, ipv4Start, ipv4End); case '%': end = i; break loop; default: return false; } } if (compressBegin < 0) { return colons == 7 && wordLen > 0; } return compressBegin + 2 == end || wordLen > 0 && (colons < 8 || compressBegin <= start); } private  NetUtil(); }
NetUtil { public static boolean isValidIpV6Address(String ip) { int end = ip.length(); if (end < 2) { return false; } int start; char c = ip.charAt(0); if (c == '[') { end--; if (ip.charAt(end) != ']') { return false; } start = 1; c = ip.charAt(1); } else { start = 0; } int colons; int compressBegin; if (c == ':') { if (ip.charAt(start + 1) != ':') { return false; } colons = 2; compressBegin = start; start += 2; } else { colons = 0; compressBegin = -1; } int wordLen = 0; loop: for (int i = start; i < end; i++) { c = ip.charAt(i); if (isValidHexChar(c)) { if (wordLen < 4) { wordLen++; continue; } return false; } switch (c) { case ':': if (colons > 7) { return false; } if (ip.charAt(i - 1) == ':') { if (compressBegin >= 0) { return false; } compressBegin = i - 1; } else { wordLen = 0; } colons++; break; case '.': if (compressBegin < 0 && colons != 6 || (colons == 7 && compressBegin >= start || colons > 7)) { return false; } int ipv4Start = i - wordLen; int j = ipv4Start - 2; if (isValidIPv4MappedChar(ip.charAt(j))) { if (!isValidIPv4MappedChar(ip.charAt(j - 1)) || !isValidIPv4MappedChar(ip.charAt(j - 2)) || !isValidIPv4MappedChar(ip.charAt(j - 3))) { return false; } j -= 5; } for (; j >= start; --j) { char tmpChar = ip.charAt(j); if (tmpChar != '0' && tmpChar != ':') { return false; } } int ipv4End = ip.indexOf('%', ipv4Start + 7); if (ipv4End < 0) { ipv4End = end; } return isValidIpV4Address(ip, ipv4Start, ipv4End); case '%': end = i; break loop; default: return false; } } if (compressBegin < 0) { return colons == 7 && wordLen > 0; } return compressBegin + 2 == end || wordLen > 0 && (colons < 8 || compressBegin <= start); } private  NetUtil(); static boolean isIpV4StackPreferred(); static boolean isIpV6AddressesPreferred(); static byte[] createByteArrayFromIpAddressString(String ipAddressString); static String intToIpAddress(int i); static String bytesToIpAddress(byte[] bytes); static String bytesToIpAddress(byte[] bytes, int offset, int length); static boolean isValidIpV6Address(String ip); static boolean isValidIpV4Address(String ip); static Inet6Address getByName(CharSequence ip); static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped); static String toSocketAddressString(InetSocketAddress addr); static String toSocketAddressString(String host, int port); static String toAddressString(InetAddress ip); static String toAddressString(InetAddress ip, boolean ipv4Mapped); }
NetUtil { public static boolean isValidIpV6Address(String ip) { int end = ip.length(); if (end < 2) { return false; } int start; char c = ip.charAt(0); if (c == '[') { end--; if (ip.charAt(end) != ']') { return false; } start = 1; c = ip.charAt(1); } else { start = 0; } int colons; int compressBegin; if (c == ':') { if (ip.charAt(start + 1) != ':') { return false; } colons = 2; compressBegin = start; start += 2; } else { colons = 0; compressBegin = -1; } int wordLen = 0; loop: for (int i = start; i < end; i++) { c = ip.charAt(i); if (isValidHexChar(c)) { if (wordLen < 4) { wordLen++; continue; } return false; } switch (c) { case ':': if (colons > 7) { return false; } if (ip.charAt(i - 1) == ':') { if (compressBegin >= 0) { return false; } compressBegin = i - 1; } else { wordLen = 0; } colons++; break; case '.': if (compressBegin < 0 && colons != 6 || (colons == 7 && compressBegin >= start || colons > 7)) { return false; } int ipv4Start = i - wordLen; int j = ipv4Start - 2; if (isValidIPv4MappedChar(ip.charAt(j))) { if (!isValidIPv4MappedChar(ip.charAt(j - 1)) || !isValidIPv4MappedChar(ip.charAt(j - 2)) || !isValidIPv4MappedChar(ip.charAt(j - 3))) { return false; } j -= 5; } for (; j >= start; --j) { char tmpChar = ip.charAt(j); if (tmpChar != '0' && tmpChar != ':') { return false; } } int ipv4End = ip.indexOf('%', ipv4Start + 7); if (ipv4End < 0) { ipv4End = end; } return isValidIpV4Address(ip, ipv4Start, ipv4End); case '%': end = i; break loop; default: return false; } } if (compressBegin < 0) { return colons == 7 && wordLen > 0; } return compressBegin + 2 == end || wordLen > 0 && (colons < 8 || compressBegin <= start); } private  NetUtil(); static boolean isIpV4StackPreferred(); static boolean isIpV6AddressesPreferred(); static byte[] createByteArrayFromIpAddressString(String ipAddressString); static String intToIpAddress(int i); static String bytesToIpAddress(byte[] bytes); static String bytesToIpAddress(byte[] bytes, int offset, int length); static boolean isValidIpV6Address(String ip); static boolean isValidIpV4Address(String ip); static Inet6Address getByName(CharSequence ip); static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped); static String toSocketAddressString(InetSocketAddress addr); static String toSocketAddressString(String host, int port); 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; }