From 74ca8eb1d08d1cfdffb870ea1f3cf861dc359cf7 Mon Sep 17 00:00:00 2001 From: emptypoint <1215582715@qq.com> Date: Sun, 12 Feb 2023 14:54:24 +0800 Subject: [PATCH] fix doc; --- .../java/cn/hutool/core/net/Ipv4Util.java | 210 ++++++++++-------- .../java/cn/hutool/core/net/Ipv4UtilTest.java | 57 ++++- 2 files changed, 169 insertions(+), 98 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/net/Ipv4Util.java b/hutool-core/src/main/java/cn/hutool/core/net/Ipv4Util.java index 6c8c89689..2745aa5e5 100755 --- a/hutool-core/src/main/java/cn/hutool/core/net/Ipv4Util.java +++ b/hutool-core/src/main/java/cn/hutool/core/net/Ipv4Util.java @@ -17,7 +17,19 @@ import java.util.regex.Matcher; * *
pr自:https://gitee.com/loolly/hutool/pulls/161
* + *名词解释: + *
方法别名:inet_aton
* - * @param strIP IP V4 地址 - * @return long值 + * @param strIp ip地址,点分十进制,xxx.xxx.xxx.xxx + * @return ip的long值 */ - public static long ipv4ToLong(final String strIP) { - final Matcher matcher = PatternPool.IPV4.matcher(strIP); + public static long ipv4ToLong(final String strIp) { + final Matcher matcher = PatternPool.IPV4.matcher(strIp); if (matcher.matches()) { return matchAddress(matcher); } @@ -189,11 +236,11 @@ public class Ipv4Util { } /** - * 根据 ip/掩码位 计算IP段的起始IP(字符串型) - * 方法别名:inet_ntoa + * 根据 ip 和 掩码位 获取 子网的起始IP(字符串型) + *方法别名:inet_ntoa
* - * @param ip 给定的IP,如218.240.38.69 - * @param maskBit 给定的掩码位,如30 + * @param ip 给定的IP,点分十进制,如:xxx.xxx.xxx.xxx + * @param maskBit 给定的掩码位,如:30 * @return 起始IP的字符串表示 */ public static String getBeginIpStr(final String ip, final int maskBit) { @@ -201,10 +248,10 @@ public class Ipv4Util { } /** - * 根据 ip/掩码位 计算IP段的起始IP(Long型) + * 根据 ip 和 掩码位 获取 子网的起始IP(Long型) * - * @param ip 给定的IP,如218.240.38.69 - * @param maskBit 给定的掩码位,如30 + * @param ip 给定的IP,点分十进制,如:xxx.xxx.xxx.xxx + * @param maskBit 给定的掩码位,如:30 * @return 起始IP的长整型表示 */ public static Long getBeginIpLong(final String ip, final int maskBit) { @@ -212,10 +259,10 @@ public class Ipv4Util { } /** - * 根据 ip/掩码位 计算IP段的终止IP(字符串型) + * 根据 ip 和 掩码位 获取 子网的终止IP(字符串型) * - * @param ip 给定的IP,如218.240.38.69 - * @param maskBit 给定的掩码位,如30 + * @param ip 给定的IP,点分十进制,如:xxx.xxx.xxx.xxx + * @param maskBit 给定的掩码位,如:30 * @return 终止IP的字符串表示 */ public static String getEndIpStr(final String ip, final int maskBit) { @@ -223,7 +270,18 @@ public class Ipv4Util { } /** - * 根据子网掩码转换为掩码位 + * 根据 ip 和 掩码位 获取 子网的终止IP(Long型) + * + * @param ip 给定的IP,点分十进制,如:xxx.xxx.xxx.xxx + * @param maskBit 给定的掩码位,如:30 + * @return 终止IP的长整型表示 + */ + public static Long getEndIpLong(final String ip, final int maskBit) { + return getBeginIpLong(ip, maskBit) + ~ipv4ToLong(getMaskByMaskBit(maskBit)); + } + + /** + * 将 子网掩码 转换为 掩码位 * * @param mask 掩码的点分十进制表示,例如 255.255.255.0 * @return 掩码位,例如 24 @@ -238,11 +296,11 @@ public class Ipv4Util { } /** - * 计算子网大小 + * 获取 子网内的 地址总数 * - * @param maskBit 掩码位 + * @param maskBit 掩码位,取值范围:[2, {@link #IP_MASK_MAX}] * @param isAll true:全量地址,false:可用地址 - * @return 地址总数 + * @return 子网内地址总数 */ public static int countByMaskBit(final int maskBit, final boolean isAll) { //如果是可用地址的情况,掩码位小于等于0或大于等于32,则可用地址为0 @@ -255,21 +313,21 @@ public class Ipv4Util { } /** - * 根据掩码位获取掩码 + * 根据 掩码位 获取 掩码地址 * - * @param maskBit 掩码位 - * @return 掩码 + * @param maskBit 掩码位,如:24,取值范围:[{@link #IP_MASK_MIN}, {@link #IP_MASK_MAX}] + * @return 掩码地址,点分十进制,如:255.255.255.0 */ public static String getMaskByMaskBit(final int maskBit) { return MaskBit.get(maskBit); } /** - * 根据开始IP与结束IP计算掩码 + * 根据 开始IP 与 结束IP 获取 掩码地址 * - * @param fromIp 开始IP - * @param toIp 结束IP - * @return 掩码x.x.x.x + * @param fromIp 开始IP,包含,点分十进制 + * @param toIp 结束IP,包含,点分十进制 + * @return 掩码地址,点分十进制 */ public static String getMaskByIpRange(final String fromIp, final String toIp) { final long toIpLong = ipv4ToLong(toIp); @@ -286,10 +344,10 @@ public class Ipv4Util { } /** - * 计算IP区间有多少个IP + * 获得 指定区间内的 ip数量 * - * @param fromIp 开始IP - * @param toIp 结束IP + * @param fromIp 开始IP,包含,点分十进制 + * @param toIp 结束IP,包含,点分十进制 * @return IP数量 */ public static int countByIpRange(final String fromIp, final String toIp) { @@ -320,7 +378,7 @@ public class Ipv4Util { /** * 判断掩码位是否合法 * - * @param maskBit 掩码位,例如 24 + * @param maskBit 掩码位,有效范围:[{@link #IP_MASK_MIN}, {@link #IP_MASK_MAX}] * @return true:掩码位合法;false:掩码位不合法 */ public static boolean isMaskBitValid(final int maskBit) { @@ -337,47 +395,25 @@ public class Ipv4Util { * * 当然,还有127这个网段是环回地址 * - * @param ipAddress IP地址 + * @param ipAddress IP地址,点分十进制 * @return 是否为内网IP * @since 5.7.18 */ public static boolean isInnerIP(final String ipAddress) { - final boolean isInnerIp; final long ipNum = ipv4ToLong(ipAddress); - - final long aBegin = ipv4ToLong("10.0.0.0"); - final long aEnd = ipv4ToLong("10.255.255.255"); - - final long bBegin = ipv4ToLong("172.16.0.0"); - final long bEnd = ipv4ToLong("172.31.255.255"); - - final long cBegin = ipv4ToLong("192.168.0.0"); - final long cEnd = ipv4ToLong("192.168.255.255"); - - isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || LOCAL_IP.equals(ipAddress); - return isInnerIp; + return isBetween(ipNum, A_INNER_IP_LONG_BEGIN, A_INNER_IP_LONG_END) + || isBetween(ipNum, B_INNER_IP_LONG_BEGIN, B_INNER_IP_LONG_END) + || isBetween(ipNum, C_INNER_IP_LONG_BEGIN, C_INNER_IP_LONG_END) + || LOCAL_IP.equals(ipAddress); } //-------------------------------------------------------------------------------- Private method start /** - * 根据 ip/掩码位 计算IP段的终止IP(Long型) - * 注:此接口返回负数,请使用转成字符串后再转Long型 - * - * @param ip 给定的IP,如218.240.38.69 - * @param maskBit 给定的掩码位,如30 - * @return 终止IP的长整型表示 - */ - public static Long getEndIpLong(final String ip, final int maskBit) { - return getBeginIpLong(ip, maskBit) - + ~ipv4ToLong(getMaskByMaskBit(maskBit)); - } - - /** - * 将匹配到的Ipv4地址的4个分组分别处理 + * 将匹配到的Ipv4地址转为Long类型 * * @param matcher 匹配到的Ipv4正则 - * @return ipv4对应long + * @return ip的long值 */ private static long matchAddress(final Matcher matcher) { long addr = 0; @@ -388,14 +424,14 @@ public class Ipv4Util { } /** - * 指定IP的long是否在指定范围内 + * 指定IP是否在指定范围内 * * @param userIp 用户IP - * @param begin 开始IP - * @param end 结束IP + * @param begin 开始IP,包含 + * @param end 结束IP,包含 * @return 是否在范围内 */ - private static boolean isInner(final long userIp, final long begin, final long end) { + private static boolean isBetween(final long userIp, final long begin, final long end) { return (userIp >= begin) && (userIp <= end); } //-------------------------------------------------------------------------------- Private method end diff --git a/hutool-core/src/test/java/cn/hutool/core/net/Ipv4UtilTest.java b/hutool-core/src/test/java/cn/hutool/core/net/Ipv4UtilTest.java index e98f646fa..97497d5b4 100644 --- a/hutool-core/src/test/java/cn/hutool/core/net/Ipv4UtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/net/Ipv4UtilTest.java @@ -4,8 +4,6 @@ import org.junit.Assert; import org.junit.Test; import org.junit.function.ThrowingRunnable; -import java.util.List; - public class Ipv4UtilTest { @Test @@ -28,10 +26,16 @@ public class Ipv4UtilTest { @Test public void longToIpTest() { - final String ip = "192.168.1.255"; - final long ipLong = Ipv4Util.ipv4ToLong(ip); - final String ipv4 = Ipv4Util.longToIpv4(ipLong); - Assert.assertEquals(ip, ipv4); + testLongToIp("192.168.1.255"); + testLongToIp("0.0.0.0"); + testLongToIp("0.0.0.255"); + testLongToIp("0.0.255.255"); + testLongToIp("0.255.255.255"); + testLongToIp("255.255.255.255"); + testLongToIp("255.255.255.0"); + testLongToIp("255.255.0.0"); + testLongToIp("255.0.0.0"); + testLongToIp("0.255.255.0"); } @Test @@ -44,9 +48,24 @@ public class Ipv4UtilTest { @Test public void listTest(){ - final int maskBit = Ipv4Util.getMaskBitByMask("255.255.255.0"); - final List