diff options
author | Eugene Dzhurinsky <jdevelop@gmail.com> | 2015-06-03 20:59:46 -0400 |
---|---|---|
committer | Eugene Dzhurinsky <jdevelop@gmail.com> | 2015-07-07 12:35:32 -0400 |
commit | 82fd8bc677abeece738fb281f53439661254d981 (patch) | |
tree | 79c6603f108700e6fba502d3bbd5472c0a3b54b2 /src/library | |
parent | 5330319b1eacbc4da2ba4920ceace708a836df3b (diff) | |
download | scala-82fd8bc677abeece738fb281f53439661254d981.tar.gz scala-82fd8bc677abeece738fb281f53439661254d981.tar.bz2 scala-82fd8bc677abeece738fb281f53439661254d981.zip |
[ SI-7514 ] Introduce Source.fromClassPath(resource) method
Creates Source from named classpath resource.
Simplifies
val src = io.Source.fromInputStream(classOf[ClassInst].getResourceAsStream("/name"))
to
val src = io.Source.fromClassPath("/name")
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/io/Source.scala | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/library/scala/io/Source.scala b/src/library/scala/io/Source.scala index e38c197196..c0ee5f6a75 100644 --- a/src/library/scala/io/Source.scala +++ b/src/library/scala/io/Source.scala @@ -167,6 +167,16 @@ object Source { def fromInputStream(is: InputStream)(implicit codec: Codec): BufferedSource = createBufferedSource(is, reset = () => fromInputStream(is)(codec), close = () => is.close())(codec) + + /** Reads data from a classpath resource, using either a context classloader (default) or a passed one. + * + * @param resource name of the resource to load from the classpath + * @param classLoader classloader to be used, or context classloader if not specified + * @return the buffered source + */ + def fromResource(resource: String, classLoader: ClassLoader = Thread.currentThread().getContextClassLoader())(implicit codec: Codec): BufferedSource = + fromInputStream(classLoader.getResourceAsStream(resource)) + } /** An iterable representation of source data. |