当前位置:网站首页>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;
}
}
}
边栏推荐
- First time in China! The language AI strength of this Chinese enterprise is recognized as No.2 in the world! Second only to Google
- CMD command enters MySQL times service name or command error (fool teaching)
- LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
- 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
- Longest common prefix (leetcode question 14)
- Matplotlib drawing 3D graphics
- 我的创作纪念日
- Numpy——axis
- [Base64 notes] [suggestions collection]
- 2022.07.02
猜你喜欢
9 原子操作类之18罗汉增强
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
Numpy——2. Shape of array
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
648. 单词替换
【RT-Thread env 工具安装】
PV static creation and dynamic creation
Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
Command mode - unity
随机推荐
Version 2.0 of tapdata, the open source live data platform, has been released
实训九 网络服务的基本配置
Install mysql8 for Linux X ultra detailed graphic tutorial
从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
浏览积分设置的目的
UCloud是基础云计算服务提供商
LeetCode 535(C#)
Matplotlib drawing 3D graphics
爬虫实战(七):爬王者英雄图片
AI写首诗
杰理之手动配对方式【篇】
Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
杰理之关于 TWS 交叉配对的配置【篇】
ant desgin 多选
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
位运算介绍
【RT-Thread env 工具安装】
Solve the problem of remote rviz error reporting
Seize Jay Chou
Where does brain hole come from? New research from the University of California: creative people's neural connections will "take shortcuts"