Created
November 17, 2011 06:45
-
-
Save takezoe/1372537 to your computer and use it in GitHub Desktop.
read bytes from java.io.InputStream and process them by closure
This file contains hidden or 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
processInputStream(new java.io.FileInputStream("test.jpg"), 8 * 1024){ bytes => | |
... | |
} | |
/** | |
* Invokes the given function for each read bytes with specified size from java.io.InputStream. | |
*/ | |
@tailrec | |
def processInputStream(in: java.io.InputStream, bufferSize: Int)(func: Array[Byte] => Unit): Unit = { | |
val buffer = new Array[Byte](bufferSize) | |
in.read(buffer) match { | |
case -1 => | |
case len => func(buffer.slice(0, len)); processInputStream(in, bufferSize)(func) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment