NetUtils { public static String ipAddressToUrlString(InetAddress address) { if (address == null) { throw new NullPointerException("address is null"); } else if (address instanceof Inet4Address) { return address.getHostAddress(); } else if (address instanceof Inet6Address) { return '[' + InetAddresses.toAddrString(address) + ']'; } else { throw new IllegalArgumentException("Unrecognized type of InetAddress: " + address); } }  static String getHostnameFromFQDN(String fqdn); static URL getCorrectHostnamePort(String hostPort); static int getAvailablePort(); static String ipAddressToUrlString(InetAddress address); static String ipAddressAndPortToUrlString(InetAddress address, int port); static String socketAddressToUrlString(InetSocketAddress address); static String hostAndPortToUrlString(String host, int port); static Iterator<Integer> getPortRangeFromString(String rangeDefinition); static ServerSocket createSocketFromPorts(Iterator<Integer> portsIterator, SocketFactory factory);  }
@Test public void testIPv4toURL() { try { final String addressString = "192.168.0.1"; InetAddress address = InetAddress.getByName(addressString); assertEquals(addressString, NetUtils.ipAddressToUrlString(address)); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testIPv6toURL() { try { final String addressString = "2001:01db8:00:0:00:ff00:42:8329"; final String normalizedAddress = "[2001:1db8::ff00:42:8329]"; InetAddress address = InetAddress.getByName(addressString); assertEquals(normalizedAddress, NetUtils.ipAddressToUrlString(address)); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }