Skip to content

Commit

Permalink
Fix #9: File.resource APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
pathikrit committed Feb 21, 2017
1 parent 939a7bd commit 4bf31c7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## v3.0.0

* [Issue #9](https://github.com/pathikrit/better-files/issues/9): File resource utils
* [Issue #114](https://github.com/pathikrit/better-files/issues/114): Glob with automatic path
* [Issue #107](https://github.com/pathikrit/better-files/issues/107): Handle Byte-order markers
* [PR #113](https://github.com/pathikrit/better-files/pull/113): File anchor util
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ File.usingTempFile() {tempFile =>
File.newTempFile().applyAndDelete(tempFile => ...)
```

You can also load resources from your classpath using `File.resource` or `File.copyResource`.

### UNIX DSL
All the above can also be expressed using [methods](http://pathikrit.github.io/better-files/latest/api/better/files/Dsl$.html) reminiscent of the command line:
```scala
Expand Down
21 changes: 14 additions & 7 deletions core/src/main/scala/better/files/File.scala
Original file line number Diff line number Diff line change
Expand Up @@ -870,20 +870,27 @@ object File {
implicit val defaultCharset: Charset =
UnicodeCharset(Charset.defaultCharset())

/**
* Get a file from a resource
* Note: Use resourceToFile instead as this may not actually always load the file
* See: http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream
*
* @param name
* @return
*/
def resource(name: String): File =
File(Thread.currentThread().getContextClassLoader.getResource(name))
File(currentClassLoader().getResource(name))

/**
* Copies a resource into a tempFile
* Copies a resource into a file
*
* @param name
* @param out File where resource is copied into, if not specified a temp file is created
* @return
*/
def resourceAsTempFile(name: String): File = {
val in = getClass.getResourceAsStream(name)
val file = File.newTemporaryFile(prefix = name)
in.pipeTo(file.newOutputStream)
file
def copyResource(name: String)(out: File = File.newTemporaryFile(prefix = name)): out.type = {
resourceAsStream(name) > out.newOutputStream
out
}

def newTemporaryDirectory(prefix: String = "", parent: Option[File] = None)(implicit attributes: Attributes = Attributes.default): File = {
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/better/files/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package better

import java.io.InputStream

import scala.collection.mutable
import scala.util.{Failure, Success, Try}

Expand All @@ -17,6 +19,8 @@ package object files extends Implicits {
def close(): Unit
}

def resourceAsStream(name: String): InputStream = currentClassLoader().getResourceAsStream(name)

type ManagedResource[A <: Closeable] = Traversable[A]

// Some utils:
Expand All @@ -26,6 +30,8 @@ package object files extends Implicits {

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

private[files] def currentClassLoader() = Thread.currentThread().getContextClassLoader

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 {
Expand Down

0 comments on commit 4bf31c7

Please sign in to comment.