}
public final class UtilsNetwork {
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
* This constructor should remain private
*/
private UtilsNetwork() {
throw new IllegalAccessError("Can not be instantiate");
}
/**
* Checks if IP as a good standard look.
*
* @param anIp
* an ip as a string
* @return true if IP is good, false if not
*/
public static boolean checkIp(String anIp) {
if ((anIp == null) || (anIp.trim().length() == 0)) {
return false;
}
// Needs 4 parts
String[] ipArray = anIp.split("\\.");
if ((ipArray == null) || (ipArray.length != 4)) {
return false;
}
// All part are [0, 255] integer
for (int lcI = 0; lcI < ipArray.length; lcI++) {
int val = -1;
try {
val = Integer.parseInt(ipArray[lcI]);
} catch (NumberFormatException lcExc) {
return false;
}
if ((val < 0) || (val > 255)) {
return false;
}
}
return true;
}
/**
* Transform a binary IP into its integer representation.
*
* @param aBinaryIp
* a binary IP
* @return the integer representation of the IP
*/
public static String binaryIpToInteger(String[] aBinaryIp) {
StringBuilder resu = new StringBuilder();
for (int lcI = 0; lcI < aBinaryIp.length; lcI++) {
resu.append(Integer.parseInt(aBinaryIp[lcI], 2)).append('.');
}
resu.delete(resu.length() - 1, resu.length());
return resu.toString();
}
/**
* Compute binary broadcast mask for an IP.
*
* @param anIp
* an IP
* @param aMask
* a mask
* @return the broadcast address for this IP/mask
*/
public static String[] getBroadcastAddressBinary(String anIp, String aMask) {
String[] ipMask = UtilsNetwork.ipToBinary(aMask);
String[] subNetMaskBin = UtilsNetwork.getSubnetAddressBinary(anIp, aMask);
String[] invertMask = new String[ipMask.length];
String[] broadCastIp = new String[ipMask.length];
for (int lcI = 0; lcI < ipMask.length; lcI++) {
String ip0 = ipMask[lcI];
StringBuilder resu = new StringBuilder();
for (int lcI2 = 0; lcI2 < ip0.length(); lcI2++) {
if (ip0.charAt(lcI2) == '1') {
resu.append('0');
} else {
resu.append('1');
}
}
invertMask[lcI] = resu.toString();
}
for (int lcI = 0; lcI < invertMask.length; lcI++) {
String ip0 = subNetMaskBin[lcI];
String ip1 = invertMask[lcI];
StringBuilder resu = new StringBuilder();
for (int lcI2 = 0; lcI2 < ip0.length(); lcI2++) {
// Do OR
if ((ip0.charAt(lcI2) == '1') || (ip1.charAt(lcI2) == '1')) {
resu.append('1');
} else {
resu.append('0');
}
}
broadCastIp[lcI] = resu.toString();
}
return broadCastIp;
}
/**
* Compute binary subnet mask for an IP.
*
* @param anIp
* an IP
* @param aMask
* a mask
* @return the subnet address for this IP/mask
*/
public static String[] getSubnetAddressBinary(String anIp, String aMask) {
String[] ipBin = UtilsNetwork.ipToBinary(anIp);
String[] masqBin = UtilsNetwork.ipToBinary(aMask);
String[] subMasq = new String[ipBin.length];
for (int lcI = 0; lcI < masqBin.length; lcI++) {
String ip0 = ipBin[lcI];
String ip1 = masqBin[lcI];
StringBuilder resu = new StringBuilder();
for (int lcI2 = 0; lcI2 < ip0.length(); lcI2++) {
// Do AND
if ((ip0.charAt(lcI2) == '1') && (ip1.charAt(lcI2) == '1')) {
resu.append('1');
} else {
resu.append('0');
}
}
subMasq[lcI] = resu.toString();
}
return subMasq;
}
/**
* Transform an IP into its binary representation.
*
* @param anIp
* an IP
* @return the binary representation of the IP
*/
public static String[] ipToBinary(String anIp) {
String[] ipArray = anIp.split("\\.");
String[] ipBin = new String[ipArray.length];
for (int lcJ = 0; lcJ < ipBin.length; lcJ++) {
ipBin[lcJ] = Integer.toBinaryString(Integer.parseInt(ipArray[lcJ]));
if (ipBin[lcJ].length() < 8) {
int nb0 = 8 - ipBin[lcJ].length();
StringBuilder buff = new StringBuilder();
while (nb0 != 0) {
buff.append('0');
nb0--;
}
ipBin[lcJ] = buff.toString() + ipBin[lcJ];
}
}
return ipBin;
}
}
255.255.255.0这个根据所在的局域网自己设定!!!