more long arithmetic fixes

pull/24/head
Zlatin Balevsky 2019-10-29 15:34:48 +00:00
parent 9f12442897
commit c77d79513e
1 changed files with 7 additions and 6 deletions

View File

@ -50,27 +50,28 @@ class FileHasher {
InfoHash hashFile(File file) { InfoHash hashFile(File file) {
final long length = file.length() final long length = file.length()
final int size = 0x1 << getPieceSize(length) final long size = 0x1L << getPieceSize(length)
int numPieces = (int) (length / size) int numPieces = (length / size).toInteger()
if (numPieces * size < length) if (numPieces * size < length)
numPieces++ numPieces++
def output = new ByteArrayOutputStream() def output = new ByteArrayOutputStream()
RandomAccessFile raf = new RandomAccessFile(file, "r") RandomAccessFile raf = new RandomAccessFile(file, "r")
MappedByteBuffer buf = null
try { try {
MappedByteBuffer buf
for (int i = 0; i < numPieces - 1; i++) { for (int i = 0; i < numPieces - 1; i++) {
buf = raf.getChannel().map(MapMode.READ_ONLY, ((long)size) * i, size) buf = raf.getChannel().map(MapMode.READ_ONLY, size * i, size.toInteger())
digest.update buf digest.update buf
DataUtil.tryUnmap(buf) DataUtil.tryUnmap(buf)
output.write(digest.digest(), 0, 32) output.write(digest.digest(), 0, 32)
} }
def lastPieceLength = length - (numPieces - 1) * ((long)size) long lastPieceLength = length - (numPieces - 1) * size
buf = raf.getChannel().map(MapMode.READ_ONLY, length - lastPieceLength, lastPieceLength) buf = raf.getChannel().map(MapMode.READ_ONLY, length - lastPieceLength, lastPieceLength.toInteger())
digest.update buf digest.update buf
output.write(digest.digest(), 0, 32) output.write(digest.digest(), 0, 32)
} finally { } finally {
raf.close() raf.close()
DataUtil.tryUnmap(buf)
} }
byte [] hashList = output.toByteArray() byte [] hashList = output.toByteArray()