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

📄 torrentutils.java

📁 java 文件下载器。可自定义
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	private static final Set created_torrents_set;
	private static ThreadLocal tls = new ThreadLocal() {

		public Object initialValue()
		{
			return new HashMap();
		}

	};
	private static volatile Set ignore_set;
	private static boolean bSaveTorrentBackup;
	private static CopyOnWriteList torrent_attribute_listeners = new CopyOnWriteList();
	private static final int PIECE_HASH_TIMEOUT = 0x2bf20;
	private static Map torrent_delegates = new WeakHashMap();
	private static HashSet torrentFluffKeyset = new HashSet(2);
	private static Map fluffThombstone = new HashMap(1);

	public TorrentUtils()
	{
	}

	public static TOTorrent readFromFile(File file, boolean create_delegate)
		throws TOTorrentException
	{
		return readFromFile(file, create_delegate, false);
	}

	public static ExtendedTorrent readDelegateFromFile(File file, boolean force_initial_discard)
		throws TOTorrentException
	{
		return (ExtendedTorrent)readFromFile(file, true, force_initial_discard);
	}

	public static TOTorrent readFromFile(File file, boolean create_delegate, boolean force_initial_discard)
		throws TOTorrentException
	{
		TOTorrent torrent;
		try
		{
			torrent = TOTorrentFactory.deserialiseFromBEncodedFile(file);
			if (bSaveTorrentBackup)
			{
				File torrent_file_bak = new File(file.getParent(), (new StringBuilder()).append(file.getName()).append(".bak").toString());
				if (!torrent_file_bak.exists())
					try
					{
						torrent.serialiseToBEncodedFile(torrent_file_bak);
					}
					catch (Throwable e)
					{
						Debug.printStackTrace(e);
					}
			}
		}
		catch (TOTorrentException e)
		{
			Debug.outNoStack(e.getMessage());
			File torrentBackup = new File(file.getParent(), (new StringBuilder()).append(file.getName()).append(".bak").toString());
			if (torrentBackup.exists())
				torrent = TOTorrentFactory.deserialiseFromBEncodedFile(torrentBackup);
			else
				throw e;
		}
		torrent.setAdditionalStringProperty("torrent filename", file.toString());
		if (create_delegate)
		{
			torrentDelegate res = new torrentDelegate(torrent, file);
			if (force_initial_discard)
				res.discardPieces(SystemTime.getCurrentTime(), true);
			return res;
		} else
		{
			return torrent;
		}
	}

	public static TOTorrent readFromBEncodedInputStream(InputStream is)
		throws TOTorrentException
	{
		TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedInputStream(is);
		torrent.removeAdditionalProperties();
		return torrent;
	}

	public static void setMemoryOnly(TOTorrent torrent, boolean mem_only)
	{
		if (mem_only)
		{
			torrent.setAdditionalStringProperty("torrent filename", "?/\\!:mem_only:!\\/?");
		} else
		{
			String s = torrent.getAdditionalStringProperty("torrent filename");
			if (s != null && s.equals("?/\\!:mem_only:!\\/?"))
				torrent.removeAdditionalProperty("torrent filename");
		}
	}

	public static void writeToFile(TOTorrent torrent)
		throws TOTorrentException
	{
		writeToFile(torrent, false);
	}

	public static void writeToFile(TOTorrent torrent, boolean force_backup)
		throws TOTorrentException
	{
		String str;
		torrent.getMonitor().enter();
		str = torrent.getAdditionalStringProperty("torrent filename");
		if (str == null)
			throw new TOTorrentException("TorrentUtils::writeToFile: no 'torrent filename' attribute defined", 1);
		if (str.equals("?/\\!:mem_only:!\\/?"))
		{
			torrent.getMonitor().exit();
			return;
		}
		File torrent_file_tmp = new File((new StringBuilder()).append(str).append("._az").toString());
		torrent.serialiseToBEncodedFile(torrent_file_tmp);
		File torrent_file = new File(str);
		if ((force_backup || COConfigurationManager.getBooleanParameter("Save Torrent Backup")) && torrent_file.exists())
		{
			File torrent_file_bak = new File((new StringBuilder()).append(str).append(".bak").toString());
			try
			{
				torrent_file_bak.delete();
				torrent_file.renameTo(torrent_file_bak);
			}
			catch (SecurityException e)
			{
				Debug.printStackTrace(e);
			}
		}
		if (torrent_file.exists())
			torrent_file.delete();
		torrent_file_tmp.renameTo(torrent_file);
		torrent.getMonitor().exit();
		break MISSING_BLOCK_LABEL_215;
		Exception exception;
		exception;
		torrent.getMonitor().exit();
		throw exception;
	}

	public static void writeToFile(TOTorrent torrent, File file)
		throws TOTorrentException
	{
		writeToFile(torrent, file, false);
	}

	public static void writeToFile(TOTorrent torrent, File file, boolean force_backup)
		throws TOTorrentException
	{
		torrent.setAdditionalStringProperty("torrent filename", file.toString());
		writeToFile(torrent, force_backup);
	}

	public static String getTorrentFileName(TOTorrent torrent)
		throws TOTorrentException
	{
		String str = torrent.getAdditionalStringProperty("torrent filename");
		if (str == null)
			throw new TOTorrentException("TorrentUtils::getTorrentFileName: no 'torrent filename' attribute defined", 1);
		if (str.equals("?/\\!:mem_only:!\\/?"))
			return null;
		else
			return str;
	}

	public static void copyToFile(TOTorrent torrent, File file)
		throws TOTorrentException
	{
		torrent.serialiseToBEncodedFile(file);
	}

	public static void delete(TOTorrent torrent)
		throws TOTorrentException
	{
		String str;
		torrent.getMonitor().enter();
		str = torrent.getAdditionalStringProperty("torrent filename");
		if (str == null)
			throw new TOTorrentException("TorrentUtils::delete: no 'torrent filename' attribute defined", 1);
		if (str.equals("?/\\!:mem_only:!\\/?"))
		{
			torrent.getMonitor().exit();
			return;
		}
		if (!(new File(str)).delete())
			throw new TOTorrentException((new StringBuilder()).append("TorrentUtils::delete: failed to delete '").append(str).append("'").toString(), 5);
		(new File((new StringBuilder()).append(str).append(".bak").toString())).delete();
		torrent.getMonitor().exit();
		break MISSING_BLOCK_LABEL_153;
		Exception exception;
		exception;
		torrent.getMonitor().exit();
		throw exception;
	}

	public static void delete(File torrent_file)
	{
		if (!FileUtil.deleteWithRecycle(torrent_file))
			Debug.out((new StringBuilder()).append("TorrentUtils::delete: failed to delete '").append(torrent_file).append("'").toString());
		(new File((new StringBuilder()).append(torrent_file.toString()).append(".bak").toString())).delete();
	}

	public static boolean move(File from_torrent, File to_torrent)
	{
		if (!FileUtil.renameFile(from_torrent, to_torrent))
			return false;
		if ((new File((new StringBuilder()).append(from_torrent.toString()).append(".bak").toString())).exists())
			FileUtil.renameFile(new File((new StringBuilder()).append(from_torrent.toString()).append(".bak").toString()), new File((new StringBuilder()).append(to_torrent.toString()).append(".bak").toString()));
		return true;
	}

	public static String exceptionToText(TOTorrentException e)
	{
		int reason = e.getReason();
		String errorDetail;
		if (reason == 1)
			errorDetail = MessageText.getString("DownloadManager.error.filenotfound");
		else
		if (reason == 2)
			errorDetail = MessageText.getString("DownloadManager.error.fileempty");
		else
		if (reason == 3)
			errorDetail = MessageText.getString("DownloadManager.error.filetoobig");
		else
		if (reason == 6)
			errorDetail = MessageText.getString("DownloadManager.error.filewithouttorrentinfo");
		else
		if (reason == 7)
			errorDetail = MessageText.getString("DownloadManager.error.unsupportedencoding");
		else
		if (reason == 4)
			errorDetail = MessageText.getString("DownloadManager.error.ioerror");
		else
		if (reason == 8)
			errorDetail = MessageText.getString("DownloadManager.error.sha1");
		else
		if (reason == 9)
			errorDetail = MessageText.getString("DownloadManager.error.operationcancancelled");
		else
			errorDetail = Debug.getNestedExceptionMessage(e);
		String msg = Debug.getNestedExceptionMessage(e);
		if (errorDetail.indexOf(msg) == -1)
			errorDetail = (new StringBuilder()).append(errorDetail).append(" (").append(msg).append(")").toString();
		return errorDetail;
	}

	public static List announceGroupsToList(TOTorrent torrent)
	{
		List groups = new ArrayList();
		TOTorrentAnnounceURLGroup group = torrent.getAnnounceURLGroup();
		TOTorrentAnnounceURLSet sets[] = group.getAnnounceURLSets();
		if (sets.length == 0)
		{
			List s = new ArrayList();
			s.add(torrent.getAnnounceURL().toString());
			groups.add(s);
		} else
		{
			for (int i = 0; i < sets.length; i++)
			{
				List s = new ArrayList();
				TOTorrentAnnounceURLSet set = sets[i];
				URL urls[] = set.getAnnounceURLs();
				for (int j = 0; j < urls.length; j++)
					s.add(urls[j].toString());

				if (s.size() > 0)
					groups.add(s);
			}

		}
		return groups;
	}

	public static void listToAnnounceGroups(List groups, TOTorrent torrent)
	{
		TOTorrentAnnounceURLGroup tg;
		tg = torrent.getAnnounceURLGroup();
		if (groups.size() == 1)
		{
			List set = (List)groups.get(0);
			if (set.size() == 1)
			{
				torrent.setAnnounceURL(new URL((String)set.get(0)));
				tg.setAnnounceURLSets(new TOTorrentAnnounceURLSet[0]);
				return;
			}
		}
		try
		{
			Vector g = new Vector();
			for (int i = 0; i < groups.size(); i++)
			{
				List set = (List)groups.get(i);
				URL urls[] = new URL[set.size()];
				for (int j = 0; j < set.size(); j++)
					urls[j] = new URL((String)set.get(j));

				if (urls.length > 0)
					g.add(tg.createAnnounceURLSet(urls));
			}

			TOTorrentAnnounceURLSet sets[] = new TOTorrentAnnounceURLSet[g.size()];
			g.copyInto(sets);
			tg.setAnnounceURLSets(sets);
			if (sets.length == 0)
				torrent.setAnnounceURL(new URL("http://no.valid.urls.defined/announce"));
		}
		catch (MalformedURLException e)
		{
			Debug.printStackTrace(e);
		}
		return;
	}

	public static void announceGroupsInsertFirst(TOTorrent torrent, String first_url)
	{
		try
		{
			announceGroupsInsertFirst(torrent, new URL(first_url));
		}
		catch (MalformedURLException e)
		{
			Debug.printStackTrace(e);
		}
	}

	public static void announceGroupsInsertFirst(TOTorrent torrent, URL first_url)
	{
		announceGroupsInsertFirst(torrent, new URL[] {
			first_url
		});
	}

	public static void announceGroupsInsertFirst(TOTorrent torrent, URL first_urls[])
	{
		TOTorrentAnnounceURLGroup group = torrent.getAnnounceURLGroup();
		TOTorrentAnnounceURLSet sets[] = group.getAnnounceURLSets();
		TOTorrentAnnounceURLSet set1 = group.createAnnounceURLSet(first_urls);
		if (sets.length > 0)
		{
			TOTorrentAnnounceURLSet new_sets[] = new TOTorrentAnnounceURLSet[sets.length + 1];
			new_sets[0] = set1;
			System.arraycopy(sets, 0, new_sets, 1, sets.length);
			group.setAnnounceURLSets(new_sets);
		} else
		{
			TOTorrentAnnounceURLSet set2 = group.createAnnounceURLSet(new URL[] {
				torrent.getAnnounceURL()
			});
			group.setAnnounceURLSets(new TOTorrentAnnounceURLSet[] {
				set1, set2
			});
		}
	}

	public static void announceGroupsInsertLast(TOTorrent torrent, URL first_urls[])
	{
		TOTorrentAnnounceURLGroup group = torrent.getAnnounceURLGroup();
		TOTorrentAnnounceURLSet sets[] = group.getAnnounceURLSets();
		TOTorrentAnnounceURLSet set1 = group.createAnnounceURLSet(first_urls);
		if (sets.length > 0)
		{
			TOTorrentAnnounceURLSet new_sets[] = new TOTorrentAnnounceURLSet[sets.length + 1];
			new_sets[sets.length] = set1;
			System.arraycopy(sets, 0, new_sets, 0, sets.length);
			group.setAnnounceURLSets(new_sets);
		} else
		{
			TOTorrentAnnounceURLSet set2 = group.createAnnounceURLSet(new URL[] {
				torrent.getAnnounceURL()
			});
			group.setAnnounceURLSets(new TOTorrentAnnounceURLSet[] {
				set2, set1
			});
		}
	}

	public static void announceGroupsSetFirst(TOTorrent torrent, String first_url)
	{
		List groups = announceGroupsToList(torrent);
		boolean found = false;
label0:
		for (int i = 0; i < groups.size(); i++)
		{
			List set = (List)groups.get(i);
			int j = 0;
			do
			{
				if (j >= set.size())
					continue label0;
				if (first_url.equals(set.get(j)))
				{
					set.remove(j);
					set.add(0, first_url);
					groups.remove(set);
					groups.add(0, set);
					found = true;
					break label0;
				}
				j++;
			} while (true);
		}

		if (!found)
			System.out.println((new StringBuilder()).append("TorrentUtils::announceGroupsSetFirst - failed to find '").append(first_url).append("'").toString());
		listToAnnounceGroups(groups, torrent);
	}

	public static boolean announceGroupsContainsURL(TOTorrent torrent, String url)
	{
		List groups = announceGroupsToList(torrent);
		for (int i = 0; i < groups.size(); i++)
		{
			List set = (List)groups.get(i);
			for (int j = 0; j < set.size(); j++)
				if (url.equals(set.get(j)))
					return true;

		}

		return false;
	}

	public static boolean mergeAnnounceURLs(TOTorrent new_torrent, TOTorrent dest_torrent)
	{
		if (new_torrent == null || dest_torrent == null)
			return false;
		List new_groups = announceGroupsToList(new_torrent);
		List dest_groups = announceGroupsToList(dest_torrent);
		List groups_to_add = new ArrayList();
		for (int i = 0; i < new_groups.size(); i++)
		{
			List new_set = (List)new_groups.get(i);
			boolean match = false;
			int j = 0;
			do
			{
				if (j >= dest_groups.size())
					break;
				List dest_set = (List)dest_groups.get(j);
				boolean same = new_set.size() == dest_set.size();
				if (same)
				{
					int k = 0;
					do
					{
						if (k >= new_set.size())
							break;
						String new_url = (String)new_set.get(k);
						if (!dest_set.contains(new_url))
						{
							same = false;
							break;
						}
						k++;
					} while (true);
				}
				if (same)
				{
					match = true;
					break;
				}
				j++;
			} while (true);
			if (!match)
				groups_to_add.add(new_set);
		}

		if (groups_to_add.size() == 0)
			return false;
		for (int i = 0; i < groups_to_add.size(); i++)
			dest_groups.add(i, groups_to_add.get(i));

		listToAnnounceGroups(dest_groups, dest_torrent);
		return true;
	}

	public static boolean replaceAnnounceURL(TOTorrent torrent, URL old_url, URL new_url)
	{
		boolean found = false;
		String old_str = old_url.toString();
		String new_str = new_url.toString();
		List l = announceGroupsToList(torrent);
		for (int i = 0; i < l.size(); i++)
		{
			List set = (List)l.get(i);
			for (int j = 0; j < set.size(); j++)
				if (((String)set.get(j)).equals(old_str))
				{
					found = true;
					set.set(j, new_str);
				}

		}

		if (found)
			listToAnnounceGroups(l, torrent);
		if (torrent.getAnnounceURL().toString().equals(old_str))
		{
			torrent.setAnnounceURL(new_url);
			found = true;
		}
		if (found)
			try
			{
				writeToFile(torrent);
			}
			catch (Throwable e)
			{
				Debug.printStackTrace(e);
				return false;
			}
		return found;
	}

	public static void setResumeDataCompletelyValid(DownloadManagerState download_manager_state)
	{
		DiskManagerFactory.setResumeDataCompletelyValid(download_manager_state);
	}

	public static String getLocalisedName(TOTorrent torrent)
	{
		LocaleUtilDecoder decoder = LocaleTorrentUtil.getTorrentEncodingIfAvailable(torrent);
		if (decoder == null)
			return new String(torrent.getName(), "UTF8");
		return decoder.decodeString(torrent.getName());
		Throwable e;
		e;
		Debug.printStackTrace(e);
		return new String(torrent.getName());
	}

	public static void setTLSTorrentHash(HashWrapper hash)
	{
		((Map)tls.get()).put("hash", hash);
	}

	public static TOTorrent getTLSTorrent()
	{
		HashWrapper hash;
		hash = (HashWrapper)((Map)tls.get()).get("hash");
		if (hash == null)
			break MISSING_BLOCK_LABEL_60;
		DownloadManager dm;
		AzureusCore core = AzureusCoreFactory.getSingleton();
		dm = core.getGlobalManager().getDownloadManager(hash);
		if (dm != null)

⌨️ 快捷键说明

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