This commit is contained in:
Zjp 2023-02-15 21:12:21 +08:00
parent e2266f593f
commit 3b3871ba1e
2 changed files with 10 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package cn.hutool.core.net;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.BiMap;
import java.util.HashMap;
/**
@ -83,6 +84,6 @@ public class MaskBit {
*/
public static long getMaskIpLong(final int maskBit) {
Assert.isTrue(MASK_BIT_MAP.containsKey(maskBit), "非法的掩码位数:{}", maskBit);
return -1L << (32 - maskBit);
return (0xffffffffL << (32 - maskBit)) & 0xffffffffL;
}
}

View File

@ -121,7 +121,7 @@ public class Ipv4UtilTest {
}
@Test
public void ipv4ToLongTest(){
public void ipv4ToLongTest() {
long l = Ipv4Util.ipv4ToLong("127.0.0.1");
Assert.assertEquals(2130706433L, l);
l = Ipv4Util.ipv4ToLong("114.114.114.114");
@ -132,6 +132,13 @@ public class Ipv4UtilTest {
Assert.assertEquals(4294967295L, l);
}
@Test
public void getMaskIpLongTest() {
for (int i = 1; i <= 32; i++) {
Assert.assertEquals(Ipv4Util.ipv4ToLong(MaskBit.get(i)), MaskBit.getMaskIpLong(i));
}
}
@SuppressWarnings("SameParameterValue")
private void testGenerateIpList(final String fromIp, final String toIp) {
Assert.assertEquals(