Java Processing IP tool classes, including the long type of IP to the general IP type, the xx.xx.xx.xx type to a long type, based on the mask bit to get the mask, based on the ip/mask to compute the IP segment of the start IP, based on the ip/mask to compute IP segment termination of IP, and other methods, can be used directly!
Copy Code code as follows:
Package com.hh.test;
Import Java.util.HashMap;
Import Java.util.Map;
Import Org.apache.commons.lang3.StringUtils;
/**
* IP Tool class
*
* @author BL
* @email kutekute00@gmail.com
*
*/
public class Iputils
{
/**
* Convert a long IP to a generic IP type: xx.xx.xx.xx
*
* @param IP
* @return
*/
public static String getipfromlong (Long IP)
{
String S1 = string.valueof ((IP & 4278190080L)/16777216L);
String s2 = string.valueof ((IP & 16711680L)/65536L);
String s3 = string.valueof ((IP & 65280L)/256L);
String S4 = string.valueof (IP & 255L);
return s1 + "." + s2 + "." + S3 + "." + S4;
}
/**
* Convert the xx.xx.xx.xx type to a long type
*
* @param IP
* @return
*/
public static Long getipfromstring (String IP)
{
Long Iplong = 0L;
String iptemp = IP;
Iplong = Iplong * 256
+ Long.parselong (iptemp.substring (0, Iptemp.indexof (".")));
Iptemp = iptemp.substring (Iptemp.indexof (".") + 1, iptemp.length ());
Iplong = Iplong * 256
+ Long.parselong (iptemp.substring (0, Iptemp.indexof (".")));
Iptemp = iptemp.substring (Iptemp.indexof (".") + 1, iptemp.length ());
Iplong = Iplong * 256
+ Long.parselong (iptemp.substring (0, Iptemp.indexof (".")));
Iptemp = iptemp.substring (Iptemp.indexof (".") + 1, iptemp.length ());
Iplong = Iplong * 256 + long.parselong (iptemp);
return iplong;
}
/**
* Get mask based on mask bit
*
* @param maskbit
* Mask digits, such as "28", "30"
* @return
*/
public static string Getmaskbymaskbit (String maskbit)
{
Return Stringutils.isempty (maskbit)? "Error, Maskbit is null!"
: Maskbitmap (). get (Maskbit);
}
/**
* Based on the ip/mask bit to compute the IP segment starting IP such as IP string 218.240.38.69/30
*
* @param IP
* Given IP, such as 218.240.38.69
* @param maskbit
* A given mask bit, such as 30
* @return string representation of the originating IP
*/
public static string Getbeginipstr (string IP, String maskbit)
{
Return Getipfromlong (Getbeginiplong (IP, maskbit));
}
/**
* Based on the ip/mask bit to compute the IP segment starting IP such as IP string 218.240.38.69/30
*
* @param IP
* Given IP, such as 218.240.38.69
* @param maskbit
* A given mask bit, such as 30
* @return Long Integer representation of the starting IP
*/
public static Long getbeginiplong (string IP, String maskbit)
{
return getipfromstring (IP) & getipfromstring (Getmaskbymaskbit (maskbit));
}
/**
* The IP segment's termination IP, such as IP string 218.240.38.69/30, is computed according to the ip/mask bit
*
* @param IP
* Given IP, such as 218.240.38.69
* @param maskbit
* A given mask bit, such as 30
* @return string representation of terminated IP
*/
public static string Getendipstr (string IP, String maskbit)
{
Return Getipfromlong (Getendiplong (IP, maskbit));
}
/**
* The IP segment's termination IP, such as IP string 218.240.38.69/30, is computed according to the ip/mask bit
*
* @param IP
* Given IP, such as 218.240.38.69
* @param maskbit
* A given mask bit, such as 30
* @return Long integer representation of end IP
*/
public static Long getendiplong (string IP, String maskbit)
{
Return Getbeginiplong (IP, maskbit)
+ ~getipfromstring (Getmaskbymaskbit (maskbit));
}
/**
* Convert to mask bit based on subnet mask such as 255.255.255.252 to mask bit 30
*
* @param netmarks
* @return
*/
public static int Getnetmask (String netmarks)
{
StringBuffer SBF;
String str;
int inetmask = 0, count = 0;
string[] IPList = Netmarks.split ("\;");
for (int n = 0; n < iplist.length; n++)
{
SBF = ToBin (Integer.parseint (iplist[n));
str = Sbf.reverse (). toString ();
Count = 0;
for (int i = 0; i < str.length (); i++)
{
i = Str.indexof (' 1 ', i);
if (i = = 1)
{
Break
}
count++;
}
Inetmask + = count;
}
return inetmask;
}
/**
* Calculate Subnet size
*
* @param netmask
* Mask Code bit
* @return
*/
public static int Getpoolmax (int maskbit)
{
if (maskbit <= 0 | | | maskbit >= 32)
{
return 0;
}
return (int) Math.pow (2, 32-maskbit)-2;
}
private static StringBuffer toBin (int x)
{
StringBuffer result = new StringBuffer ();
Result.append (x% 2);
x/= 2;
while (x > 0)
{
Result.append (x% 2);
x/= 2;
}
return result;
}
/*
* Stores all mask bits and corresponding mask keys: Mask bit value: Mask (x.x.x.x)
*/
private static map<string, string> Maskbitmap ()
{
map<string, string> maskbit = new hashmap<string, string> ();
Maskbit.put ("1", "128.0.0.0");
Maskbit.put ("2", "192.0.0.0");
Maskbit.put ("3", "224.0.0.0");
Maskbit.put ("4", "240.0.0.0");
Maskbit.put ("5", "248.0.0.0");
Maskbit.put ("6", "252.0.0.0");
Maskbit.put ("7", "254.0.0.0");
Maskbit.put ("8", "255.0.0.0");
Maskbit.put ("9", "255.128.0.0");
Maskbit.put ("10", "255.192.0.0");
Maskbit.put ("11", "255.224.0.0");
Maskbit.put ("12", "255.240.0.0");
Maskbit.put ("13", "255.248.0.0");
Maskbit.put ("14", "255.252.0.0");
Maskbit.put ("15", "255.254.0.0");
Maskbit.put ("16", "255.255.0.0");
Maskbit.put ("17", "255.255.128.0");
Maskbit.put ("18", "255.255.192.0");
Maskbit.put ("19", "255.255.224.0");
Maskbit.put ("20", "255.255.240.0");
Maskbit.put ("21", "255.255.248.0");
Maskbit.put ("22", "255.255.252.0");
Maskbit.put ("23", "255.255.254.0");
Maskbit.put ("24", "255.255.255.0");
Maskbit.put ("25", "255.255.255.128");
Maskbit.put ("26", "255.255.255.192");
Maskbit.put ("27", "255.255.255.224");
Maskbit.put ("28", "255.255.255.240");
Maskbit.put ("29", "255.255.255.248");
Maskbit.put ("30", "255.255.255.252");
Maskbit.put ("31", "255.255.255.254");
Maskbit.put ("32", "255.255.255.255");
return maskbit;
}
/**
* Get mask based on mask bit
*
* @param masks
* @return
*/
@Deprecated
public static String getmaskbymaskbit (int masks)
{
String ret = "";
if (masks = 1)
ret = "128.0.0.0";
else if (masks = 2)
ret = "192.0.0.0";
else if (masks = 3)
ret = "224.0.0.0";
else if (masks = 4)
ret = "240.0.0.0";
else if (masks = 5)
ret = "248.0.0.0";
else if (masks = 6)
ret = "252.0.0.0";
else if (masks = 7)
ret = "254.0.0.0";
else if (masks = 8)
ret = "255.0.0.0";
else if (masks = 9)
ret = "255.128.0.0";
else if (masks = 10)
ret = "255.192.0.0";
else if (masks = 11)
ret = "255.224.0.0";
else if (masks = 12)
ret = "255.240.0.0";
else if (masks = 13)
ret = "255.248.0.0";
else if (masks = 14)
ret = "255.252.0.0";
else if (masks = 15)
ret = "255.254.0.0";
else if (masks = 16)
ret = "255.255.0.0";
else if (masks = 17)
ret = "255.255.128.0";
else if (masks = 18)
ret = "255.255.192.0";
else if (masks = 19)
ret = "255.255.224.0";
else if (masks = 20)
ret = "255.255.240.0";
else if (masks = 21)
ret = "255.255.248.0";
else if (masks = 22)
ret = "255.255.252.0";
else if (masks = 23)
ret = "255.255.254.0";
else if (masks = 24)
ret = "255.255.255.0";
else if (masks = 25)
ret = "255.255.255.128";
else if (masks = 26)
ret = "255.255.255.192";
else if (masks = 27)
ret = "255.255.255.224";
else if (masks = 28)
ret = "255.255.255.240";
else if (masks = 29)
ret = "255.255.255.248";
else if (masks = 30)
ret = "255.255.255.252";
else if (masks = 31)
ret = "255.255.255.254";
else if (masks = 32)
ret = "255.255.255.255";
return ret;
}
}