📄 totorrentcreateimpl.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: TOTorrentCreateImpl.java
package org.gudy.azureus2.core3.torrent.impl;
import java.io.File;
import java.net.URL;
import java.util.*;
import org.gudy.azureus2.core3.internat.MessageText;
import org.gudy.azureus2.core3.torrent.*;
import org.gudy.azureus2.core3.util.*;
// Referenced classes of package org.gudy.azureus2.core3.torrent.impl:
// TOTorrentImpl, TOTorrentCreatorImpl, TOTorrentFileHasher, TOTorrentFileHasherListener,
// TOTorrentFileImpl
public class TOTorrentCreateImpl extends TOTorrentImpl
implements TOTorrentFileHasherListener
{
protected File torrent_base;
protected long piece_length;
protected TOTorrentFileHasher file_hasher;
protected long total_file_size;
protected long total_file_count;
protected long piece_count;
protected boolean add_other_hashes;
protected List progress_listeners;
protected int reported_progress;
protected Set ignore_set;
protected boolean cancelled;
public static TOTorrentCreator create(File _torrent_base, URL _announce_url, boolean _add_other_hashes, long _piece_length)
throws TOTorrentException
{
TOTorrentCreateImpl t = new TOTorrentCreateImpl(_torrent_base, _announce_url, _add_other_hashes, _piece_length);
return new TOTorrentCreatorImpl(t);
}
public static TOTorrentCreator create(File _torrent_base, URL _announce_url, boolean _add_other_hashes, long _piece_min_size, long _piece_max_size, long _piece_num_lower, long _piece_num_upper)
throws TOTorrentException
{
TOTorrentCreateImpl t = new TOTorrentCreateImpl(_torrent_base, _announce_url, _add_other_hashes, _piece_min_size, _piece_max_size, _piece_num_lower, _piece_num_upper);
return new TOTorrentCreatorImpl(t);
}
protected TOTorrentCreateImpl(File _torrent_base, URL _announce_url, boolean _add_other_hashes, long _piece_length)
throws TOTorrentException
{
super(_torrent_base.getName(), _announce_url, _torrent_base.isFile());
total_file_size = -1L;
total_file_count = 0L;
progress_listeners = new ArrayList();
ignore_set = new HashSet();
torrent_base = _torrent_base;
piece_length = _piece_length;
add_other_hashes = _add_other_hashes;
}
protected TOTorrentCreateImpl(File _torrent_base, URL _announce_url, boolean _add_other_hashes, long _piece_min_size, long _piece_max_size,
long _piece_num_lower, long _piece_num_upper)
throws TOTorrentException
{
super(_torrent_base.getName(), _announce_url, _torrent_base.isFile());
total_file_size = -1L;
total_file_count = 0L;
progress_listeners = new ArrayList();
ignore_set = new HashSet();
torrent_base = _torrent_base;
add_other_hashes = _add_other_hashes;
long total_size = calculateTotalFileSize(_torrent_base);
piece_length = getComputedPieceSize(total_size, _piece_min_size, _piece_max_size, _piece_num_lower, _piece_num_upper);
}
protected void create()
throws TOTorrentException
{
constructFixed(torrent_base, piece_length);
}
protected void constructFixed(File _torrent_base, long _piece_length)
throws TOTorrentException
{
setIgnoreList();
setCreationDate(SystemTime.getCurrentTime() / 1000L);
setCreatedBy("Azureus/4.2.0.0");
setPieceLength(_piece_length);
report("Torrent.create.progress.piecelength", _piece_length);
piece_count = calculateNumberOfPieces(_torrent_base, _piece_length);
if (piece_count == 0L)
throw new TOTorrentException("TOTorrentCreate: specified files have zero total length", 2);
report("Torrent.create.progress.hashing");
for (int i = 0; i < progress_listeners.size(); i++)
((TOTorrentProgressListener)progress_listeners.get(i)).reportProgress(0);
boolean add_other_per_file_hashes = add_other_hashes && !getSimpleTorrent();
file_hasher = new TOTorrentFileHasher(add_other_hashes, add_other_per_file_hashes, (int)_piece_length, progress_listeners.size() != 0 ? ((TOTorrentFileHasherListener) (this)) : null);
if (cancelled)
throw new TOTorrentException("TOTorrentCreate: operation cancelled", 9);
if (getSimpleTorrent())
{
long length = file_hasher.add(_torrent_base);
setFiles(new TOTorrentFileImpl[] {
new TOTorrentFileImpl(this, 0L, length, new byte[][] {
getName()
})
});
setPieces(file_hasher.getPieces());
} else
{
Vector encoded = new Vector();
processDir(file_hasher, _torrent_base, encoded, "");
TOTorrentFileImpl files[] = new TOTorrentFileImpl[encoded.size()];
encoded.copyInto(files);
setFiles(files);
}
setPieces(file_hasher.getPieces());
if (add_other_hashes)
{
byte sha1_digest[] = file_hasher.getSHA1Digest();
byte ed2k_digest[] = file_hasher.getED2KDigest();
addAdditionalInfoProperty("sha1", sha1_digest);
addAdditionalInfoProperty("ed2k", ed2k_digest);
}
}
protected void processDir(TOTorrentFileHasher hasher, File dir, Vector encoded, String root)
throws TOTorrentException
{
File dir_file_list[] = dir.listFiles();
if (dir_file_list == null)
throw new TOTorrentException((new StringBuilder()).append("TOTorrentCreate: directory '").append(dir.getAbsolutePath()).append("' returned error when listing files in it").toString(), 1);
List file_list = new ArrayList(Arrays.asList(dir_file_list));
Collections.sort(file_list);
long offset = 0L;
for (int i = 0; i < file_list.size(); i++)
{
File file = (File)file_list.get(i);
String file_name = file.getName();
if (file_name.equals(".") || file_name.equals(".."))
continue;
if (file.isDirectory())
{
if (root.length() > 0)
file_name = (new StringBuilder()).append(root).append(File.separator).append(file_name).toString();
processDir(hasher, file, encoded, file_name);
continue;
}
if (ignoreFile(file_name))
continue;
if (root.length() > 0)
file_name = (new StringBuilder()).append(root).append(File.separator).append(file_name).toString();
long length = hasher.add(file);
TOTorrentFileImpl tf = new TOTorrentFileImpl(this, offset, length, file_name);
offset += length;
if (add_other_hashes)
{
byte ed2k_digest[] = hasher.getPerFileED2KDigest();
byte sha1_digest[] = hasher.getPerFileSHA1Digest();
tf.setAdditionalProperty("sha1", sha1_digest);
tf.setAdditionalProperty("ed2k", ed2k_digest);
}
encoded.addElement(tf);
}
}
public void pieceHashed(int piece_number)
{
for (int i = 0; i < progress_listeners.size(); i++)
{
int this_progress = (int)((long)(piece_number * 100) / piece_count);
if (this_progress != reported_progress)
{
reported_progress = this_progress;
((TOTorrentProgressListener)progress_listeners.get(i)).reportProgress(reported_progress);
}
}
}
protected long calculateNumberOfPieces(File _file, long _piece_length)
throws TOTorrentException
{
long res = getPieceCount(calculateTotalFileSize(_file), _piece_length);
report("Torrent.create.progress.piececount", (new StringBuilder()).append("").append(res).toString());
return res;
}
protected long calculateTotalFileSize(File file)
throws TOTorrentException
{
if (total_file_size == -1L)
total_file_size = getTotalFileSize(file);
return total_file_size;
}
protected long getTotalFileSize(File file)
throws TOTorrentException
{
report("Torrent.create.progress.parsingfiles");
long res = getTotalFileSizeSupport(file);
report("Torrent.create.progress.totalfilesize", res);
report("Torrent.create.progress.totalfilecount", (new StringBuilder()).append("").append(total_file_count).toString());
return res;
}
protected long getTotalFileSizeSupport(File file)
throws TOTorrentException
{
String name = file.getName();
if (name.equals(".") || name.equals(".."))
return 0L;
if (!file.exists())
throw new TOTorrentException((new StringBuilder()).append("TOTorrentCreate: file '").append(file.getName()).append("' doesn't exist").toString(), 1);
if (file.isFile())
if (!ignoreFile(name))
{
total_file_count++;
return file.length();
} else
{
return 0L;
}
File dir_files[] = file.listFiles();
if (dir_files == null)
throw new TOTorrentException((new StringBuilder()).append("TOTorrentCreate: directory '").append(file.getAbsolutePath()).append("' returned error when listing files in it").toString(), 1);
long length = 0L;
for (int i = 0; i < dir_files.length; i++)
length += getTotalFileSizeSupport(dir_files[i]);
return length;
}
protected void report(String resource_key)
{
report(resource_key, ((String) (null)));
}
protected void report(String resource_key, long bytes)
{
if (progress_listeners.size() > 0)
report(resource_key, DisplayFormatters.formatByteCountToKiBEtc(bytes));
}
protected void report(String resource_key, String additional_text)
{
if (progress_listeners.size() > 0)
{
String prefix = MessageText.getString(resource_key);
for (int i = 0; i < progress_listeners.size(); i++)
((TOTorrentProgressListener)progress_listeners.get(i)).reportCurrentTask((new StringBuilder()).append(prefix).append(additional_text != null ? additional_text : "").toString());
}
}
protected static long getTorrentDataSizeFromFileOrDirSupport(File file)
{
String name = file.getName();
if (name.equals(".") || name.equals(".."))
return 0L;
if (!file.exists())
return 0L;
if (file.isFile())
return file.length();
File dir_files[] = file.listFiles();
long length = 0L;
for (int i = 0; i < dir_files.length; i++)
length += getTorrentDataSizeFromFileOrDirSupport(dir_files[i]);
return length;
}
public static long getTorrentDataSizeFromFileOrDir(File file_or_dir)
{
return getTorrentDataSizeFromFileOrDirSupport(file_or_dir);
}
public static long getComputedPieceSize(long total_size, long _piece_min_size, long _piece_max_size, long _piece_num_lower,
long _piece_num_upper)
{
long piece_length = -1L;
long current_piece_size = _piece_min_size;
do
{
if (current_piece_size > _piece_max_size)
break;
long pieces = total_size / current_piece_size;
if (pieces <= _piece_num_upper)
{
piece_length = current_piece_size;
break;
}
current_piece_size <<= 1;
} while (true);
if (piece_length == -1L)
piece_length = _piece_max_size;
return piece_length;
}
public static long getPieceCount(long total_size, long piece_size)
{
return (total_size + (piece_size - 1L)) / piece_size;
}
protected void setIgnoreList()
{
try
{
ignore_set = TorrentUtils.getIgnoreSet();
}
catch (NoClassDefFoundError e)
{
return;
}
}
protected boolean ignoreFile(String file)
{
if (ignore_set.contains(file.toLowerCase()))
{
report("Torrent.create.progress.ignoringfile", (new StringBuilder()).append(" '").append(file).append("'").toString());
return true;
} else
{
return false;
}
}
protected void cancel()
{
if (!cancelled)
{
report("Torrent.create.progress.cancelled");
cancelled = true;
if (file_hasher != null)
file_hasher.cancel();
}
}
protected void addListener(TOTorrentProgressListener listener)
{
progress_listeners.add(listener);
}
protected void removeListener(TOTorrentProgressListener listener)
{
progress_listeners.remove(listener);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -