当前位置:网站首页>IP 工具类
IP 工具类
2022-07-07 17:29:00 【稻草人0.0】
public class IPUtils {
public IPUtils() {
}
public static String convertIPToString(long ipLong) {
if (ipLong < 0L) {
ipLong += 4294967296L;
}
StringBuilder builder = new StringBuilder(15);
builder.append(ipLong >> 24);
builder.append('.');
builder.append((ipLong & 16777215L) >> 16);
builder.append('.');
builder.append((ipLong & 65535L) >> 8);
builder.append('.');
builder.append(ipLong & 255L);
return builder.toString();
}
public static Long convertIPToLong(String ipAddr, Long defaultValue) {
try {
return convertIPToLong(ipAddr);
} catch (IllegalArgumentException var3) {
return defaultValue;
}
}
public static long convertIPToLong(String ipAddr) throws IllegalArgumentException {
if (ipAddr != null && isIPAddr(ipAddr)) {
String[] ips = ipAddr.split("\\.");
long ipLong = 0L;
ipLong += Long.parseLong(ips[0]) << 24;
ipLong += Long.parseLong(ips[1]) << 16;
ipLong += Long.parseLong(ips[2]) << 8;
ipLong += Long.parseLong(ips[3]);
return ipLong;
} else {
throw new IllegalArgumentException("`" + ipAddr + "` is not a valid IP address!");
}
}
public static Integer convertIPToInt(String ipAddr, Integer defaultValue) {
try {
return convertIPToInt(ipAddr);
} catch (IllegalArgumentException var3) {
return defaultValue;
}
}
public static int convertIPToInt(String ipAddr) {
long ipVal = convertIPToLong(ipAddr);
if (ipVal > 2147483647L) {
ipVal -= 4294967296L;
}
return Long.valueOf(ipVal).intValue();
}
public static boolean isIPAddr(String addr) {
if (addr != null && addr.trim().length() != 0) {
String[] ips = addr.split("\\.");
if (ips.length != 4) {
return false;
} else {
try {
int ipa = Integer.parseInt(ips[0]);
int ipb = Integer.parseInt(ips[1]);
int ipc = Integer.parseInt(ips[2]);
int ipd = Integer.parseInt(ips[3]);
return ipa >= 0 && ipa <= 255 && ipb >= 0 && ipb <= 255 && ipc >= 0 && ipc <= 255 && ipd >= 0 && ipd <= 255;
} catch (NumberFormatException var6) {
return false;
}
}
} else {
return false;
}
}
}
边栏推荐
- R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
- Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
- what‘s the meaning of inference
- [information security laws and regulations] review
- 博睿数据入选《2022爱分析 · IT运维厂商全景报告》
- what‘s the meaning of inference
- Initial experience of cache and ehcache "suggestions collection"
- LeetCode 535(C#)
- 解决rosdep的报错问题
- Former richest man, addicted to farming
猜你喜欢
2022如何评估与选择低代码开发平台?
[tpm2.0 principle and Application guide] Chapter 9, 10 and 11
Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
2022.07.04
Policy mode - unity
Numpy——axis
State mode - Unity (finite state machine)
Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
多个kubernetes集群如何实现共享同一个存储
杰理之关于 TWS 配对方式配置【篇】
随机推荐
How to share the same storage among multiple kubernetes clusters
MySQL、sqlserver oracle数据库连接方式
In 2021, the national average salary was released. Have you reached the standard?
L1-025 positive integer a+b (Lua)
Hongmeng smart home [1.0]
Classification and application of enterprise MES Manufacturing Execution System
2022.07.02
648. 单词替换
ASP.NET体育馆综合会员管理系统源码,免费分享
In the first half of 2022, I found 10 books that have been passed around by my circle of friends
吞吐量Throughout
5billion, another master fund was born in Fujian
现在股票开户可以直接在网上开吗?安全吗。
Responsibility chain model - unity
歌单11111
LeetCode 515(C#)
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
Flipping game (enumeration)
Kirin Xin'an with heterogeneous integration cloud financial information and innovation solutions appeared at the 15th Hunan Financial Technology Exchange Conference