Java InetAddress Check isInRage(InetAddress check, InetAddress bcast, int netmask)

Here you can find the source of isInRage(InetAddress check, InetAddress bcast, int netmask)

Description

is In Rage

License

LGPL

Declaration

public static boolean isInRage(InetAddress check, InetAddress bcast, int netmask) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.net.InetAddress;

public class Main {
    public static boolean isInRage(InetAddress check, InetAddress bcast, int netmask) {
        //System.out.println("*** check " + check + " in " + bcast + "/" + netmask);
        int byts = netmask / 8;
        // netmask%8 are the remaining bits. The mask is for the upper bits of the remaining byte
        byte mask = (byte) ~(0xff >>> (netmask % 8));
        byte[] b1 = check.getAddress();
        byte[] b2 = bcast.getAddress();

        int i;//from  www . jav a2s.c om
        for (i = 0; i < byts; i++) {
            if (b1[i] != b2[i]) {
                return false;
            }
        }

        // remaining bits
        if (mask != 0 && (b1[i] & mask) != (b2[i] & mask)) {
            return false;
        }
        return true;
    }
}

Related

  1. isClassicAddress(InetAddress address)
  2. isCommonSubnet(InetAddress address1, InetAddress address2)
  3. isGlobalAddressV6(InetAddress addr)
  4. isHostLocalHost(InetAddress host)
  5. isInet6Compatible(InetAddress address, Inet6Address inet6Address)
  6. isIpAddressInRange(InetAddress ipStart, InetAddress ipEnd, InetAddress ipToCheck)
  7. isIPLocal(final InetAddress adr)
  8. isIPv6(final InetAddress ip)
  9. isIPv6(InetAddress addr)