📄 resourcedownloadertorrentimpl.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: ResourceDownloaderTorrentImpl.java
package org.gudy.azureus2.pluginsimpl.local.utils.resourcedownloader;
import com.aelitis.azureus.core.util.HTTPUtils;
import java.io.*;
import org.gudy.azureus2.core3.torrent.*;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.plugins.PluginInterface;
import org.gudy.azureus2.plugins.download.*;
import org.gudy.azureus2.plugins.utils.Utilities;
import org.gudy.azureus2.plugins.utils.resourcedownloader.*;
import org.gudy.azureus2.pluginsimpl.local.PluginInitializer;
import org.gudy.azureus2.pluginsimpl.local.torrent.TorrentImpl;
// Referenced classes of package org.gudy.azureus2.pluginsimpl.local.utils.resourcedownloader:
// ResourceDownloaderBaseImpl
public class ResourceDownloaderTorrentImpl extends ResourceDownloaderBaseImpl
implements ResourceDownloaderListener
{
public static final int MAX_FOLLOWS = 1;
protected ResourceDownloaderBaseImpl delegate;
protected boolean persistent;
protected File download_dir;
protected long size;
protected TOTorrent torrent_holder[];
protected DownloadManager download_manager;
protected Download download;
protected boolean cancelled;
protected ResourceDownloader current_downloader;
protected Object result;
protected AESemaphore done_sem;
public ResourceDownloaderTorrentImpl(ResourceDownloaderBaseImpl _parent, ResourceDownloader _delegate, boolean _persistent, File _download_dir)
{
super(_parent);
size = -2L;
torrent_holder = new TOTorrent[1];
done_sem = new AESemaphore("RDTorrent");
persistent = _persistent;
download_dir = _download_dir;
delegate = (ResourceDownloaderBaseImpl)_delegate;
delegate.setParent(this);
download_manager = PluginInitializer.getDefaultInterface().getDownloadManager();
}
public String getName()
{
return (new StringBuilder()).append(delegate.getName()).append(": torrent").toString();
}
public long getSize()
throws ResourceDownloaderException
{
if (size != -2L)
break MISSING_BLOCK_LABEL_77;
size = getSizeSupport();
if (size == -2L)
size = -1L;
setSize(size);
break MISSING_BLOCK_LABEL_77;
Exception exception;
exception;
if (size == -2L)
size = -1L;
setSize(size);
throw exception;
return size;
}
protected void setSize(long l)
{
size = l;
if (size >= 0L)
delegate.setSize(size);
}
public void setProperty(String name, Object value)
throws ResourceDownloaderException
{
setPropertySupport(name, value);
delegate.setProperty(name, value);
}
protected long getSizeSupport()
throws ResourceDownloaderException
{
InputStream is;
if (torrent_holder[0] != null)
break MISSING_BLOCK_LABEL_89;
ResourceDownloader x = delegate.getClone(this);
addReportListener(x);
is = x.download();
torrent_holder[0] = TOTorrentFactory.deserialiseFromBEncodedInputStream(is);
try
{
is.close();
}
catch (IOException e) { }
break MISSING_BLOCK_LABEL_65;
Exception exception;
exception;
try
{
is.close();
}
catch (IOException e) { }
throw exception;
if (!torrent_holder[0].isSimpleTorrent())
throw new ResourceDownloaderException("Only simple torrents supported");
TOTorrentException e;
try
{
String file_str = new String(torrent_holder[0].getName());
int pos = file_str.lastIndexOf(".");
String file_type;
if (pos != -1)
file_type = file_str.substring(pos + 1);
else
file_type = null;
setProperty("ContentType", HTTPUtils.guessContentTypeFromFileType(file_type));
}
// Misplaced declaration of an exception variable
catch (TOTorrentException e)
{
Debug.printStackTrace(e);
}
return torrent_holder[0].getSize();
e;
throw new ResourceDownloaderException("Torrent deserialisation failed", e);
}
protected void setSizeAndTorrent(long _size, TOTorrent _torrent_holder[])
{
size = _size;
torrent_holder = _torrent_holder;
}
public ResourceDownloaderBaseImpl getClone(ResourceDownloaderBaseImpl parent)
{
ResourceDownloaderTorrentImpl c = new ResourceDownloaderTorrentImpl(parent, delegate.getClone(this), persistent, download_dir);
c.setSizeAndTorrent(size, torrent_holder);
c.setProperties(this);
return c;
}
public InputStream download()
throws ResourceDownloaderException
{
asyncDownload();
done_sem.reserve();
if (result instanceof InputStream)
return (InputStream)result;
else
throw (ResourceDownloaderException)result;
}
public void asyncDownload()
{
this_mon.enter();
if (cancelled)
{
done_sem.release();
informFailed((ResourceDownloaderException)result);
} else
if (torrent_holder[0] == null)
{
current_downloader = delegate.getClone(this);
informActivity((new StringBuilder()).append(getLogIndent()).append("Downloading: ").append(getName()).toString());
current_downloader.addListener(this);
current_downloader.asyncDownload();
} else
{
downloadTorrent();
}
this_mon.exit();
break MISSING_BLOCK_LABEL_135;
Exception exception;
exception;
this_mon.exit();
throw exception;
}
protected void downloadTorrent()
{
try
{
informActivity((new StringBuilder()).append(getLogIndent()).append("Downloading: ").append(new String(torrent_holder[0].getName(), "UTF8")).toString());
final File torrent_file = File.createTempFile("AZU", null);
if (download_dir != null && !download_dir.exists())
FileUtil.mkdirs(download_dir);
final File data_dir = download_dir != null ? download_dir : torrent_file.getParentFile();
TOTorrent torrent = torrent_holder[0];
TorrentUtils.setFlag(torrent, 1, true);
torrent.serialiseToBEncodedFile(torrent_file);
if (persistent)
download = download_manager.addDownload(new TorrentImpl(torrent), torrent_file, data_dir);
else
download = download_manager.addNonPersistentDownload(new TorrentImpl(torrent), torrent_file, data_dir);
download.moveTo(1);
download.setForceStart(true);
download.setFlag(4L, true);
download_manager.addListener(new DownloadManagerListener() {
final File val$torrent_file;
final File val$data_dir;
final ResourceDownloaderTorrentImpl this$0;
public void downloadAdded(Download download1)
{
}
public void downloadRemoved(Download _download)
{
if (download == _download)
ResourceDownloaderTorrentImpl.this.downloadRemoved(torrent_file, data_dir);
}
{
this$0 = ResourceDownloaderTorrentImpl.this;
torrent_file = file;
data_dir = file1;
super();
}
});
download.addListener(new DownloadListener() {
final File val$torrent_file;
final File val$data_dir;
final ResourceDownloaderTorrentImpl this$0;
public void stateChanged(final Download download, int old_state, int new_state)
{
if (new_state == 5)
{
download.removeListener(this);
PluginInitializer.getDefaultInterface().getUtilities().createThread("resource complete event dispatcher", new Runnable() {
final Download val$download;
final 2 this$1;
public void run()
{
downloadSucceeded(download, torrent_file, data_dir);
}
{
this$1 = 2.this;
download = download1;
super();
}
});
}
}
public void positionChanged(Download download1, int i, int j)
{
}
{
this$0 = ResourceDownloaderTorrentImpl.this;
torrent_file = file;
data_dir = file1;
super();
}
});
Thread t = new AEThread(torrent) {
final TOTorrent val$torrent;
final ResourceDownloaderTorrentImpl this$0;
public void runSupport()
{
int last_percentage = 0;
while (result == null)
{
int this_percentage = download.getStats().getCompleted() / 10;
long total = torrent.getSize();
if (this_percentage != last_percentage)
{
reportPercentComplete(ResourceDownloaderTorrentImpl.this, this_percentage);
last_percentage = this_percentage;
}
try
{
Thread.sleep(1000L);
}
catch (Throwable e)
{
Debug.printStackTrace(e);
}
}
}
{
this$0 = ResourceDownloaderTorrentImpl.this;
torrent = totorrent;
super(x0);
}
};
t.setDaemon(true);
t.start();
if (download.getState() == 5)
downloadSucceeded(download, torrent_file, data_dir);
}
catch (Throwable e)
{
failed(this, new ResourceDownloaderException("Torrent download failed", e));
}
}
protected void downloadSucceeded(Download download, File torrent_file, File data_dir)
{
reportActivity("Torrent download complete");
File target_file = new File(data_dir, new String(torrent_holder[0].getFiles()[0].getPathComponents()[0]));
if (!target_file.exists())
{
File actual_target_file = new File(download.getSavePath());
try
{
if (download_dir != null && actual_target_file.exists())
FileUtil.copyFile(actual_target_file, target_file);
}
catch (Throwable e)
{
Debug.printStackTrace(e);
}
target_file = actual_target_file;
}
try
{
if (!target_file.exists())
throw new Exception((new StringBuilder()).append("File '").append(target_file.toString()).append("' not found").toString());
InputStream data = new FileInputStream(target_file);
informComplete(data);
result = data;
done_sem.release();
}
catch (Throwable e)
{
Debug.printStackTrace(e);
failed(this, new ResourceDownloaderException((new StringBuilder()).append("Failed to read downloaded torrent data: ").append(e.getMessage()).toString(), e));
}
}
protected void downloadRemoved(File torrent_file, File data_dir)
{
reportActivity("Torrent removed");
if (!(result instanceof InputStream))
failed(this, new ResourceDownloaderException("Download did not complete"));
}
public void cancel()
{
setCancelled();
this_mon.enter();
result = new ResourceDownloaderCancelledException();
cancelled = true;
informFailed((ResourceDownloaderException)result);
done_sem.release();
if (current_downloader != null)
current_downloader.cancel();
this_mon.exit();
break MISSING_BLOCK_LABEL_81;
Exception exception;
exception;
this_mon.exit();
throw exception;
}
public boolean completed(ResourceDownloader downloader, InputStream data)
{
torrent_holder[0] = TOTorrentFactory.deserialiseFromBEncodedInputStream(data);
if (torrent_holder[0].isSimpleTorrent())
downloadTorrent();
else
failed(this, new ResourceDownloaderException("Only simple torrents supported"));
TOTorrentException e;
try
{
data.close();
}
// Misplaced declaration of an exception variable
catch (TOTorrentException e) { }
break MISSING_BLOCK_LABEL_97;
e;
failed(downloader, new ResourceDownloaderException("Torrent deserialisation failed", e));
try
{
data.close();
}
// Misplaced declaration of an exception variable
catch (TOTorrentException e) { }
break MISSING_BLOCK_LABEL_97;
Exception exception;
exception;
try
{
data.close();
}
catch (IOException e) { }
throw exception;
return true;
}
public void failed(ResourceDownloader downloader, ResourceDownloaderException e)
{
result = e;
done_sem.release();
informFailed(e);
}
public void reportPercentComplete(ResourceDownloader downloader, int percentage)
{
if (downloader == this)
informPercentDone(percentage);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -