AddressUtils { public static InetSocketAddress parseAddress(String address, int defaultPort) { requireNonNull(address, "address"); int separatorIdx = address.lastIndexOf(':'); int ipV6HostSeparatorIdx = address.lastIndexOf(']'); if (separatorIdx > ipV6HostSeparatorIdx) { if (separatorIdx == address.indexOf(':') || ipV6HostSeparatorIdx > -1) { String port = address.substring(separatorIdx + 1); if (port.chars().allMatch(Character::isDigit)) { return AddressUtils.createUnresolved(address.substring(0, separatorIdx), Integer.parseInt(port)); } else { return AddressUtils.createUnresolved(address.substring(0, separatorIdx), defaultPort); } } } return AddressUtils.createUnresolved(address, defaultPort); } private  AddressUtils(); static InetSocketAddress createInetSocketAddress(String hostname, int port, boolean resolve); static InetSocketAddress createResolved(String hostname, int port); static InetSocketAddress createUnresolved(String hostname, int port); static InetSocketAddress parseAddress(String address, int defaultPort); static InetSocketAddress replaceUnresolvedNumericIp(InetSocketAddress inetSocketAddress); static InetSocketAddress replaceWithResolved(InetSocketAddress inetSocketAddress); static SocketAddress updateHost(@Nullable Supplier<? extends SocketAddress> address, String host); static SocketAddress updatePort(@Nullable Supplier<? extends SocketAddress> address, int port);  }
@Test public void shouldParseAddressForIPv4() { InetSocketAddress socketAddress = AddressUtils.parseAddress("127.0.0.1", 80); assertThat(socketAddress.isUnresolved()).isFalse(); assertThat(socketAddress.getAddress().getHostAddress()).isEqualTo("127.0.0.1"); assertThat(socketAddress.getPort()).isEqualTo(80); assertThat(socketAddress.getHostString()).isEqualTo("127.0.0.1"); }
@Test public void shouldParseAddressForIPv4WithPort() { InetSocketAddress socketAddress = AddressUtils.parseAddress("127.0.0.1:8080", 80); assertThat(socketAddress.isUnresolved()).isFalse(); assertThat(socketAddress.getAddress().getHostAddress()).isEqualTo("127.0.0.1"); assertThat(socketAddress.getPort()).isEqualTo(8080); assertThat(socketAddress.getHostString()).isEqualTo("127.0.0.1"); }
@Test public void shouldParseAddressForIPv6() { InetSocketAddress socketAddress = AddressUtils.parseAddress("[1abc:2abc:3abc::5ABC:6abc]", 80); assertThat(socketAddress.isUnresolved()).isFalse(); assertThat(socketAddress.getAddress().getHostAddress()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); assertThat(socketAddress.getPort()).isEqualTo(80); assertThat(socketAddress.getHostString()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); }
@Test public void shouldParseAddressForIPv6WithPort() { InetSocketAddress socketAddress = AddressUtils.parseAddress("[1abc:2abc:3abc::5ABC:6abc]:8080", 80); assertThat(socketAddress.isUnresolved()).isFalse(); assertThat(socketAddress.getAddress().getHostAddress()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); assertThat(socketAddress.getPort()).isEqualTo(8080); assertThat(socketAddress.getHostString()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); }
@Test public void shouldParseAddressForIPv6WithoutBrackets() { InetSocketAddress socketAddress = AddressUtils.parseAddress("1abc:2abc:3abc:0:0:0:5abc:6abc", 80); assertThat(socketAddress.isUnresolved()).isFalse(); assertThat(socketAddress.getAddress().getHostAddress()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); assertThat(socketAddress.getPort()).isEqualTo(80); assertThat(socketAddress.getHostString()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); }
@Test public void shouldParseAddressForIPv6WithNotNumericPort() { InetSocketAddress socketAddress = AddressUtils.parseAddress("[1abc:2abc:3abc::5ABC:6abc]:abc42", 80); assertThat(socketAddress.isUnresolved()).isFalse(); assertThat(socketAddress.getAddress().getHostAddress()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); assertThat(socketAddress.getPort()).isEqualTo(80); assertThat(socketAddress.getHostString()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); }
@Test public void shouldParseAddressForHostName() { InetSocketAddress socketAddress = AddressUtils.parseAddress("example.com", 80); assertThat(socketAddress.isUnresolved()).isTrue(); assertThat(socketAddress.getPort()).isEqualTo(80); assertThat(socketAddress.getHostString()).isEqualTo("example.com"); }
@Test public void shouldParseAddressForHostNameWithPort() { InetSocketAddress socketAddress = AddressUtils.parseAddress("example.com:8080", 80); assertThat(socketAddress.isUnresolved()).isTrue(); assertThat(socketAddress.getPort()).isEqualTo(8080); assertThat(socketAddress.getHostString()).isEqualTo("example.com"); }
@Test public void parseAddressBadValues() { assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> AddressUtils.parseAddress(null, 0)) .withMessage("address"); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> AddressUtils.parseAddress("address", -1)) .withMessage("port out of range:-1"); }