Search in sources :

Example 1 with ED2KHasher

use of com.biglybt.core.util.ED2KHasher 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)

Aggregations

TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 ED2KHasher (com.biglybt.core.util.ED2KHasher)1 SHA1Hasher (com.biglybt.core.util.SHA1Hasher)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1