FileDocCategorySizeDatePackage
IpRangeImpl.javaAPI DocAzureus 3.0.3.47436Tue Jun 05 01:53:30 BST 2007org.gudy.azureus2.core3.ipfilter.impl

IpRangeImpl

public class IpRangeImpl extends Object implements org.gudy.azureus2.core3.ipfilter.IpRange
author
Olivier

Fields Summary
private static final byte
FLAG_SESSION_ONLY
private static final byte
FLAG_ADDED_TO_RANGE_LIST
private static final byte
FLAG_INVALID_START
private static final byte
FLAG_INVALID_END
private static final byte
FLAG_INVALID
private int
ipStart
private int
ipEnd
private byte
flags
private Object
descRef
private static final byte
FLAG_MERGED
private int
merged_end
private org.gudy.azureus2.core3.ipfilter.IpRange[]
my_merged_entries
Constructors Summary
public IpRangeImpl(String _description, String _startIp, String _endIp, boolean _sessionOnly)


	      
			  
		if (_sessionOnly) {
			flags = FLAG_SESSION_ONLY;
		}

		if (_startIp == null || _endIp == null) {

			throw (new RuntimeException(
					"Invalid start/end values - null not supported"));
		}
		
		try {
			ipStart = PRHelpers.addressToInt(_startIp);
		} catch (UnknownHostException e) {
			flags |= FLAG_INVALID_START;
		}
		try {
			ipEnd = PRHelpers.addressToInt(_endIp);
		} catch (UnknownHostException e) {
			flags |= FLAG_INVALID_END;
		}

		if (_description != "") {
			setDescription(_description);
		}

		checkValid();
	
public IpRangeImpl(String _description, int _startIp, int _endIp, boolean _sessionOnly)

		if (_sessionOnly) {
			flags = FLAG_SESSION_ONLY;
		}

		ipStart = _startIp;
		ipEnd = _endIp;

		if (_description != "") {
			setDescription(_description);
		}

		checkValid();
	
Methods Summary
public voidaddMergedEntry(org.gudy.azureus2.core3.ipfilter.IpRange e2)

		if (my_merged_entries == null) {

			my_merged_entries = new IpRange[] { e2 };

		} else {

			IpRange[] x = new IpRange[my_merged_entries.length + 1];

			System.arraycopy(my_merged_entries, 0, x, 0, my_merged_entries.length);

			x[x.length - 1] = e2;

			my_merged_entries = x;
		}
	
public voidcheckValid()

		((IpFilterImpl) IpFilterImpl.getInstance()).setValidOrNot(this, isValid());
	
public intcompareDescription(org.gudy.azureus2.core3.ipfilter.IpRange other)

		return getDescription().compareTo(other.getDescription());
	
public intcompareEndIpTo(org.gudy.azureus2.core3.ipfilter.IpRange other)

		long l = getEndIpLong() - ((IpRangeImpl) other).getEndIpLong();

		if (l < 0) {
			return (-1);
		} else if (l > 0) {
			return (1);
		}
		return (0);
	
public intcompareStartIpTo(org.gudy.azureus2.core3.ipfilter.IpRange other)

		long l = getStartIpLong() - ((IpRangeImpl) other).getStartIpLong();

		if (l < 0) {
			return (-1);
		} else if (l > 0) {
			return (1);
		} else {
			return (0);
		}
	
public booleangetAddedToRangeList()

		return (flags & FLAG_ADDED_TO_RANGE_LIST) != 0;
	
protected java.lang.ObjectgetDescRef()

		return descRef;
	
public java.lang.StringgetDescription()

		return new String(IpFilterManagerFactory.getSingleton().getDescription(
				descRef));
	
public java.lang.StringgetEndIp()

		return (flags & FLAG_INVALID_END) > 0 ? "" : PRHelpers.intToAddress(ipEnd);
	
public longgetEndIpLong()

		if ((flags & FLAG_INVALID_END) > 0) {
			return -1;
		}

		long val = ipEnd;

		if (val < 0) {
			val += 0x100000000L;
		}

		return (val);
	
public booleangetMerged()

		return (flags & FLAG_MERGED) != 0;
	
public longgetMergedEndLong()

		return (merged_end < 0 ? (merged_end + 0x100000000L) : merged_end);
	
public org.gudy.azureus2.core3.ipfilter.IpRange[]getMergedEntries()

		return (my_merged_entries);
	
public java.lang.StringgetStartIp()

		return (flags & FLAG_INVALID_START) > 0 ? ""
				: PRHelpers.intToAddress(ipStart);
	
public longgetStartIpLong()

		if ((flags & FLAG_INVALID_START) > 0) {
			return -1;
		}

		long val = ipStart;

		if (val < 0) {

			val += 0x100000000L;
		}

		return (val);
	
public booleanisInRange(java.lang.String ipAddress)

		if (!isValid()) {
			return false;
		}

		try {
			long int_address = PRHelpers.addressToInt(ipAddress);

			if (int_address < 0) {

				int_address += 0x100000000L;
			}

  		long start_address = ipStart;
  		long end_address = ipEnd;			

			if (start_address < 0) {

				start_address += 0x100000000L;
			}
			if (end_address < 0) {

				end_address += 0x100000000L;
			}

			return (int_address >= start_address && int_address <= end_address);

		} catch (UnknownHostException e) {

			return (false);
		}
	
public booleanisSessionOnly()

		return (flags & FLAG_SESSION_ONLY) != 0;
	
public booleanisValid()

		if ((flags & FLAG_INVALID) > 0) {
			return false;
		}

		long start_address = ipStart;
		long end_address = ipEnd;			

		if (start_address < 0) {

			start_address += 0x100000000L;
		}
		if (end_address < 0) {

			end_address += 0x100000000L;
		}

		return (end_address >= start_address);
	
public voidresetMergeInfo()

		flags &= ~FLAG_MERGED;

		if ((flags & FLAG_INVALID_END) == 0) {
			merged_end = ipEnd;
		}
	
protected voidsetAddedToRangeList(boolean b)

		if (b) {
			flags |= FLAG_ADDED_TO_RANGE_LIST;
		} else {
			flags &= ~FLAG_ADDED_TO_RANGE_LIST;
		}
	
protected voidsetDescRef(java.lang.Object descRef)

		this.descRef = descRef;
	
public voidsetDescription(java.lang.String str)

		descRef = IpFilterManagerFactory.getSingleton().addDescription(this,
				str.getBytes());
	
public voidsetEndIp(java.lang.String str)

		if (str == null) {
			throw (new RuntimeException("Invalid end value - null not supported"));
		}

		if (str.equals(getEndIp())) {
			return;
		}

		flags &= ~FLAG_INVALID_END;
		try {
			ipEnd = PRHelpers.addressToInt(str);
		} catch (UnknownHostException e) {
			flags |= FLAG_INVALID_END;
		}

		if ((flags & FLAG_INVALID) == 0) {
			checkValid();
		}
	
public voidsetMerged()

		flags |= FLAG_MERGED;
	
public voidsetMergedEnd(long endIpLong)

		merged_end = (int) (endIpLong >= 0x100000000L ? endIpLong - 0x100000000L
				: endIpLong);
	
public voidsetSessionOnly(boolean _sessionOnly)

		if (_sessionOnly) {
			flags |= FLAG_SESSION_ONLY;
		} else {
			flags &= ~FLAG_SESSION_ONLY;
		}
	
public voidsetStartIp(java.lang.String str)

		if (str == null) {
			throw (new RuntimeException("Invalid start value - null not supported"));
		}

		if (str.equals(getStartIp())) {
			return;
		}

		flags &= ~FLAG_INVALID_START;
		try {
			ipStart = PRHelpers.addressToInt(str);
		} catch (UnknownHostException e) {
			flags |= FLAG_INVALID_START;
		}

		if ((flags & FLAG_INVALID) == 0) {
			checkValid();
		}
	
public java.lang.StringtoString()

		return getDescription() + " : " + getStartIp() + " - " + getEndIp();