Skip to content

Commit

Permalink
Fix #21: Add file.usingLock util
Browse files Browse the repository at this point in the history
  • Loading branch information
pathikrit committed Feb 2, 2017
1 parent febffb3 commit 6f95055
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# better-files [![License][licenseImg]][licenseLink] [![CircleCI][circleCiImg]][circleCiLink] [![Codacy][codacyImg]][codacyLink] [![Gitter][gitterImg]][gitterLink]
# better-files [![License][licenseImg]][licenseLink] [![CircleCI][circleCiImg]][circleCiLink] [![Codacy][codacyImg]][codacyLink]

`better-files` is a [dependency-free](build.sbt) *pragmatic* [thin Scala wrapper](core/src/main/scala/better/files/File.scala) around [Java NIO](https://docs.oracle.com/javase/tutorial/essential/io/fileio.html).

## Talks
## Talks [![Gitter][gitterImg]][gitterLink]
- [ScalaDays NYC 2016][scalaDaysNyc2016Event] ([slides][scalaDaysNyc2016Slides])

<a href="http://www.youtube.com/watch?feature=player_embedded&v=uaYKkpqs6CE" target="_blank">
Expand Down Expand Up @@ -40,7 +40,7 @@ libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.16"
)
```
Latest `version`: [![Maven][mavenImg]][mavenLink]
Latest `version`: [![Maven][mavenImg]][mavenLink] [![Scaladex][scaladexImg]][scaladexLink]

Although this library is currently only actively developed for Scala 2.12,
you can find reasonably recent versions of this library for Scala 2.10 and 2.11 [here](https://oss.sonatype.org/#nexus-search;quick~better-files).
Expand Down Expand Up @@ -78,6 +78,9 @@ you can find reasonably recent versions of this library for Scala 2.10 and 2.11
[gitterImg2]: https://badges.gitter.im/Join%20Chat.svg
[gitterLink]: https://gitter.im/pathikrit/better-files

[scaladexImg]: https://index.scala-lang.org/pathikrit/better-files/better-files/latest.svg
[scaladexLink]: https://index.scala-lang.org/pathikrit/better-files

[scaladocImg]: https://www.javadoc.io/badge/com.github.pathikrit/better-files_2.12.svg?color=blue&label=scaladocs
<!--[scaladocLink]: https://www.javadoc.io/page/com.github.pathikrit/better-files_2.12/latest/better/files/File.html-->
[scaladocLink]: http://pathikrit.github.io/better-files/latest/api/better/files/File.html
Expand Down
15 changes: 8 additions & 7 deletions core/src/main/scala/better/files/File.scala
Original file line number Diff line number Diff line change
Expand Up @@ -434,17 +434,18 @@ class File private(val path: Path) {
def isHidden: Boolean =
Files.isHidden(path)

def isLocked(mode: File.RandomAccessMode, position: Long = 0L, size: Long = Long.MaxValue, isShared: Boolean = false): Boolean = {
val channel = newRandomAccess(mode).getChannel
def isLocked(mode: File.RandomAccessMode, position: Long = 0L, size: Long = Long.MaxValue, isShared: Boolean = false): Boolean =
try {
channel.tryLock(position, size, isShared).release()
false
usingLock(mode) {channel =>
channel.tryLock(position, size, isShared).release()
false
}
} catch {
case _: OverlappingFileLockException | _: NonWritableChannelException | _: NonReadableChannelException => true
} finally {
channel.close()
}
}

def usingLock[U](mode: File.RandomAccessMode)(f: FileChannel => U): U =
using(newRandomAccess(mode).getChannel)(f)

def isReadLocked(position: Long = 0L, size: Long = Long.MaxValue, isShared: Boolean = false) =
isLocked(File.RandomAccessMode.read, position, size, isShared)
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/better/files/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ package object files extends Implicits {
private[files] def newMultiMap[A, B]: mutable.MultiMap[A, B] = new mutable.HashMap[A, mutable.Set[B]] with mutable.MultiMap[A, B]

@inline private[files] def when[A](condition: Boolean)(f: => A): Option[A] = if (condition) Some(f) else None

@inline private[files] def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)

private[files] def using[A <: Closeable, U](resource: A)(f: A => U): U = try { f(resource) } finally {resource.close()}

private[files] def produce[A](f: => A) = new {
def till(hasMore: => Boolean): Iterator[A] = new Iterator[A] {
override def hasNext = hasMore
Expand Down

0 comments on commit 6f95055

Please sign in to comment.