File Locking And Block Execution
Examples using file locks and executing multiple operations within the context of a single open file
Lock
Perform an actions within a file lock
import scalax.file.{FileOps, Path}
val file: FileOps = Path ("file")
implicit val codec = scalax.io.Codec.UTF8
// By default the entire file is locked with exclusive access
val result: Option[String] = file.withLock() { s =>
s.slurpString
}
// if the filesystem does not support locking then None will be returned
result match {
case None => file.slurpString // oh well this is the best I can do
case Some(data) => data
}
def fail: Nothing = throw new AssertionError("Uh oh")
// or perhaps we only lock part of the file
val result2: Traversable[Byte] = file.withLock(10, 20) { s =>
s.bytes slice (10,20)
} getOrElse {fail}