@Test public void setPortTest() { assertEquals("localhost:123", setPort("localhost", 123)); assertEquals("localhost:123", setPort("localhost:1586", 123)); assertEquals("[2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d]:123", setPort("[2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d]", 123)); assertEquals("[2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d]:123", setPort("[2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d]:154542", 123)); }
public static String setPort(String address, int port) { Assert.notNull(address, "Address is null or empty"); Assert.isTrue(port > 0 && port < Short.MAX_VALUE, "Invalid port number: " + port); int from = 0; if(isIpv6(address)) { from = address.indexOf(']'); } int portStart = address.lastIndexOf(':'); String hostAndProto = address; if(portStart > from) { hostAndProto = address.substring(0, portStart); } return hostAndProto + ":" + port; }
AddressUtils { public static String setPort(String address, int port) { Assert.notNull(address, "Address is null or empty"); Assert.isTrue(port > 0 && port < Short.MAX_VALUE, "Invalid port number: " + port); int from = 0; if(isIpv6(address)) { from = address.indexOf(']'); } int portStart = address.lastIndexOf(':'); String hostAndProto = address; if(portStart > from) { hostAndProto = address.substring(0, portStart); } return hostAndProto + ":" + port; } }
AddressUtils { public static String setPort(String address, int port) { Assert.notNull(address, "Address is null or empty"); Assert.isTrue(port > 0 && port < Short.MAX_VALUE, "Invalid port number: " + port); int from = 0; if(isIpv6(address)) { from = address.indexOf(']'); } int portStart = address.lastIndexOf(':'); String hostAndProto = address; if(portStart > from) { hostAndProto = address.substring(0, portStart); } return hostAndProto + ":" + port; }  }
AddressUtils { public static String setPort(String address, int port) { Assert.notNull(address, "Address is null or empty"); Assert.isTrue(port > 0 && port < Short.MAX_VALUE, "Invalid port number: " + port); int from = 0; if(isIpv6(address)) { from = address.indexOf(']'); } int portStart = address.lastIndexOf(':'); String hostAndProto = address; if(portStart > from) { hostAndProto = address.substring(0, portStart); } return hostAndProto + ":" + port; }  static int getPort(String addr); static boolean hasPort(String addr); static String setPort(String address, int port); static boolean isIpv6(String url); static String getHostPort(String addr); static boolean isHttps(String url); static String getHost(String addr); static String setHost(String addr, String host); static boolean isLocal(String host); }
AddressUtils { public static String setPort(String address, int port) { Assert.notNull(address, "Address is null or empty"); Assert.isTrue(port > 0 && port < Short.MAX_VALUE, "Invalid port number: " + port); int from = 0; if(isIpv6(address)) { from = address.indexOf(']'); } int portStart = address.lastIndexOf(':'); String hostAndProto = address; if(portStart > from) { hostAndProto = address.substring(0, portStart); } return hostAndProto + ":" + port; }  static int getPort(String addr); static boolean hasPort(String addr); static String setPort(String address, int port); static boolean isIpv6(String url); static String getHostPort(String addr); static boolean isHttps(String url); static String getHost(String addr); static String setHost(String addr, String host); static boolean isLocal(String host);  }