⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadmanagerstateimpl.java

📁 java 文件下载器。可自定义
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

		public boolean isOurContent()
		{
			return false;
		}

		public String getPrimaryFile()
		{
			return null;
		}

		public void setPrimaryFile(String s)
		{
		}

		protected nullState(DownloadManager _dm)
		{
			download_manager = _dm;
		}
	}


	private static final int VER_INCOMING_PEER_SOURCE = 1;
	private static final int VER_CURRENT = 1;
	private static final LogIDs LOGID;
	private static final String RESUME_KEY = "resume";
	private static final String TRACKER_CACHE_KEY = "tracker_cache";
	private static final String ATTRIBUTE_KEY = "attributes";
	private static final String AZUREUS_PROPERTIES_KEY = "azureus_properties";
	private static final File ACTIVE_DIR;
	public static boolean SUPPRESS_FIXUP_ERRORS = false;
	private static Random random;
	private static final Map default_parameters;
	private static final Map default_attributes;
	private static AEMonitor class_mon = new AEMonitor("DownloadManagerState:class");
	private static Map state_map = new HashMap();
	private static Map global_state_cache = new HashMap();
	private static List global_state_cache_wrappers = new ArrayList();
	private DownloadManagerImpl download_manager;
	private final org.gudy.azureus2.core3.util.TorrentUtils.ExtendedTorrent torrent;
	private boolean write_required;
	private Category category;
	private CopyOnWriteList listeners_cow;
	private CopyOnWriteMap listeners_read_map_cow;
	private CopyOnWriteMap listeners_write_map_cow;
	private Map parameters;
	private Map attributes;
	private AEMonitor this_mon;
	private boolean firstPrimaryFileRead;
	private int supressWrites;
	private static ThreadLocal tls_wbr = new ThreadLocal() {

		public Object initialValue()
		{
			return new ArrayList(1);
		}

	};

	private static DownloadManagerState getDownloadState(DownloadManagerImpl download_manager, TOTorrent original_torrent, org.gudy.azureus2.core3.util.TorrentUtils.ExtendedTorrent target_torrent)
		throws TOTorrentException
	{
		byte hash[];
		DownloadManagerStateImpl res;
		hash = target_torrent.getHash();
		res = null;
		class_mon.enter();
		HashWrapper hash_wrapper = new HashWrapper(hash);
		res = (DownloadManagerStateImpl)state_map.get(hash_wrapper);
		if (res == null)
		{
			res = new DownloadManagerStateImpl(download_manager, target_torrent);
			state_map.put(hash_wrapper, res);
		} else
		{
			if (res.getDownloadManager() == null && download_manager != null)
				res.setDownloadManager(download_manager);
			if (original_torrent != null)
				res.mergeTorrentDetails(original_torrent);
		}
		class_mon.exit();
		break MISSING_BLOCK_LABEL_121;
		Exception exception;
		exception;
		class_mon.exit();
		throw exception;
		return res;
	}

	public static DownloadManagerState getDownloadState(TOTorrent original_torrent)
		throws TOTorrentException
	{
		byte torrent_hash[] = original_torrent.getHash();
		org.gudy.azureus2.core3.util.TorrentUtils.ExtendedTorrent saved_state = null;
		File saved_file = getStateFile(torrent_hash);
		if (saved_file.exists())
			try
			{
				saved_state = TorrentUtils.readDelegateFromFile(saved_file, false);
			}
			catch (Throwable e)
			{
				Debug.out((new StringBuilder()).append("Failed to load download state for ").append(saved_file).toString(), e);
			}
		if (saved_state == null)
		{
			TorrentUtils.copyToFile(original_torrent, saved_file);
			saved_state = TorrentUtils.readDelegateFromFile(saved_file, false);
		}
		return getDownloadState(null, original_torrent, saved_state);
	}

	protected static DownloadManagerState getDownloadState(DownloadManagerImpl download_manager, String torrent_file, byte torrent_hash[], boolean inactive)
		throws TOTorrentException
	{
		boolean discard_pieces = state_map.size() > 32;
		TOTorrent original_torrent = null;
		org.gudy.azureus2.core3.util.TorrentUtils.ExtendedTorrent saved_state = null;
		if (torrent_hash != null)
		{
			File saved_file = getStateFile(torrent_hash);
			if (saved_file.exists())
				try
				{
					Map cached_state = (Map)global_state_cache.remove(new HashWrapper(torrent_hash));
					if (cached_state != null)
					{
						CachedStateWrapper wrapper = new CachedStateWrapper(download_manager, torrent_file, torrent_hash, cached_state, inactive);
						global_state_cache_wrappers.add(wrapper);
						saved_state = wrapper;
					} else
					{
						saved_state = TorrentUtils.readDelegateFromFile(saved_file, discard_pieces);
					}
				}
				catch (Throwable e)
				{
					Debug.out((new StringBuilder()).append("Failed to load download state for ").append(saved_file).toString());
				}
		}
		if (saved_state == null)
		{
			original_torrent = TorrentUtils.readDelegateFromFile(new File(torrent_file), discard_pieces);
			torrent_hash = original_torrent.getHash();
			File saved_file = getStateFile(torrent_hash);
			if (saved_file.exists())
				try
				{
					saved_state = TorrentUtils.readDelegateFromFile(saved_file, discard_pieces);
				}
				catch (Throwable e)
				{
					Debug.out((new StringBuilder()).append("Failed to load download state for ").append(saved_file).toString());
				}
			if (saved_state == null)
			{
				TorrentUtils.copyToFile(original_torrent, saved_file);
				saved_state = TorrentUtils.readDelegateFromFile(saved_file, discard_pieces);
			}
		}
		DownloadManagerState res = getDownloadState(download_manager, original_torrent, saved_state);
		if (inactive)
			res.setActive(false);
		return res;
	}

	protected static File getStateFile(byte torrent_hash[])
	{
		return new File(ACTIVE_DIR, (new StringBuilder()).append(ByteFormatter.encodeString(torrent_hash)).append(".dat").toString());
	}

	protected static File getGlobalStateFile()
	{
		return new File(ACTIVE_DIR, "cache.dat");
	}

	public static void loadGlobalStateCache()
	{
		File file;
		file = getGlobalStateFile();
		if (!file.canRead())
			return;
		BufferedInputStream is = new BufferedInputStream(new GZIPInputStream(new FileInputStream(file)));
		Map map = BDecoder.decode(is);
		List cache = (List)map.get("state");
		if (cache != null)
		{
			for (int i = 0; i < cache.size(); i++)
			{
				Map entry = (Map)cache.get(i);
				byte hash[] = (byte[])(byte[])entry.get("hash");
				if (hash != null)
					global_state_cache.put(new HashWrapper(hash), entry);
			}

		}
		is.close();
		IOException e;
		try
		{
			is.close();
		}
		// Misplaced declaration of an exception variable
		catch (IOException e) { }
		break MISSING_BLOCK_LABEL_184;
		e;
		Debug.printStackTrace(e);
		try
		{
			is.close();
		}
		// Misplaced declaration of an exception variable
		catch (IOException e) { }
		break MISSING_BLOCK_LABEL_184;
		Exception exception;
		exception;
		try
		{
			is.close();
		}
		catch (Throwable e) { }
		throw exception;
		Throwable e;
		e;
		Debug.printStackTrace(e);
	}

	public static void saveGlobalStateCache()
	{
		class_mon.enter();
		Map map = new HashMap();
		List cache = new ArrayList();
		map.put("state", cache);
		Iterator it = state_map.values().iterator();
		do
		{
			if (!it.hasNext())
				break;
			DownloadManagerState dms = (DownloadManagerState)it.next();
			DownloadManager dm = dms.getDownloadManager();
			if (dm != null && dm.isPersistent())
				try
				{
					Map state = CachedStateWrapper.export(dms);
					cache.add(state);
				}
				catch (Throwable e)
				{
					Debug.printStackTrace(e);
				}
		} while (true);
		GZIPOutputStream os = new GZIPOutputStream(new FileOutputStream(getGlobalStateFile()));
		try
		{
			os.write(BEncoder.encode(map));
			os.close();
		}
		catch (IOException e)
		{
			Debug.printStackTrace(e);
			try
			{
				os.close();
			}
			catch (IOException f) { }
		}
		class_mon.exit();
		break MISSING_BLOCK_LABEL_199;
		Throwable e;
		e;
		Debug.printStackTrace(e);
		class_mon.exit();
		break MISSING_BLOCK_LABEL_199;
		Exception exception;
		exception;
		class_mon.exit();
		throw exception;
	}

	public static void discardGlobalStateCache()
	{
		getGlobalStateFile().delete();
		for (int i = 0; i < global_state_cache_wrappers.size(); i++)
			((CachedStateWrapper)global_state_cache_wrappers.get(i)).clearCache();

		global_state_cache_wrappers.clear();
	}

	protected DownloadManagerStateImpl(DownloadManagerImpl _download_manager, org.gudy.azureus2.core3.util.TorrentUtils.ExtendedTorrent _torrent)
	{
		listeners_cow = new CopyOnWriteList();
		listeners_read_map_cow = new CopyOnWriteMap();
		listeners_write_map_cow = new CopyOnWriteMap();
		this_mon = new AEMonitor("DownloadManagerState");
		firstPrimaryFileRead = true;
		supressWrites = 0;
		download_manager = _download_manager;
		torrent = _torrent;
		attributes = torrent.getAdditionalMapProperty("attributes");
		if (attributes == null)
			attributes = new HashMap();
		String cat_string = getStringAttribute("category");
		if (cat_string != null)
		{
			Category cat = CategoryManager.getCategory(cat_string);
			if (cat != null)
				setCategory(cat);
		}
		parameters = getMapAttribute("parameters");
		if (parameters == null)
			parameters = new HashMap();
		int version = getIntAttribute("version");
		if (version < 1)
			if (getPeerSources().length > 0)
			{
				if (PEPeerSource.isPeerSourceEnabledByDefault("Incoming"))
					setPeerSourceEnabled("Incoming", true);
			} else
			{
				setPeerSources(PEPeerSource.getDefaultEnabledPeerSources());
			}
		if (version < 1)
			setIntAttribute("version", 1);
		addListeners();
	}

	public void parameterChanged(String parameterName)
	{
		informWritten("parameters");
	}

	protected void addListeners()
	{
		COConfigurationManager.addParameterListener("Max.Peer.Connections.Per.Torrent.When.Seeding", this);
		COConfigurationManager.addParameterListener("Max.Peer.Connections.Per.Torrent.When.Seeding.Enable", this);
		COConfigurationManager.addParameterListener("Max.Peer.Connections.Per.Torrent", this);
		COConfigurationManager.addParameterListener("Max Uploads", this);
		COConfigurationManager.addParameterListener("Max Uploads Seeding", this);
		COConfigurationManager.addParameterListener("Max Seeds Per Torrent", this);
		COConfigurationManager.addParameterListener("enable.seedingonly.maxuploads", this);
	}

	protected void removeListeners()
	{
		COConfigurationManager.removeParameterListener("Max.Peer.Connections.Per.Torrent.When.Seeding", this);
		COConfigurationManager.removeParameterListener("Max.Peer.Connections.Per.Torrent.When.Seeding.Enable", this);
		COConfigurationManager.removeParameterListener("Max.Peer.Connections.Per.Torrent", this);
		COConfigurationManager.removeParameterListener("Max Uploads", this);
		COConfigurationManager.removeParameterListener("Max Uploads Seeding", this);
		COConfigurationManager.removeParameterListener("Max Seeds Per Torrent", this);
		COConfigurationManager.removeParameterListener("enable.seedingonly.maxuploads", this);
	}

	public DownloadManager getDownloadManager()
	{
		return download_manager;
	}

	protected void setDownloadManager(DownloadManagerImpl dm)
	{
		download_manager = dm;
	}

	public File getStateFile()
	{
		File parent = new File(ACTIVE_DIR, (new StringBuilder()).append(ByteFormatter.encodeString(torrent.getHash())).append(File.separatorChar).toString());
		return StringInterner.internFile(parent);
		Throwable e;
		e;
		Debug.printStackTrace(e);
		return null;
	}

	public void clearTrackerResponseCache()
	{
		setTrackerResponseCache(new HashMap());
	}

	public Map getTrackerResponseCache()
	{
		Map tracker_response_cache = null;
		tracker_response_cache = torrent.getAdditionalMapProperty("tracker_cache");
		if (tracker_response_cache == null)
			tracker_response_cache = new HashMap();
		return tracker_response_cache;
	}

	public void setTrackerResponseCache(Map value)
	{
		this_mon.enter();
		boolean changed = !BEncoder.mapsAreIdentical(value, getTrackerResponseCache());
		if (changed)
		{
			write_required = true;
			torrent.setAdditionalMapProperty("tracker_cache", value);
		}
		this_mon.exit();
		break MISSING_BLOCK_LABEL_65;
		Exception exception;
		exception;
		this_mon.exit();
		throw exception;
	}

	public Map getResumeData()
	{
		Map map;
		this_mon.enter();
		map = torrent.getAdditionalMapProperty("resume");
		this_mon.exit();
		return map;
		Exception exception;
		exception;
		this_mon.exit();
		throw exception;
	}

	public void clearResumeData()
	{
		setResumeData(null);
	}

	public void setResumeData(Map data)
	{
		this_mon.enter();
		if (data == null)
		{
			setLongAttribute("resumecomplete", 1L);
			torrent.removeAdditionalProperty("resume");
		} else
		{
			torrent.setAdditionalMapProperty("resume", data);
			boolean complete = DiskManagerFactory.isTorrentResumeDataComplete(this);
			setLongAttribute("resumecomplete", complete ? 2L : 1L);
		}
		write_required = true;
		this_mon.exit();
		break MISSING_BLOCK_LABEL_91;
		Exception exception;
		exception;
		this_mon.exit();
		throw exception;
		save();
		return;
	}

	public boolean isResumeDataComplete()
	{
		long state = getLongAttribute("resumecomplete");
		if (state == 0L)
		{
			boolean complete = DiskManagerFactory.isTorrentResumeDataComplete(this);
			setLongAttribute("resumecomplete", complete ? 2L : 1L);
			return complete;
		} else
		{
			return state == 2L;
		}
	}

	public TOTorrent getTorrent()
	{
		return torrent;
	}

	public void setActive(boolean active)
	{
		torrent.setDiscardFluff(!active);
	}

	public void discardFluff()
	{
		torrent.setDiscardFluff(true);
	}

	public void suppressStateSave(boolean suppress)
	{
		if (suppress)
			supressWrites++;
		else
		if (supressWrites > 0)
			supressWrites--;
	}

	public void save()
	{
		if (supressWrites > 0)
			return;
		boolean do_write;
		this_mon.enter();
		do_write = write_required;
		if (write_required)
			write_required = false;
		this_mon.exit();
		break MISSING_BLOCK_LABEL_52;
		Exception exception;
		exception;
		this_mon.exit();
		throw exception;
		if (do_write)
			try
			{
				if (Logger.isEnabled())
					Logger.log(new LogEvent(torrent, LOGID, (new StringBuilder()).append("Saving state for download '").append(TorrentUtils.getLocalisedName(torrent)).append("'").toString()));
				torrent.setAdditionalMapProperty("attributes", attributes);
				TorrentUtils.writeToFile(torrent, true);
			}
			catch (Throwable e)
			{
				Logger.log(new LogEvent(torrent, LOGID, "Saving state", e));
			}
		return;
	}

	public void delete()
	{
		class_mon.enter();
		HashWrapper wrapper = torrent.getHashWrapper();
		state_map.remove(wrapper);
		TorrentUtils.delete(torrent);
		File dir = new File(ACTIVE_DIR, ByteFormatter.encodeString(wrapper.getBytes()));
		if (dir.exists() && dir.isDirectory())
			FileUtil.recursiveDelete(dir);
		removeListeners();
		class_mon.exit();
		break MISSING_BLOCK_LABEL_106;
		Throwable e;
		e;
		Debug.printStackTrace(e);
		class_mon.exit();
		break MISSING_BLOCK_LABEL_106;
		Exception exception;
		exception;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -