FileDocCategorySizeDatePackage
IpFilterManagerImpl.javaAPI DocAzureus 3.0.3.45583Tue Jun 05 01:23:34 BST 2007org.gudy.azureus2.core3.ipfilter.impl

IpFilterManagerImpl

public class IpFilterManagerImpl extends Object implements org.gudy.azureus2.core3.config.ParameterListener, org.gudy.azureus2.core3.ipfilter.IpFilterManager
author
parg

Fields Summary
protected static IpFilterManagerImpl
singleton
private RandomAccessFile
rafDescriptions
Constructors Summary
public IpFilterManagerImpl()

	
	 	 
	  
		COConfigurationManager.addAndFireParameterListener(
				"Ip Filter Enable Description Cache", this);
	
Methods Summary
public java.lang.ObjectaddDescription(org.gudy.azureus2.core3.ipfilter.IpRange range, byte[] description)

		//if (true) return;
		if (rafDescriptions == null) {
			return null;
		}
		
		try {
			if (description == null || description.length == 0)
				return null;

			int start;
			int end;
			start = (int)rafDescriptions.getFilePointer();
			int len = (int)rafDescriptions.length();

			//System.out.println(len - 0x1FFFFFF);
			if (len + 61 >= 0x1FFFFFF) {
				// we could try to fit a desc < 61, but why bother.. at this point
				// we have at least 550,072 ranges
				return null;
			}
			
			if (start != len) {
				rafDescriptions.seek(len);
				start = (int)rafDescriptions.getFilePointer();
			}
			
			// last 25: position
			// 26 - 31 (6, 61 chars max): len
			
			if (description.length <= 61) {
				rafDescriptions.write(description);
			} else {
				rafDescriptions.write(description, 0,  61);
			}
			end = (int)rafDescriptions.getFilePointer();
			
			//System.out.println("add " + new String(description, 0, (end - start)) + "; " + start + " - " + end);
			
			int info = start + ((end - start) << 25);
			
			return new Integer(info);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	
public voidcacheAllDescriptions()

		IpRange[] ranges = getIPFilter().getRanges();
		for (int i = 0; i < ranges.length; i++) {
			Object info = ((IpRangeImpl)ranges[i]).getDescRef();
			if (info instanceof Integer) {
				byte[] desc = getDescription(info);
				Object[] data = { desc, info }; 
				((IpRangeImpl)ranges[i]).setDescRef(data);
			}
		}
	
public voidclearDescriptionCache()

		IpRange[] ranges = getIPFilter().getRanges();
		for (int i = 0; i < ranges.length; i++) {
			Object info = ((IpRangeImpl)ranges[i]).getDescRef();
			if (info instanceof Object[]) {
				Integer data = (Integer)((Object[])info)[1]; 
				((IpRangeImpl)ranges[i]).setDescRef(data);
			}
		}
	
public voiddeleteAllDescriptions()

		if (rafDescriptions != null) {
  		try {
  			rafDescriptions.close();
  		} catch (IOException e) {
  		}
  		rafDescriptions = null;
		}
		
		parameterChanged(null);
	
public org.gudy.azureus2.core3.ipfilter.BadIpsgetBadIps()

		return (BadIpsImpl.getInstance());
	
public byte[]getDescription(java.lang.Object info)

		// if cached, info is an object array, with the first index being the descr
		if (info instanceof Object[]) {
			return (byte[])(((Object[])info)[0]);
		}
		
		if (rafDescriptions == null || !(info instanceof Integer)) {
			return "".getBytes();
		}
		
		try {
			int posInfo = ((Integer)info).intValue();
			int pos = posInfo & 0x1FFFFFF;
			int len = posInfo >> 25;

			if (rafDescriptions.getFilePointer() != pos) {
				rafDescriptions.seek(pos);
			}

			byte[] bytes = new byte[len];
			rafDescriptions.read(bytes);
			
			return bytes;
		} catch (IOException e) {
			return "".getBytes();
		}
	
public org.gudy.azureus2.core3.ipfilter.IpFiltergetIPFilter()

		return( IpFilterImpl.getInstance());
	
public static org.gudy.azureus2.core3.ipfilter.IpFilterManagergetSingleton()

		return( singleton );
	
public voidparameterChanged(java.lang.String parameterName)

		boolean enable = COConfigurationManager.getBooleanParameter("Ip Filter Enable Description Cache");
		if (enable && rafDescriptions == null) {
			File fDescriptions = FileUtil.getUserFile("ipfilter.cache");
			try {
				if (fDescriptions.exists()) {
					fDescriptions.delete();
				}
				rafDescriptions = new RandomAccessFile(fDescriptions, "rw");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (!enable && rafDescriptions != null) {
			try {
				rafDescriptions.close();
			} catch (IOException e) {
			}
			rafDescriptions = null;
		}