IPAddressCheckerSubnet.java

package com.ziesemer.utils.web.addressFilter.conditions;

import java.net.InetAddress;

/**
 * @author Mark A. Ziesemer
 * 	<a href="http://www.ziesemer.com.">&lt;www.ziesemer.com&gt;</a>
 */
public class IPAddressCheckerSubnet implements IAddressChecker{
	
	protected final byte[] network;
	protected final byte[] netmask;
	
	public IPAddressCheckerSubnet(final byte[] network, final byte[] netmask){
		this.network = network;
		this.netmask = netmask;
	}
	
	public boolean matches(final InetAddress ip, final String hostName) throws Exception{
		if(ip == null){
			return false;
		}
		return IPUtils.isInSubnet(network, netmask, ip.getAddress());
	}
	
	/**
	 * <p>
	 * 	Returns a copy of the network checked against, primarily for unit testing.
	 * </p>
	 */
	public byte[] getNetwork(){
		return network.clone();
	}
	
	/**
	 * <p>
	 * 	Returns a copy of the netmask checked against, primarily for unit testing.
	 * </p>
	 */
	public byte[] getNetmask(){
		return netmask.clone();
	}
	
}