[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/singgel/java-base/master/java-network/Cidr.java [Back]  [Original]

/*
 * @Author:  hekuangsheng@163.com
 * @Date: 2023-02-03 03:04:20
 * @LastEditors:  hekuangsheng@163.com
 * @LastEditTime: 2023-02-03 03:09:30
 * @FilePath: /java-base/java-network/cidr.java
 * @Description: ,`customMade`, koroFileHeader : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class Cidr {
	
	public static void main(String[] args) {
		System.err.println(subnetDivide("117.72.16.0/23",9));
	}
	
	/***
	 * 2
	 * @param n
	 * @return
	 */
	public static boolean isPowerOfTwo(int n) {
        String str = Integer.toBinaryString(n);
        if(n < 1) {
			return false;
		} else if(str.lastIndexOf("1") == 0) {
			return true;
		} else {
			return false;
		}
  }
	
	
//	public static String toBinary(String str){ // 
//		char[] strChar=str.toCharArray(); 
//		String result=""; 
//		for(int i=0;i> 1;
		v |= v >> 2;
		v |= v >> 4;
		v |= v >> 8;
		v |= v >> 16;
		v++;
		return v;
	}
	
	/***
	 * 2
	 * @param n 
	 * @return
	 */
	private static int calculate2Num(int n) {
        if (n == 0) {
			return 1;
		}
        return 2 * calculate2Num(n - 1);
    }
	/***
	 * 
	 * @return
	 */
	public static List divideByPow(String ips,int pow){
//		Map map=new HashMap();
		List ipList = new ArrayList();
		try {
			if(ips ==null || "".equals(ips.trim()) || "null".equals(ips.trim())){
				return ipList;
			}
			ips = ips.trim();
			if(ips.indexOf("/")>0) {
				String temp[] = ips.split("/");
				if(temp.length == 2 && isIP(temp[0])){
					String ip=temp[0];
					String subnet=temp[1];
					String ipBitStr = makeBinaryString( getIpFromString(ip).intValue());
					int leg = ipBitStr.length();
					int bitNum  = Integer.parseInt(subnet);
					String str0 = "00000000000000000000000000000000";
					String str1 = "11111111111111111111111111111111";
					if(bitNum 1){
			int pow =judge(subNum);
			int difference=subNum-calculate2Num(pow);
			ipList =divideByPow(ips,pow);//
			if(difference!=0) {
				while (difference>0) {
					if(difference==1) {
						difference--;
						ipList.addAll(divideByPow(ipList.get(0),1));
						ipList.remove(ipList.get(0));
					}else {
						pow =judge(difference);
						difference=difference-calculate2Num(pow);
						for (int i = 0; i < calculate2Num(pow); i++) {
							ipList.addAll(divideByPow(ipList.get(0),pow));
							ipList.remove(ipList.get(0));
						}
						
					}
				}
			}
		}else {
			ipList.add(ips);
		}
		return ipList;
	}
	
	/** 
     * longIpIpxx.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;  
    }  
    /** 
     * xx.xx.xx.xxlong 
     * 
     * @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;  
    }  
    /** 
     *  
     * 
     * @param maskBit 
     *            "28""30" 
     * @return 
     */  
    public static String getMaskByMaskBit(String maskBit)  
    {  
        return "".equals(maskBit) ? "error, maskBit is null !" : getMaskMap(maskBit);  
    }  
      
    /** 
     *  ip/ IPIP  IP 218.240.38.69/30 
     * 
     * @param ip 
     *            IP218.240.38.69 
     * @param maskBit 
     *            30 
     * @return IP 
     */  
    public static String getBeginIpStr(String ip, String maskBit)  
    {  
        return getIpFromLong(getBeginIpLong(ip, maskBit));  
    }  
    /** 
     *  ip/ IPIP  IP 218.240.38.69/30 
     * 
     * @param ip 
     *            IP218.240.38.69 
     * @param maskBit 
     *            30 
     * @return IP 
     */  
    public static Long getBeginIpLong(String ip, String maskBit)  
    {  
        return getIpFromString(ip) & getIpFromString(getMaskByMaskBit(maskBit));  
    }  
    /** 
     *  ip/ IPIP  IP 218.240.38.69/30 
     * 
     * @param ip 
     *            IP218.240.38.69 
     * @param maskBit 
     *            30 
     * @return IP 
     */  
    public static String getEndIpStr(String ip, String maskBit)  
    {  
        return getIpFromLong(getEndIpLong(ip, maskBit));  
    }  
      
     /** 
     *  ip/ IPIP  IP 218.240.38.69/30 
     * 
     * @param ip 
     *            IP218.240.38.69 
     * @param maskBit 
     *            30 
     * @return IP 
     */  
    public static Long getEndIpLong(String ip, String maskBit)  
    {  
        return getBeginIpLong(ip, maskBit)  
                + ~getIpFromString(getMaskByMaskBit(maskBit));  
    }  
    
    public static String getMaskMap(String maskBit) {
    	switch (maskBit){
			case "1" : return "128.0.0.0";
			case "2" : return "192.0.0.0";
			case "3" : return "224.0.0.0";
			case "4" : return "240.0.0.0";
			case "5" : return "248.0.0.0";
			case "6" : return "252.0.0.0";
			case "7" : return "254.0.0.0";
			case "8" : return "255.0.0.0";
			case "9" : return "255.128.0.0";
			case "10" : return "255.192.0.0";
			case "11" : return "255.224.0.0";
			case "12" : return "255.240.0.0";
			case "13" : return "255.248.0.0";
			case "14" : return "255.252.0.0";
			case "15" : return "255.254.0.0";
			case "16" : return "255.255.0.0";
			case "17" : return "255.255.128.0";
			case "18" : return "255.255.192.0";
			case "19" : return "255.255.224.0";
			case "20" : return "255.255.240.0";
			case "21" : return "255.255.248.0";
			case "22" : return "255.255.252.0";
			case "23" : return "255.255.254.0";
			case "24" : return "255.255.255.0";
			case "25" : return "255.255.255.128";
			case "26" : return "255.255.255.192";
			case "27" : return "255.255.255.224";
			case "28" : return "255.255.255.240";
			case "29" : return "255.255.255.248";
			case "30" : return "255.255.255.252";
			case "31" : return "255.255.255.254";
			case "32" : return "255.255.255.255";
			default: return "-1";
		}
    }
    
    
    /**
	 * 
	 */
	public static String makeBinaryString(int i){
		String binary = "";  
		do{  
			int Bit = (i & 1);  
			String newDigit = ((Bit == 0) ? "0" : "1");  
			binary = newDigit + binary;  
			i >>>= 1;  
		}while (i != 0);  
		return binary; 
	}
    
	/**
	 * 
	 *  102ip
	 * @return
	 */
	public static  String getIpStringFrom10bit(String ss)
	{
		while(ss.length() 

Web Proxy Viewer  |  New URL  |  Original Page