| 1564 | |
| 1565 | == Scala == |
| 1566 | {{{ |
| 1567 | import java.io.{FileInputStream, File} |
| 1568 | import java.nio.{LongBuffer, ByteOrder, ByteBuffer} |
| 1569 | import java.nio.channels.FileChannel.MapMode |
| 1570 | import scala.math._ |
| 1571 | |
| 1572 | class OpenSubtitlesHasher { |
| 1573 | private val hashChunkSize = 64L * 1024L |
| 1574 | |
| 1575 | def computeHash(file: File) : String = { |
| 1576 | val fileSize = file.length |
| 1577 | val chunkSizeForFile = min(fileSize, hashChunkSize) |
| 1578 | |
| 1579 | val fileChannel = new FileInputStream(file).getChannel |
| 1580 | |
| 1581 | try { |
| 1582 | val head = computeHashForChunk(fileChannel.map(MapMode.READ_ONLY, 0, chunkSizeForFile)) |
| 1583 | val tail = computeHashForChunk(fileChannel.map(MapMode.READ_ONLY, max(fileSize - hashChunkSize, 0), chunkSizeForFile)) |
| 1584 | |
| 1585 | "%016x".format(fileSize + head + tail) |
| 1586 | } finally { |
| 1587 | fileChannel.close() |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | private def computeHashForChunk(buffer: ByteBuffer) : Long = { |
| 1592 | def doCompute(longBuffer: LongBuffer, hash: Long) : Long = { |
| 1593 | longBuffer.hasRemaining match { |
| 1594 | case false => hash |
| 1595 | case true => doCompute(longBuffer, hash + longBuffer.get) |
| 1596 | } |
| 1597 | } |
| 1598 | val longBuffer = buffer.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer() |
| 1599 | doCompute(longBuffer, 0L) |
| 1600 | } |
| 1601 | } |
| 1602 | }}} |