mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
optimize split IPAndPort
This commit is contained in:
@@ -7,6 +7,7 @@ import java.net.ServerSocket;
|
||||
import java.util.Enumeration;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -75,4 +76,27 @@ public class AddressUtils {
|
||||
logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
|
||||
return localAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拆分IP地址和端口号
|
||||
*
|
||||
* @param text ip地址和端口号,ip和端口号以英文冒号(:)分隔;
|
||||
*
|
||||
* <pre>
|
||||
* ipv4 127.0.0.1:3306
|
||||
* ipv6 [::1]:3306
|
||||
* </pre>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String[] splitIPAndPort(String text) {
|
||||
text = text.replace("[", "").replace("]", "");
|
||||
int idx = text.lastIndexOf(':');
|
||||
if (idx > 0) {
|
||||
String ip = text.substring(0, idx);
|
||||
String port = text.substring(idx + 1);
|
||||
return new String[] { ip, port };
|
||||
}
|
||||
return ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user