Ensure file handles are always closed (#122)
* [core] Ensure files are closed even when interrupted * [changelog] updated
This commit is contained in:
parent
cad152379e
commit
ad0f848bed
2 changed files with 13 additions and 12 deletions
|
@ -12,6 +12,7 @@ The format is based on [[https://keepachangelog.com/en/1.0.0/][Keep a Changelog]
|
||||||
|
|
||||||
- Log copy and delete operations (#119)
|
- Log copy and delete operations (#119)
|
||||||
- Log errors with red label (#119)
|
- Log errors with red label (#119)
|
||||||
|
- Ensure file handles are always closed (#122)
|
||||||
|
|
||||||
** Changed
|
** Changed
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import java.nio.file.Path
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
|
|
||||||
import net.kemitix.thorp.domain.MD5Hash
|
import net.kemitix.thorp.domain.MD5Hash
|
||||||
import zio.Task
|
import zio.{Task, UIO, ZManaged}
|
||||||
|
|
||||||
import scala.collection.immutable.NumericRange
|
import scala.collection.immutable.NumericRange
|
||||||
|
|
||||||
|
@ -47,22 +47,22 @@ object MD5HashGenerator {
|
||||||
offset: Long,
|
offset: Long,
|
||||||
endOffset: Long
|
endOffset: Long
|
||||||
) =
|
) =
|
||||||
for {
|
openFile(file, offset)
|
||||||
fis <- openFile(file, offset)
|
.use(digestFile(_, offset, endOffset))
|
||||||
digest <- digestFile(fis, offset, endOffset)
|
|
||||||
_ <- closeFile(fis)
|
|
||||||
} yield digest
|
|
||||||
|
|
||||||
private def openFile(
|
private def openFile(
|
||||||
file: File,
|
file: File,
|
||||||
offset: Long
|
offset: Long
|
||||||
) = Task {
|
) =
|
||||||
val stream = new FileInputStream(file)
|
ZManaged.make {
|
||||||
stream skip offset
|
Task {
|
||||||
stream
|
val stream = new FileInputStream(file)
|
||||||
}
|
stream skip offset
|
||||||
|
stream
|
||||||
|
}
|
||||||
|
}(closeFile)
|
||||||
|
|
||||||
private def closeFile(fis: FileInputStream) = Task(fis.close())
|
private def closeFile(fis: FileInputStream) = UIO(fis.close())
|
||||||
|
|
||||||
private def digestFile(
|
private def digestFile(
|
||||||
fis: FileInputStream,
|
fis: FileInputStream,
|
||||||
|
|
Loading…
Reference in a new issue