Search in sources :

Example 11 with TOTorrentException

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();
        }
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) File(java.io.File) TOTorrentProgressListener(com.biglybt.core.torrent.TOTorrentProgressListener)

Example 12 with TOTorrentException

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));
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) File(java.io.File)

Example 13 with TOTorrentException

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);
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) ED2KHasher(com.biglybt.core.util.ED2KHasher) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SHA1Hasher(com.biglybt.core.util.SHA1Hasher) FileInputStream(java.io.FileInputStream) TOTorrentException(com.biglybt.core.torrent.TOTorrentException)

Example 14 with TOTorrentException

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));
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) SimpleXMLParserDocumentException(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException) SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 15 with TOTorrentException

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));
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) MalformedURLException(java.net.MalformedURLException) SimpleXMLParserDocumentNode(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentNode) URL(java.net.URL) TOTorrentAnnounceURLGroup(com.biglybt.core.torrent.TOTorrentAnnounceURLGroup)

Aggregations

TOTorrentException (com.biglybt.core.torrent.TOTorrentException)45 TOTorrent (com.biglybt.core.torrent.TOTorrent)23 File (java.io.File)15 IOException (java.io.IOException)9 URL (java.net.URL)8 DownloadManager (com.biglybt.core.download.DownloadManager)7 Core (com.biglybt.core.Core)3 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)3 GlobalManager (com.biglybt.core.global.GlobalManager)3 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)3 SimpleXMLParserDocumentNode (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentNode)3 List (java.util.List)3 Map (java.util.Map)3 CoreRunningListener (com.biglybt.core.CoreRunningListener)2 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)2 LogEvent (com.biglybt.core.logging.LogEvent)2 TOTorrentProgressListener (com.biglybt.core.torrent.TOTorrentProgressListener)2 TRHostTorrent (com.biglybt.core.tracker.host.TRHostTorrent)2 TorrentImpl (com.biglybt.pifimpl.local.torrent.TorrentImpl)2 TrackerEditorListener (com.biglybt.ui.swt.maketorrent.TrackerEditorListener)2