当前位置:网站首页>IP tools
IP tools
2022-07-07 19:39:00 【Scarecrow 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;
}
}
}
边栏推荐
猜你喜欢

项目经理『面试八问』,看了等于会了

Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog

Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica

PV静态创建和动态创建

2022.07.04

2022.07.04

从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks

Numpy——2. Shape of array

Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry

超分辨率技术在实时音视频领域的研究与实践
随机推荐
tp6 实现佣金排行榜
网信办公布《数据出境安全评估办法》,9 月 1 日起施行
Unable to link the remote redis server (solution 100%
怎么在手机上买股票开户 股票开户安全吗
Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)
9 原子操作类之18罗汉增强
银行理财产品怎么买?需要办银行卡吗?
2022.07.05
【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器
让这个 CRMEB 单商户微信商城系统火起来,太好用了!
关于ssh登录时卡顿30s左右的问题调试处理
Number - number (Lua)
L1-025 positive integer a+b (Lua)
Notes...
指定opencv非标准安装的版本
Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
The top of slashdata developer tool is up to you!!!
鸿蒙智能家居【1.0】
L1-019 who falls first (Lua)
从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks