-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Io.readBytesFromResources() to read binary data. #33
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ import java.io._ | |
import java.nio.file.{Files, Path} | ||
|
||
import com.fulcrumgenomics.commons.CommonsDef._ | ||
|
||
import scala.collection.mutable.ArrayBuffer | ||
import scala.io.Source | ||
|
||
/** | ||
|
@@ -181,6 +183,13 @@ trait IoUtil { | |
/** Creates an object that will asynchronously read character data from a stream and pipe it into a sink function. */ | ||
def pipeStream(stream: InputStream, sink: String => Unit) : AsyncStreamSink = new AsyncStreamSink(stream, sink) | ||
|
||
/** Private function to get an input stream from a resource, or fail if the resource doesn't exist. */ | ||
private def streamFromResource(name: String): InputStream = { | ||
Seq(getClass.getResourceAsStream _, getClass.getClassLoader.getResourceAsStream _) | ||
.flatMap(m => Option(m(name))) | ||
.headOption.getOrElse(throw new IllegalArgumentException(s"Resource does not exist at path: $name")) | ||
} | ||
|
||
/** Finds a resource with a given name on the classpath and produces an iterator of lines of text from the resource. | ||
* | ||
* The way resources are traditionally loaded from the class path via `Class.getResource()` and | ||
|
@@ -191,17 +200,27 @@ trait IoUtil { | |
* | ||
* The implementation here first tries `Class.getResource` and then `ClassLoader.getResource` to enable it to | ||
* find relative paths and also absolute paths with and without leading `/s`. | ||
* | ||
*/ | ||
def readLinesFromResource(name: String): Iterator[String] = { | ||
Seq(getClass.getResourceAsStream _, getClass.getClassLoader.getResourceAsStream _) | ||
.flatMap(m => Option(m(name))) | ||
.headOption match { | ||
case None => | ||
throw new IllegalArgumentException(s"Resource does not exist at path: $name") | ||
case Some(in) => | ||
val stream = new BufferedInputStream(in, bufferSize) | ||
Source.fromInputStream(stream).withClose(() => stream.safelyClose()).getLines | ||
} | ||
val stream = new BufferedInputStream(streamFromResource(name), bufferSize) | ||
Source.fromInputStream(stream).withClose(() => stream.safelyClose()).getLines | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for this too! |
||
} | ||
|
||
/** Finds a resource with a given name on the classpath and produces an array of the bytes from the resource. | ||
* | ||
* The way resources are traditionally loaded from the class path via `Class.getResource()` and | ||
* `ClassLoader.getResource()` have confusing behaviour. Notably: | ||
* | ||
* * Class.getResource() interprets paths as relative, and requires a leading `/` to make them absolute | ||
* * ClassLoader.getResource() interprets all paths a absolute and fails on leading `/`s | ||
* | ||
* The implementation here first tries `Class.getResource` and then `ClassLoader.getResource` to enable it to | ||
* find relative paths and also absolute paths with and without leading `/s`. | ||
*/ | ||
def readBytesFromResource(name: String): Array[Byte] = { | ||
val stream = new BufferedInputStream(streamFromResource(name), bufferSize) | ||
val array = Iterator.continually(stream.read()).takeWhile(_ != -1).map(_.toByte).toArray | ||
stream.safelyClose() | ||
array | ||
} | ||
} |
Binary file added
BIN
+256 Bytes
src/test/resources/com/fulcrumgenomics/commons/io/to-bytes-from-resource-test.bin
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused import
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh, thanks!