mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
NetUtil.bigIntegerToIPv6增加长度修正
This commit is contained in:
parent
4224e86461
commit
735f531e3c
@ -20,6 +20,7 @@
|
|||||||
* 【core 】 优化NumberUtil.count(pr#3772@Github)
|
* 【core 】 优化NumberUtil.count(pr#3772@Github)
|
||||||
* 【crypto 】 SM2.signHex改名为signHexFromHex,原名标记废弃,避免歧义(issue#IB0NVY@Gitee)
|
* 【crypto 】 SM2.signHex改名为signHexFromHex,原名标记废弃,避免歧义(issue#IB0NVY@Gitee)
|
||||||
* 【all 】 优化所调用的ObjectUtil#defaultIfNull避免重复创建(pr#1274@Gitee)
|
* 【all 】 优化所调用的ObjectUtil#defaultIfNull避免重复创建(pr#1274@Gitee)
|
||||||
|
* 【core 】 NetUtil.bigIntegerToIPv6增加长度修正(issue#IB27HV@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【json 】 修复JSONConfig.setDateFormat设置后toBean无效问题(issue#3713@Github)
|
* 【json 】 修复JSONConfig.setDateFormat设置后toBean无效问题(issue#3713@Github)
|
||||||
|
@ -108,9 +108,30 @@ public class NetUtil {
|
|||||||
* @since 5.5.7
|
* @since 5.5.7
|
||||||
*/
|
*/
|
||||||
public static String bigIntegerToIPv6(BigInteger bigInteger) {
|
public static String bigIntegerToIPv6(BigInteger bigInteger) {
|
||||||
|
// 确保 BigInteger 在 IPv6 地址范围内
|
||||||
|
if (bigInteger.compareTo(BigInteger.ZERO) < 0 || bigInteger.compareTo(new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16)) > 0) {
|
||||||
|
throw new IllegalArgumentException("BigInteger value is out of IPv6 range");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 BigInteger 转换为 16 字节的字节数组
|
||||||
|
byte[] bytes = bigInteger.toByteArray();
|
||||||
|
if (bytes.length > 16) {
|
||||||
|
// 如果字节数组长度大于 16,去掉前导零
|
||||||
|
int offset = bytes[0] == 0 ? 1 : 0;
|
||||||
|
final byte[] newBytes = new byte[16];
|
||||||
|
System.arraycopy(bytes, offset, newBytes, 0, 16);
|
||||||
|
bytes = newBytes;
|
||||||
|
} else if (bytes.length < 16) {
|
||||||
|
// 如果字节数组长度小于 16,前面补零
|
||||||
|
byte[] paddedBytes = new byte[16];
|
||||||
|
System.arraycopy(bytes, 0, paddedBytes, 16 - bytes.length, bytes.length);
|
||||||
|
bytes = paddedBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将字节数组转换为 IPv6 地址字符串
|
||||||
try {
|
try {
|
||||||
return InetAddress.getByAddress(bigInteger.toByteArray()).toString().substring(1);
|
return Inet6Address.getByAddress(bytes).getHostAddress();
|
||||||
} catch (UnknownHostException ignore) {
|
} catch (UnknownHostException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,18 @@ package cn.hutool.core.net;
|
|||||||
import cn.hutool.core.lang.Console;
|
import cn.hutool.core.lang.Console;
|
||||||
import cn.hutool.core.lang.PatternPool;
|
import cn.hutool.core.lang.PatternPool;
|
||||||
import cn.hutool.core.util.ReUtil;
|
import cn.hutool.core.util.ReUtil;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.net.HttpCookie;
|
import java.net.HttpCookie;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NetUtil单元测试
|
* NetUtil单元测试
|
||||||
*
|
*
|
||||||
@ -126,4 +129,11 @@ public class NetUtilTest {
|
|||||||
final String ip = NetUtil.getMultistageReverseProxyIp(ips);
|
final String ip = NetUtil.getMultistageReverseProxyIp(ips);
|
||||||
assertEquals("12.34.56.78", ip);
|
assertEquals("12.34.56.78", ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void bigIntegerToIPv6Test() {
|
||||||
|
BigInteger bigInteger = new BigInteger("21987654321098765432109876543210", 10);
|
||||||
|
String ipv6Address = NetUtil.bigIntegerToIPv6(bigInteger);
|
||||||
|
Assertions.assertEquals("0:115:85f1:5eb3:c74d:a870:11c6:7eea", ipv6Address);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user