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

📄 torrentdownloaderimpl.java

📁 java 文件下载器。可自定义
💻 JAVA
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   TorrentDownloaderImpl.java

package org.gudy.azureus2.core3.torrentdownloader.impl;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.ssl.*;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.security.SESecurityManager;
import org.gudy.azureus2.core3.torrentdownloader.TorrentDownloader;
import org.gudy.azureus2.core3.torrentdownloader.TorrentDownloaderCallBackInterface;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.core3.util.protocol.magnet.MagnetConnection;

public class TorrentDownloaderImpl extends AEThread
	implements TorrentDownloader
{

	private String original_url;
	private String url_str;
	private String referrer;
	private Map request_properties;
	private String file_str;
	private URL url;
	private HttpURLConnection con;
	private String error;
	private String status;
	private TorrentDownloaderCallBackInterface iface;
	private int state;
	private int percentDone;
	private int readTotal;
	private boolean cancel;
	private String filename;
	private String directoryname;
	private File file;
	private byte buf[];
	private int bufBytes;
	private boolean deleteFileOnCancel;
	private boolean ignoreReponseCode;
	private AEMonitor this_mon;
	private int errCode;

	public TorrentDownloaderImpl()
	{
		super("Torrent Downloader");
		error = "Ok";
		status = "";
		state = -1;
		percentDone = 0;
		readTotal = 0;
		cancel = false;
		file = null;
		buf = new byte[1020];
		bufBytes = 0;
		deleteFileOnCancel = true;
		ignoreReponseCode = false;
		this_mon = new AEMonitor("TorrentDownloader");
		setDaemon(true);
	}

	public void init(TorrentDownloaderCallBackInterface _iface, String _url, String _referrer, Map _request_properties, String _file)
	{
		iface = _iface;
		original_url = _url;
		_url = _url.replace('\\', '/');
		_url = _url.replaceAll(" ", "%20");
		setName((new StringBuilder()).append("TorrentDownloader: ").append(_url).toString());
		url_str = _url;
		referrer = _referrer;
		request_properties = _request_properties;
		file_str = _file;
	}

	public void notifyListener()
	{
		if (iface != null)
			iface.TorrentDownloaderEvent(state, this);
		else
		if (state == 4)
			System.err.println(error);
	}

	private void cleanUpFile()
	{
		if (file != null && file.exists())
			file.delete();
	}

	private void error(int errCode, String err)
	{
		this_mon.enter();
		state = 4;
		setError(errCode, err);
		cleanUpFile();
		notifyListener();
		this_mon.exit();
		break MISSING_BLOCK_LABEL_46;
		Exception exception;
		exception;
		this_mon.exit();
		throw exception;
	}

	public void runSupport()
	{
		try
		{
			new URL(url_str);
		}
		catch (Throwable t)
		{
			if (url_str.length() == 40)
				try
				{
					byte infohash[] = ByteFormatter.decodeString(url_str.toUpperCase());
					url_str = (new StringBuilder()).append("magnet:?xt=urn:btih:").append(Base32.encode(infohash)).toString();
				}
				catch (Throwable e) { }
		}
		url = AddressUtils.adjustURL(new URL(url_str));
		String protocol = url.getProtocol().toLowerCase();
		if (protocol.equals("magnet") || protocol.equals("dht"))
			url = AddressUtils.adjustURL(new URL((new StringBuilder()).append(url_str).append("&pause_on_error=true").toString()));
		int i = 0;
		do
		{
			if (i >= 2)
				break;
			try
			{
				if (protocol.equals("https"))
				{
					HttpsURLConnection ssl_con = (HttpsURLConnection)url.openConnection();
					ssl_con.setHostnameVerifier(new HostnameVerifier() {

						final TorrentDownloaderImpl this$0;

						public boolean verify(String host, SSLSession session)
						{
							return true;
						}

			
			{
				this$0 = TorrentDownloaderImpl.this;
				super();
			}
					});
					con = ssl_con;
				} else
				{
					con = (HttpURLConnection)url.openConnection();
				}
				con.setRequestProperty("User-Agent", "Azureus 4.2.0.0");
				if (referrer != null && referrer.length() > 0)
					con.setRequestProperty("Referer", referrer);
				if (request_properties != null)
				{
					Iterator it = request_properties.entrySet().iterator();
					do
					{
						if (!it.hasNext())
							break;
						java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
						String key = (String)entry.getKey();
						String value = (String)entry.getValue();
						if (!key.equalsIgnoreCase("Accept-Encoding"))
							con.setRequestProperty(key, value);
					} while (true);
				}
				con.connect();
				break;
			}
			catch (SSLException e)
			{
				if (i != 0 || SESecurityManager.installServerCertificates(url) == null)
					throw e;
				i++;
			}
		} while (true);
		int response = con.getResponseCode();
		if (!ignoreReponseCode && response != 202 && response != 200)
		{
			error(response, (new StringBuilder()).append(Integer.toString(response)).append(": ").append(con.getResponseMessage()).toString());
			return;
		}
		String tmp;
		filename = con.getHeaderField("Content-Disposition");
		if (filename != null && filename.toLowerCase().matches(".*attachment.*"))
			for (; filename.toLowerCase().charAt(0) != 'a'; filename = filename.substring(1));
		if (filename != null && filename.toLowerCase().startsWith("attachment") && filename.indexOf('=') != -1)
			break MISSING_BLOCK_LABEL_798;
		tmp = url.getFile();
		if (tmp.length() == 0 || tmp.equals("/"))
		{
			filename = url.getHost();
			break MISSING_BLOCK_LABEL_898;
		}
		if (!tmp.startsWith("?"))
			break MISSING_BLOCK_LABEL_740;
		String query = tmp.toUpperCase();
		int pos = query.indexOf("XT=URN:SHA1:");
		if (pos == -1)
			pos = query.indexOf("XT=URN:BTIH:");
		if (pos != -1)
		{
			pos += 12;
			int p2 = query.indexOf("&", pos);
			if (p2 == -1)
				filename = query.substring(pos);
			else
				filename = query.substring(pos, p2);
		} else
		{
			filename = (new StringBuilder()).append("Torrent").append((long)(Math.random() * 9.2233720368547758E+018D)).toString();
		}
		new StringBuilder();
		this;
		JVM INSTR dup_x1 ;
		filename;
		append();
		".tmp";
		append();
		toString();
		filename;
		break MISSING_BLOCK_LABEL_898;
		if (tmp.lastIndexOf('/') != -1)
			tmp = tmp.substring(tmp.lastIndexOf('/') + 1);
		int param_pos = tmp.indexOf('?');
		if (param_pos != -1)
			tmp = tmp.substring(0, param_pos);
		filename = URLDecoder.decode(tmp, "UTF8");
		break MISSING_BLOCK_LABEL_898;
		filename = filename.substring(filename.indexOf('=') + 1);
		if (filename.startsWith("\"") && filename.endsWith("\""))
			filename = filename.substring(1, filename.lastIndexOf('"'));
		filename = URLDecoder.decode(filename, "UTF8");
		File temp = new File(filename);
		filename = temp.getName();
		filename = FileUtil.convertOSSpecificChars(filename, false);
		directoryname = COConfigurationManager.getDirectoryParameter("General_sDefaultTorrent_Directory");
		boolean useTorrentSave = COConfigurationManager.getBooleanParameter("Save Torrent Files");
		if (file_str != null)
		{
			File temp = new File(file_str);
			if (!useTorrentSave || directoryname.length() == 0)
				if (temp.isDirectory())
					directoryname = temp.getCanonicalPath();
				else
					directoryname = temp.getCanonicalFile().getParent();
			if (!temp.isDirectory())
				filename = temp.getName();
		}
		state = 0;
		notifyListener();
		break MISSING_BLOCK_LABEL_1194;
		MalformedURLException e;
		e;
		error(0, (new StringBuilder()).append("Exception while parsing URL '").append(url).append("':").append(e.getMessage()).toString());
		break MISSING_BLOCK_LABEL_1194;
		e;
		error(0, (new StringBuilder()).append("Exception while initializing download of '").append(url).append("': Unknown Host '").append(e.getMessage()).append("'").toString());
		break MISSING_BLOCK_LABEL_1194;
		IOException ioe;
		ioe;
		error(0, (new StringBuilder()).append("I/O Exception while initializing download of '").append(url).append("':").append(ioe.toString()).toString());
		break MISSING_BLOCK_LABEL_1194;
		ioe;
		error(0, (new StringBuilder()).append("Exception while initializing download of '").append(url).append("':").append(ioe.toString()).toString());
		if (state == 4)
			return;
		boolean status_reader_run[];
		status_reader_run = (new boolean[] {
			true
		});
		state = 1;
		notifyListener();
		state = 2;
		notifyListener();
		Thread status_reader = new AEThread(status_reader_run) {

			final boolean val$status_reader_run[];
			final TorrentDownloaderImpl this$0;

			public void runSupport()
			{
				boolean changed_status = false;
_L1:
				Thread.sleep(250L);
				this_mon.enter();
				if (!status_reader_run[0])
				{
					this_mon.exit();
					break MISSING_BLOCK_LABEL_146;
				}
				this_mon.exit();
				break MISSING_BLOCK_LABEL_66;
				Throwable e;
				e;
				this_mon.exit();
				throw e;
				String s = con.getResponseMessage();
				if (!s.equals(getStatus()))
				{
					if (!s.toLowerCase().startsWith("error:"))
						setStatus(s);
					else
						error(con.getResponseCode(), s.substring(6));
					changed_status = true;
				}
				  goto _L1
				s;
				if (changed_status)
					setStatus("");
				return;
			}

			
			{
				this$0 = TorrentDownloaderImpl.this;
				status_reader_run = aflag;
				super(x0);
			}
		};
		status_reader.setDaemon(true);
		status_reader.start();
		InputStream in = con.getInputStream();
		this_mon.enter();
		status_reader_run[0] = false;
		this_mon.exit();
		break MISSING_BLOCK_LABEL_1391;
		Exception exception;
		exception;
		this_mon.exit();
		throw exception;
		FileNotFoundException e;
		e;
		if (ignoreReponseCode)
			in = con.getErrorStream();
		else
			throw e;
		this_mon.enter();
		status_reader_run[0] = false;
		this_mon.exit();
		break MISSING_BLOCK_LABEL_1391;
		Exception exception1;
		exception1;
		this_mon.exit();
		throw exception1;
		Exception exception2;
		exception2;
		this_mon.enter();
		status_reader_run[0] = false;
		this_mon.exit();
		break MISSING_BLOCK_LABEL_1388;
		Exception exception3;
		exception3;
		this_mon.exit();
		throw exception3;
		throw exception2;
		if (state == 4)
			break MISSING_BLOCK_LABEL_1912;
		file = new File(directoryname, filename);
		boolean useTempFile = false;
		try
		{
			file.createNewFile();
			useTempFile = !file.exists();
		}
		catch (Throwable t)
		{
			useTempFile = true;
		}
		if (useTempFile)
		{
			file = File.createTempFile("AZU", ".torrent", new File(directoryname));
			file.createNewFile();
		}
		FileOutputStream fileout = new FileOutputStream(file, false);
		bufBytes = 0;
		int size = con.getContentLength();
		percentDone = -1;
		do
		{
			if (cancel)
				break;
			try
			{
				bufBytes = in.read(buf);
				readTotal += bufBytes;
				if (size != 0)
					percentDone = (100 * readTotal) / size;
				notifyListener();
			}
			catch (IOException e) { }
			if (bufBytes > 0)
				fileout.write(buf, 0, bufBytes);
		} while (bufBytes > 0);
		in.close();
		fileout.flush();
		fileout.close();
		if (cancel)
		{
			state = 6;
			if (deleteFileOnCancel)
				cleanUpFile();
			break MISSING_BLOCK_LABEL_1812;
		}
		if (readTotal <= 0)
		{
			error(0, (new StringBuilder()).append("No data contained in '").append(url.toString()).append("'").toString());
			return;
		}
		try
		{
			if (!filename.toLowerCase().endsWith(".torrent"))
			{
				org.gudy.azureus2.core3.torrent.TOTorrent torrent = TorrentUtils.readFromFile(file, false);
				String name = (new StringBuilder()).append(TorrentUtils.getLocalisedName(torrent)).append(".torrent").toString();
				File new_file = new File(directoryname, name);
				if (file.renameTo(new_file))
				{
					filename = name;
					file = new_file;
				}
			}
		}
		catch (Throwable e)
		{
			Debug.printStackTrace(e);
		}
		TorrentUtils.setObtainedFrom(file, original_url);
		state = 3;
		notifyListener();
		break MISSING_BLOCK_LABEL_1912;
		status_reader_run;
		if (!cancel)
			Debug.out((new StringBuilder()).append("'").append(directoryname).append("' '").append(filename).append("'").toString(), status_reader_run);
		error(0, (new StringBuilder()).append("Exception while downloading '").append(url.toString()).append("':").append(status_reader_run.getMessage()).toString());
	}

	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj instanceof TorrentDownloaderImpl)
		{
			TorrentDownloaderImpl other = (TorrentDownloaderImpl)obj;
			if (other.getURL().equals(url.toString()))
			{
				File other_file = other.getFile();
				File this_file = file;
				if (other_file == this_file)
					return true;
				if (other_file == null || this_file == null)
					return false;
				else
					return other_file.getAbsolutePath().equals(this_file.getAbsolutePath());
			} else
			{
				return false;
			}
		} else
		{
			return false;
		}
	}

	public int hashCode()
	{
		return url.hashCode();
	}

	public String getError()
	{
		return error;
	}

	public void setError(int errCode, String err)
	{
		error = err;
		this.errCode = errCode;
	}

	public int getErrorCode()
	{
		return errCode;
	}

	protected void setStatus(String str)
	{
		status = str;
		notifyListener();
	}

	public String getStatus()
	{
		return status;
	}

	public File getFile()
	{
		if (!isAlive() || file == null)
			file = new File(directoryname, filename);
		return file;
	}

	public int getPercentDone()
	{
		return percentDone;
	}

	public int getDownloadState()
	{
		return state;
	}

	public void setDownloadState(int state)
	{
		this.state = state;
	}

	public String getURL()
	{
		return url.toString();
	}

	public void cancel()
	{
		cancel = true;
		if (con instanceof MagnetConnection)
			con.disconnect();
	}

	public void setDownloadPath(String path, String file)
	{
		if (!isAlive())
		{
			if (path != null)
				directoryname = path;
			if (file != null)
				filename = file;
		}
	}

	public int getTotalRead()
	{
		return readTotal;
	}

	public byte[] getLastReadBytes()
	{
		if (bufBytes <= 0)
		{
			return new byte[0];
		} else
		{
			byte bytes[] = new byte[bufBytes];
			System.arraycopy(buf, 0, bytes, 0, bufBytes);
			return bytes;
		}
	}

	public int getLastReadCount()
	{
		return bufBytes;
	}

	public void setDeleteFileOnCancel(boolean deleteFileOnCancel)
	{
		this.deleteFileOnCancel = deleteFileOnCancel;
	}

	public boolean getDeleteFileOnCancel()
	{
		return deleteFileOnCancel;
	}

	public boolean isIgnoreReponseCode()
	{
		return ignoreReponseCode;
	}

	public void setIgnoreReponseCode(boolean ignoreReponseCode)
	{
		this.ignoreReponseCode = ignoreReponseCode;
	}



}

⌨️ 快捷键说明

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