Returns the string representation of an InetAddress. For IPv4 addresses, this is identical to InetAddress#getHostAddress(), but for IPv6 addresses, the output follows RFC 5952 section 4. The main difference is that this method uses
(InetAddress ip)
| 343 | |
| 344 | |
| 345 | public static String toAddrString(InetAddress ip) { |
| 346 | Preconditions.checkNotNull(ip); |
| 347 | if (ip instanceof Inet4Address) { |
| 348 | // For IPv4, Java's formatting is good enough. |
| 349 | return ip.getHostAddress(); |
| 350 | } |
| 351 | Preconditions.checkArgument(ip instanceof Inet6Address); |
| 352 | byte[] bytes = ip.getAddress(); |
| 353 | int[] hextets = new int[IPV6_PART_COUNT]; |
| 354 | for (int i = 0; i < hextets.length; i++) { |
| 355 | hextets[i] = Ints.fromBytes((byte) 0, (byte) 0, bytes[2 * i], bytes[2 * i + 1]); |
| 356 | } |
| 357 | compressLongestRunOfZeroes(hextets); |
| 358 | return hextetsToIPv6String(hextets); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Identify and mark the longest run of zeroes in an IPv6 address. |
no test coverage detected