From c77d79513e3da06b75e70312a7210d5e17225422 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 29 Oct 2019 15:34:48 +0000 Subject: [PATCH] more long arithmetic fixes --- .../groovy/com/muwire/core/files/FileHasher.groovy | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy b/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy index 2e8776ab..bf13c067 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy @@ -50,27 +50,28 @@ class FileHasher { InfoHash hashFile(File file) { final long length = file.length() - final int size = 0x1 << getPieceSize(length) - int numPieces = (int) (length / size) + final long size = 0x1L << getPieceSize(length) + int numPieces = (length / size).toInteger() if (numPieces * size < length) numPieces++ def output = new ByteArrayOutputStream() RandomAccessFile raf = new RandomAccessFile(file, "r") + MappedByteBuffer buf = null try { - MappedByteBuffer buf 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 DataUtil.tryUnmap(buf) output.write(digest.digest(), 0, 32) } - def lastPieceLength = length - (numPieces - 1) * ((long)size) - buf = raf.getChannel().map(MapMode.READ_ONLY, length - lastPieceLength, lastPieceLength) + long lastPieceLength = length - (numPieces - 1) * size + buf = raf.getChannel().map(MapMode.READ_ONLY, length - lastPieceLength, lastPieceLength.toInteger()) digest.update buf output.write(digest.digest(), 0, 32) } finally { raf.close() + DataUtil.tryUnmap(buf) } byte [] hashList = output.toByteArray()