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);
}
Aggregations