check if one subnet is in range of the other, useful , for dividing subnet into chunks - Java Network

Java examples for Network:Network Address

Description

check if one subnet is in range of the other, useful , for dividing subnet into chunks

Demo Code

/** //ww w. ja  v  a 2s. c  o m
 * Copyright 2012 InCNTRE, This file is released under Apache 2.0 license except for component libraries under different licenses
http://www.apache.org/licenses/LICENSE-2.0
 */
//package com.java2s;

public class Main {


    /** 
     * check if one subnet is in range of the other, useful , for dividing subnet into chunks
     * @see generateIpRules
     * @param ip1
     * @param subnet1
     * @param ip2
     * @param subnet2
     * @return boolean of whether IP in range or not 
     */
    public static boolean checkIfInRange(int ip1, int subnet1, int ip2,
            int subnet2) {

        if (subnet2 < subnet1)
            return false;

        String ip1String = Integer.toHexString(ip1);
        String ip2String = Integer.toHexString(ip2);

        if (ip1String.substring(0, subnet1).equals(
                ip2String.substring(0, subnet2)))
            return true;

        return false;
    }
}

Related Tutorials