diff --git a/README.md b/README.md index 44c56004..e7888a83 100644 --- a/README.md +++ b/README.md @@ -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]) @@ -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). @@ -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]: http://pathikrit.github.io/better-files/latest/api/better/files/File.html diff --git a/core/src/main/scala/better/files/File.scala b/core/src/main/scala/better/files/File.scala index d537bcfb..57ed649c 100644 --- a/core/src/main/scala/better/files/File.scala +++ b/core/src/main/scala/better/files/File.scala @@ -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) diff --git a/core/src/main/scala/better/files/package.scala b/core/src/main/scala/better/files/package.scala index 83a6d627..c7f3b083 100644 --- a/core/src/main/scala/better/files/package.scala +++ b/core/src/main/scala/better/files/package.scala @@ -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