Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Created March 13, 2015 07:28
Show Gist options
  • Save wookietreiber/ed439fd7958b6d67eeb4 to your computer and use it in GitHub Desktop.
Save wookietreiber/ed439fd7958b6d67eeb4 to your computer and use it in GitHub Desktop.
Scala StringInputStream: have a String and read from it with an InputStream
import java.io.InputStream
class StringInputStream(s: String) extends InputStream {
private val bytes = s.getBytes
private var pos = 0
override def read(): Int = if (pos >= bytes.length) {
-1
} else {
val r = bytes(pos)
pos += 1
r.toInt
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment