use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class TOTorrentCreatorImpl method create.
@Override
public TOTorrent create() throws TOTorrentException {
try {
if (announce_url == null) {
throw (new TOTorrentException("Skeleton creator", TOTorrentException.RT_WRITE_FAILS));
}
File base_to_use;
if (is_desc) {
base_to_use = createLayoutMap();
} else {
base_to_use = torrent_base;
}
if (piece_length > 0) {
torrent = new TOTorrentCreateImpl(linkage_map, base_to_use, announce_url, add_other_hashes, piece_length);
} else {
torrent = new TOTorrentCreateImpl(linkage_map, base_to_use, announce_url, add_other_hashes, piece_min_size, piece_max_size, piece_num_lower, piece_num_upper);
}
for (TOTorrentProgressListener l : listeners) {
torrent.addListener(l);
}
torrent.create();
return (torrent);
} finally {
if (is_desc) {
destroyLayoutMap();
}
}
}
use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class TOTorrentCreatorImpl method createLayoutMap.
private File createLayoutMap() throws TOTorrentException {
if (descriptor_dir != null) {
return (descriptor_dir);
}
try {
descriptor_dir = AETemporaryFileHandler.createTempDir();
File top_level_file = null;
List<DescEntry> desc_entries = readDescriptor();
for (DescEntry entry : desc_entries) {
List<String> logical_path = entry.getLogicalPath();
File target = entry.getTarget();
File temp = descriptor_dir;
int prefix_length = descriptor_dir.getAbsolutePath().length() + 1;
for (int i = 0; i < logical_path.size(); i++) {
temp = new File(temp, logical_path.get(i));
if (top_level_file == null) {
top_level_file = temp;
}
}
if (target.isDirectory()) {
if (!temp.isDirectory()) {
if (!temp.mkdirs()) {
throw (new TOTorrentException("Failed to create logical directory: " + temp, TOTorrentException.RT_WRITE_FAILS));
}
}
mapDirectory(prefix_length, target, temp);
} else {
File p = temp.getParentFile();
if (!p.isDirectory()) {
if (!p.mkdirs()) {
throw (new TOTorrentException("Failed to create logical directory: " + p, TOTorrentException.RT_WRITE_FAILS));
}
}
if (temp.exists()) {
throw (new TOTorrentException("Duplicate file: " + temp, TOTorrentException.RT_WRITE_FAILS));
} else {
temp.createNewFile();
linkage_map.put(temp.getAbsolutePath().substring(prefix_length), target);
}
}
}
return (top_level_file);
} catch (TOTorrentException e) {
throw (e);
} catch (Throwable e) {
throw (new TOTorrentException(Debug.getNestedExceptionMessage(e), TOTorrentException.RT_WRITE_FAILS));
}
}
use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class TOTorrentFileHasher method add.
long add(File _file) throws TOTorrentException {
long file_length = 0;
InputStream is = null;
SHA1Hasher sha1_hash = null;
ED2KHasher ed2k_hash = null;
try {
if (do_other_per_file_hash) {
sha1_hash = new SHA1Hasher();
ed2k_hash = new ED2KHasher();
}
is = new BufferedInputStream(new FileInputStream(_file), 65536);
while (true) {
if (cancelled) {
throw (new TOTorrentException("TOTorrentCreate: operation cancelled", TOTorrentException.RT_CANCELLED));
}
int len = is.read(buffer, buffer_pos, piece_length - buffer_pos);
if (len > 0) {
if (do_other_per_file_hash) {
sha1_hash.update(buffer, buffer_pos, len);
ed2k_hash.update(buffer, buffer_pos, len);
}
file_length += len;
buffer_pos += len;
if (buffer_pos == piece_length) {
// hash this piece
byte[] hash = new SHA1Hasher().calculateHash(buffer);
if (overall_sha1_hash != null) {
overall_sha1_hash.update(buffer);
overall_ed2k_hash.update(buffer);
}
pieces.add(hash);
if (listener != null) {
listener.pieceHashed(pieces.size());
}
buffer_pos = 0;
}
} else {
break;
}
}
if (do_other_per_file_hash) {
per_file_sha1_digest = sha1_hash.getDigest();
per_file_ed2k_digest = ed2k_hash.getDigest();
}
} catch (TOTorrentException e) {
throw (e);
} catch (Throwable e) {
throw (new TOTorrentException("TOTorrentFileHasher: file read fails '" + e.toString() + "'", TOTorrentException.RT_READ_FAILS));
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
}
return (file_length);
}
use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class TOTorrentXMLDeserialiser method deserialise.
public TOTorrent deserialise(File file) throws TOTorrentException {
try {
SimpleXMLParserDocument doc = SimpleXMLParserDocumentFactory.create(file);
TOTorrent res = decodeRoot(doc);
return (res);
} catch (SimpleXMLParserDocumentException e) {
throw (new TOTorrentException("XML Parse Fails: " + e.getMessage(), TOTorrentException.RT_DECODE_FAILS));
}
}
use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.
the class TOTorrentXMLDeserialiser method decodeRoot.
protected TOTorrent decodeRoot(SimpleXMLParserDocument doc) throws TOTorrentException {
String root_name = doc.getName();
if (root_name.equalsIgnoreCase("TORRENT")) {
TOTorrentImpl torrent = new TOTorrentImpl();
SimpleXMLParserDocumentNode[] kids = doc.getChildren();
URL announce_url = null;
byte[] torrent_hash = null;
byte[] torrent_hash_override = null;
for (int i = 0; i < kids.length; i++) {
SimpleXMLParserDocumentNode kid = kids[i];
String name = kid.getName();
if (name.equalsIgnoreCase("ANNOUNCE_URL")) {
try {
announce_url = new URL(kid.getValue());
} catch (MalformedURLException e) {
throw (new TOTorrentException("ANNOUNCE_URL malformed", TOTorrentException.RT_DECODE_FAILS));
}
} else if (name.equalsIgnoreCase("ANNOUNCE_LIST")) {
SimpleXMLParserDocumentNode[] set_nodes = kid.getChildren();
TOTorrentAnnounceURLGroup group = torrent.getAnnounceURLGroup();
TOTorrentAnnounceURLSet[] sets = new TOTorrentAnnounceURLSet[set_nodes.length];
for (int j = 0; j < sets.length; j++) {
SimpleXMLParserDocumentNode[] url_nodes = set_nodes[j].getChildren();
URL[] urls = new URL[url_nodes.length];
for (int k = 0; k < urls.length; k++) {
try {
urls[k] = new URL(url_nodes[k].getValue());
} catch (MalformedURLException e) {
throw (new TOTorrentException("ANNOUNCE_LIST malformed", TOTorrentException.RT_DECODE_FAILS));
}
}
sets[j] = group.createAnnounceURLSet(urls);
}
group.setAnnounceURLSets(sets);
} else if (name.equalsIgnoreCase("COMMENT")) {
torrent.setComment(readLocalisableString(kid));
} else if (name.equalsIgnoreCase("CREATED_BY")) {
torrent.setCreatedBy(readLocalisableString(kid));
} else if (name.equalsIgnoreCase("CREATION_DATE")) {
torrent.setCreationDate(readGenericLong(kid).longValue());
} else if (name.equalsIgnoreCase("TORRENT_HASH")) {
torrent_hash = readGenericBytes(kid);
} else if (name.equalsIgnoreCase("TORRENT_HASH_OVERRIDE")) {
torrent_hash_override = readGenericBytes(kid);
} else if (name.equalsIgnoreCase("INFO")) {
decodeInfo(kid, torrent);
} else {
mapEntry entry = readGenericMapEntry(kid);
torrent.addAdditionalProperty(entry.name, entry.value);
}
}
if (announce_url == null) {
throw (new TOTorrentException("ANNOUNCE_URL missing", TOTorrentException.RT_DECODE_FAILS));
}
torrent.setAnnounceURL(announce_url);
if (torrent_hash_override != null) {
try {
torrent.setHashOverride(torrent_hash_override);
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
if (torrent_hash != null) {
if (!Arrays.equals(torrent.getHash(), torrent_hash)) {
throw (new TOTorrentException("Hash differs - declared TORRENT_HASH and computed hash differ. If this really is the intent (unlikely) then remove the TORRENT_HASH element", TOTorrentException.RT_DECODE_FAILS));
}
}
return (torrent);
} else {
throw (new TOTorrentException("Invalid root element", TOTorrentException.RT_DECODE_FAILS));
}
}
Aggregations