NetworkUtil { public static String defaultPort(String maybeHostPort, int defaultPort) { Pair<String, Integer> decoded = extractHostPort(maybeHostPort, defaultPort); return encodeIPv6LiteralHost(decoded.getLeft()) + ":" + decoded.getRight(); } private  NetworkUtil(); static void configure(SocketChannel sc); static void closeQuietly(Closeable closeable); static URI toUri(HttpHost host); static URI toUri(HttpHost host, String path); static URI appendUriPath(URI uri, String... pathSegments); static URIBuilder builderFrom(HttpHost host); static String toHostPort(String host, String port); static String toHostPort(String host, int port); static String toHostPort(InetSocketAddress address); static InetSocketAddress parseInetSocketAddress(String hostPortString); static InetSocketAddress toInetSocketAddress(String maybeLiteralHost, int port); static List<InetSocketAddress> parseInetSocketAddresses(String... hostPortStrings); static String defaultPort(String maybeHostPort, int defaultPort); static Pair<String, Integer> extractHostPort(String maybeHostPort, int defaultPort); static String encodeIPv6LiteralHost(String hostname); static String decodeIPv6LiteralHost(String hostname); static ByteBuffer enlargeSslPacketBuffer(SSLEngine engine, ByteBuffer buffer); static ByteBuffer enlargeSslApplicationBuffer(SSLEngine engine, ByteBuffer buffer); static ByteBuffer enlargeSslBuffer(ByteBuffer src, int sessionProposedCapacity); static InetSocketAddress ensureUnresolved(InetSocketAddress address); static InetSocketAddress ensureResolved(InetSocketAddress address); static InetSocketAddress refresh(InetSocketAddress original);  }
@Test public void testDefaultPort() { Assert.assertEquals("127.0.0.1:1234", NetworkUtil.defaultPort("127.0.0.1:1234", 9999)); Assert.assertEquals("127.0.0.1:9999", NetworkUtil.defaultPort("127.0.0.1", 9999)); Assert.assertEquals("[::1]:1234", NetworkUtil.defaultPort("[::1]:1234", 9999)); Assert.assertEquals("[::1]:9999", NetworkUtil.defaultPort("::1", 9999)); Assert.assertEquals("localhost.localdomain.local:9999", NetworkUtil.defaultPort("localhost.localdomain.local", 9999)); Assert.assertEquals("localhost.localdomain.local:1234", NetworkUtil.defaultPort("localhost.localdomain.local:1234", 9999)); }