mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!929 修复Ipv4Util.list()方法的bug
Merge pull request !929 from emptypoint/fix-bug-Ipv4Util
This commit is contained in:
commit
648a8e9443
@ -112,21 +112,38 @@ public class Ipv4Util {
|
|||||||
* @return 区间地址
|
* @return 区间地址
|
||||||
*/
|
*/
|
||||||
public static List<String> list(String ipFrom, String ipTo) {
|
public static List<String> list(String ipFrom, String ipTo) {
|
||||||
final int[] ipf = Convert.convert(int[].class, StrUtil.splitToArray(ipFrom, CharUtil.DOT));
|
// 确定ip数量
|
||||||
final int[] ipt = Convert.convert(int[].class, StrUtil.splitToArray(ipTo, CharUtil.DOT));
|
final int count = countByIpRange(ipFrom, ipTo);
|
||||||
|
final int[] from = Convert.convert(int[].class, StrUtil.splitToArray(ipFrom, CharUtil.DOT));
|
||||||
|
final int[] to = Convert.convert(int[].class, StrUtil.splitToArray(ipTo, CharUtil.DOT));
|
||||||
|
|
||||||
final List<String> ips = new ArrayList<>();
|
final List<String> ips = new ArrayList<>(count);
|
||||||
for (int a = ipf[0]; a <= ipt[0]; a++) {
|
// 是否是循环的第一个值
|
||||||
for (int b = (a == ipf[0] ? ipf[1] : 0); b <= (a == ipt[0] ? ipt[1]
|
boolean aIsStart = true, bIsStart = true, cIsStart = true;
|
||||||
: 255); b++) {
|
// 是否是循环的最后一个值
|
||||||
for (int c = (b == ipf[1] ? ipf[2] : 0); c <= (b == ipt[1] ? ipt[2]
|
boolean aIsEnd, bIsEnd, cIsEnd;
|
||||||
: 255); c++) {
|
// 循环的结束值
|
||||||
for (int d = (c == ipf[2] ? ipf[3] : 0); d <= (c == ipt[2] ? ipt[3]
|
int aEnd = to[0], bEnd, cEnd, dEnd;
|
||||||
: 255); d++) {
|
for (int a = from[0]; a <= aEnd; a++) {
|
||||||
|
aIsEnd = (a == aEnd);
|
||||||
|
// 本次循环的结束结束值
|
||||||
|
bEnd = aIsEnd ? to[1] : 255;
|
||||||
|
for (int b = (aIsStart ? from[1] : 0); b <= bEnd; b++) {
|
||||||
|
// 在上一个循环是最后值的基础上进行判断
|
||||||
|
bIsEnd = aIsEnd && (b == bEnd);
|
||||||
|
cEnd = bIsEnd ? to[2] : 255;
|
||||||
|
for (int c = (bIsStart ? from[2] : 0); c <= cEnd; c++) {
|
||||||
|
// 在之前循环是最后值的基础上进行判断
|
||||||
|
cIsEnd = bIsEnd && (c == cEnd);
|
||||||
|
dEnd = cIsEnd ? to[3] : 255;
|
||||||
|
for (int d = (cIsStart ? from[3] : 0); d <= dEnd; d++) {
|
||||||
ips.add(a + "." + b + "." + c + "." + d);
|
ips.add(a + "." + b + "." + c + "." + d);
|
||||||
}
|
}
|
||||||
|
cIsStart = false;
|
||||||
}
|
}
|
||||||
|
bIsStart = false;
|
||||||
}
|
}
|
||||||
|
aIsStart = false;
|
||||||
}
|
}
|
||||||
return ips;
|
return ips;
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,38 @@ public class Ipv4UtilTest {
|
|||||||
int maskBit = Ipv4Util.getMaskBitByMask("255.255.255.0");
|
int maskBit = Ipv4Util.getMaskBitByMask("255.255.255.0");
|
||||||
final List<String> list = Ipv4Util.list("192.168.100.2", maskBit, false);
|
final List<String> list = Ipv4Util.list("192.168.100.2", maskBit, false);
|
||||||
Assert.assertEquals(254, list.size());
|
Assert.assertEquals(254, list.size());
|
||||||
|
|
||||||
|
testGenerateIpList("10.1.0.1", "10.2.1.2");
|
||||||
|
|
||||||
|
testGenerateIpList("10.2.1.1", "10.2.1.2");
|
||||||
|
testGenerateIpList("10.2.0.1", "10.2.1.2");
|
||||||
|
testGenerateIpList("10.1.0.1", "10.2.1.2");
|
||||||
|
testGenerateIpList("10.1.2.1", "10.2.1.2");
|
||||||
|
|
||||||
|
testGenerateIpList("10.2.1.2", "10.2.1.2");
|
||||||
|
testGenerateIpList("10.2.0.2", "10.2.1.2");
|
||||||
|
testGenerateIpList("10.1.1.2", "10.2.1.2");
|
||||||
|
testGenerateIpList("10.1.2.2", "10.2.1.2");
|
||||||
|
|
||||||
|
testGenerateIpList("10.2.0.3", "10.2.1.2");
|
||||||
|
testGenerateIpList("10.1.0.3", "10.2.1.2");
|
||||||
|
testGenerateIpList("10.1.1.3", "10.2.1.2");
|
||||||
|
testGenerateIpList("10.1.2.3", "10.2.1.2");
|
||||||
|
|
||||||
|
testGenerateIpList("9.255.2.1", "10.2.1.2");
|
||||||
|
testGenerateIpList("9.255.2.2", "10.2.1.2");
|
||||||
|
testGenerateIpList("9.255.2.3", "10.2.1.2");
|
||||||
|
|
||||||
|
testGenerateIpList("9.255.1.2", "10.2.1.2");
|
||||||
|
testGenerateIpList("9.255.0.2", "10.2.1.2");
|
||||||
|
testGenerateIpList("9.255.3.2", "10.2.1.2");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testGenerateIpList(final String fromIp, final String toIp) {
|
||||||
|
Assert.assertEquals(
|
||||||
|
Ipv4Util.countByIpRange(fromIp, toIp),
|
||||||
|
Ipv4Util.list(fromIp, toIp).size()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user