Changes between Version 45 and Version 46 of HashSourceCodes


Ignore:
Timestamp:
Apr 21, 2013, 4:28:08 AM (12 years ago)
Author:
os
Comment:

add scala

Legend:

Unmodified
Added
Removed
Modified
  • HashSourceCodes

    v45 v46  
    15621562end;
    15631563}}}
     1564
     1565== Scala ==
     1566{{{
     1567import java.io.{FileInputStream, File}
     1568import java.nio.{LongBuffer, ByteOrder, ByteBuffer}
     1569import java.nio.channels.FileChannel.MapMode
     1570import scala.math._
     1571
     1572class 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}}}