Skip to content

Instantly share code, notes, and snippets.

@zipcode
Created February 21, 2014 19:57
Show Gist options
  • Save zipcode/9142184 to your computer and use it in GitHub Desktop.
Save zipcode/9142184 to your computer and use it in GitHub Desktop.
scala> implicit class InputStreamAsString(is: InputStream) {
| def asString = {
| val buff = new Array[Byte](256)
| new String(Iterator.continually({ val c = is.read(buff); buff.take(c)}).takeWhile(_.size>0).flatten.toArray)
| }
| }
defined class InputStreamAsString
scala> var s = new ByteArrayInputStream("asdfasdfasdfasdfasdf".getBytes("UTF-8"))
s: java.io.ByteArrayInputStream = java.io.ByteArrayInputStream@72c454a
scala> s.asString
res32: String = asdfasdfasdfasdfasdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment