Skip to content

Commit

Permalink
Fix OutOfMemoryError on file hash calculation (#71)
Browse files Browse the repository at this point in the history
This prevents OutOfMemoryError while reading large files (> 2GB)
  • Loading branch information
DominikWolek authored May 12, 2020
1 parent b6f7ec6 commit 374110b
Showing 1 changed file with 2 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pl.edu.uj.ii.ksi.mordor.services.hash

import com.coremedia.iso.Hex
import java.io.File
import java.nio.file.Files
import java.security.MessageDigest
import org.springframework.stereotype.Service

Expand All @@ -14,7 +13,7 @@ class Sha256FileHashProvider : FileHashProvider {

override fun calculate(file: File): String {
val messageDigest = MessageDigest.getInstance(algorithm)
val bytes = Files.readAllBytes(file.toPath())
return Hex.encodeHex(messageDigest.digest(bytes))
file.forEachBlock { buffer, bytesRead -> messageDigest.update(buffer, 0, bytesRead) }
return Hex.encodeHex(messageDigest.digest())
}
}

0 comments on commit 374110b

Please sign in to comment.