IP地址,子网掩码,地址池计算

package com.metarnet.util;

public class IPPoolUtil {
	public static void main(String[] args)
	{
		System.out.println(getNetMask("255.255.255.0"));
		
		System.out.println(getPoolMax(getNetMask("255.255.255.128")));
		
		System.out.println(getEndIP("10.229.0.1",24).getStartIP());
		
		System.out.println(getEndIP("10.229.0.1",24).getEndIP());
	}
	/**
	 * 根据起始IP地址和子网掩码计算终止IP
	 */
	public static Nets getEndIP(String StartIP,int netmask)
	{
		return getEndIP(StartIP,getMask(netmask));
	}
	/**
	 * 根据起始IP地址和子网掩码计算终止IP
	 */
	public static Nets getEndIP(String StartIP,String netmask)
	{
		Nets nets = new Nets();
		String[] start=Negation(StartIP,netmask).split("\\.");
		nets.setStartIP(start[0]+"."+start[1]+"."+start[2]+"."+(Integer.valueOf(start[3])+1));
		nets.setEndIP(TaskOR(Negation(StartIP,netmask),netmask));
		nets.setNetMask(netmask);
		return nets;
	}
	/**
	 * 根据掩码位计算掩码
	 */
	public static String getMask(int masks)
	{
		if(masks == 1)
            return "128.0.0.0";
        else if(masks == 2)
            return "192.0.0.0";
        else if(masks == 3)
            return "224.0.0.0";
        else if(masks == 4)
            return "240.0.0.0";
        else if(masks == 5)
            return "248.0.0.0";
        else if(masks == 6) 
            return "252.0.0.0";
        else if(masks == 7)
            return "254.0.0.0";
        else if(masks == 8)
            return "255.0.0.0";
        else if(masks ==9)
            return "255.128.0.0";
        else if(masks == 10)
            return "255.192.0.0";
        else if(masks == 11)
            return "255.224.0.0";
        else if(masks == 12)
            return "255.240.0.0";
        else if(masks == 13)
            return "255.248.0.0";
        else if(masks == 14)
            return "255.252.0.0";
        else if(masks == 15)
            return "255.254.0.0";
        else if(masks == 16)
            return "255.255.0.0";
        else if(masks == 17)
            return "255.255.128.0";
        else if(masks == 18)
            return "255.255.192.0";
        else if(masks == 19)
            return "255.255.224.0";
        else if(masks == 20)
            return "255.255.240.0";
        else if(masks == 21)
            return "255.255.248.0";
        else if(masks == 22)
            return "255.255.252.0";
        else if(masks == 23)
            return "255.255.254.0";
        else if(masks == 24)
            return "255.255.255.0";
        else if(masks == 25)
            return "255.255.255.128";
        else if(masks == 26)
            return "255.255.255.192";
        else if(masks == 27)
            return "255.255.255.224";
        else if(masks == 28)
            return "255.255.255.240";
        else if(masks == 29)
            return "255.255.255.248";
        else if(masks == 30)
            return "255.255.255.252";
        else if(masks == 31)
            return "255.255.255.254";
        else if(masks == 32)
            return "255.255.255.255";
        return "";
	}
	/**
	 * temp1根据temp2取反
	 */
	private static String Negation(String StartIP,String netmask)
	{
		String[] temp1=StartIP.trim().split("\\.");
		String[] temp2=netmask.trim().split("\\.");
		int[] rets=new int[4];
		for (int i =0;i<4;i++) {
			rets[i]=Integer.parseInt(temp1[i])&Integer.parseInt(temp2[i]);
		}
		return rets[0]+"."+rets[1]+"."+rets[2]+"."+rets[3];
	}
	/**
	 * temp1根据temp2取或
	 */
	private static String TaskOR(String StartIP,String netmask)
	{
		String[] temp1=StartIP.trim().split("\\.");
		String[] temp2=netmask.trim().split("\\.");
		int[] rets=new int[4];
		for (int i =0;i<4;i++) {
			rets[i]=255-(Integer.parseInt(temp1[i])^Integer.parseInt(temp2[i]));
		}
		return rets[0]+"."+rets[1]+"."+rets[2]+"."+(rets[3]-1);
	}
	/**
	 * 计算子网大小
	 */
	public static int getPoolMax(int netmask)
	{
		if(netmask<=0||netmask>=32)
		{
			return 0;
		}
		int bits=32-netmask;
		return (int) Math.pow(2,bits) -2;
	}
	/**
	 * 转换为验码位数
	 */
	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;
	}
	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;
	}
}
class Nets {
	private String StartIP;
	private String EndIP;
	private String NetMask;
	public String getStartIP() {
		return StartIP;
	}
	public void setStartIP(String startIP) {
		StartIP = startIP;
	}
	public String getEndIP() {
		return EndIP;
	}
	public void setEndIP(String endIP) {
		EndIP = endIP;
	}
	public String getNetMask() {
		return NetMask;
	}
	public void setNetMask(String netMask) {
		NetMask = netMask;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值