diff --git a/src/main/java/com/dispose/common/Helper.java b/src/main/java/com/dispose/common/Helper.java index 42e295d4..36a6c5d0 100644 --- a/src/main/java/com/dispose/common/Helper.java +++ b/src/main/java/com/dispose/common/Helper.java @@ -16,7 +16,10 @@ import java.nio.charset.StandardCharsets; import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; @@ -130,19 +133,33 @@ public class Helper { * @return the string */ public static String ipAddrToFullFormat(String ipAddr) { - String[] ipArrays = ipAddr.split("-"); + List formatString = new ArrayList<>(); + + for (String v : ipAddr.split(DisposeConfigValue.SPLIT_CHAR)) { + if (v.contains("/")) { + String[] ret = v.split("/"); + IPAddressString addrString = new IPAddressString(ret[0]); + IPAddress addr = addrString.getAddress(); + formatString.add(addr.toNormalizedString().toUpperCase() + "/" + ret[1]); + } else if (v.contains("-")) { + String[] ret = v.split("-"); + + for (int i = 0; i < ret.length; i++) { + IPAddressString addrString = new IPAddressString(ret[i]); + IPAddress addr = addrString.getAddress(); + ret[i] = addr.toNormalizedString().toUpperCase(); + } + + formatString.add(ret[0] + "-" + ret[1]); + } else { + IPAddressString addrString = new IPAddressString(v); + IPAddress addr = addrString.getAddress(); + formatString.add(addr.toNormalizedString().toUpperCase()); + } - for(int i = 0; i < ipArrays.length; i++) { - IPAddressString addrString = new IPAddressString(ipArrays[i]); - IPAddress addr = addrString.getAddress(); - ipArrays[i] = addr.toFullString().toUpperCase(); } - if(ipArrays.length == 1) { - return ipArrays[0]; - } else { - return ipArrays[0] + "-" + ipArrays[1]; - } + return formatString.stream().map(Objects::toString).collect(Collectors.joining(",")); } /** @@ -220,7 +237,8 @@ public class Helper { public static String inputStream2String(InputStream inputStream) { StringBuilder sb = new StringBuilder(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, + StandardCharsets.UTF_8))) { String line; while ((line = reader.readLine()) != null) { sb.append(line); diff --git a/src/test/java/com/dispose/test/dev/debug/demo.java b/src/test/java/com/dispose/test/dev/debug/demo.java index ec9e387f..2f955ab6 100644 --- a/src/test/java/com/dispose/test/dev/debug/demo.java +++ b/src/test/java/com/dispose/test/dev/debug/demo.java @@ -523,20 +523,22 @@ public class demo { /** * Ip segment. */ - public void ipSegment() { - String str = "192.168.1.1/23, 192.168.1.2-192.168.1.80, ::1-::255, 192.168.1, 01.012.012.01, " + - "123.123.123-"; + @Test + public void ipSegmentTest() { + String str = "192.168.1.1/23, 192.168.1.2-192.168.1.80, ::1-::255, 01.012.012.01, ::1/24, ::02:23"; String fIpAddr = ""; for (String s : str.split(DisposeConfigValue.SPLIT_CHAR)) { - log.info("process string: {}", s); +// log.info("process string: {}", s); +// +// String normalString = s.replace(" ", ""); +// +// if (!Pattern.matches(ConstValue.ipAddrSegmentReg(), normalString)) { +// log.error("Step1: {} not match", normalString); +// } - String normalString = s.replace(" ", ""); - - if (!Pattern.matches(ConstValue.ipAddrSegmentReg(), normalString)) { - log.error("Step1: {} not match", normalString); - } + log.info("Format: {} --> {}", s, Helper.ipAddrToFullFormat(s)); // if (normalString.indexOf("-") == normalString.lastIndexOf("-")) { // if(normalString.contains("-")) {