Skip to content

Instantly share code, notes, and snippets.

@machisuji
Created February 19, 2012 16:22
Show Gist options
  • Save machisuji/1864504 to your computer and use it in GitHub Desktop.
Save machisuji/1864504 to your computer and use it in GitHub Desktop.
val in: InputStream
val buffer: Array[Byte]
val stream: Stream[Array[Byte]] =
Stream.continually(in.read(buffer)).takeWhile(-1 !=).map(buffer take)
val iterator: Iterator[Array[Byte]] =
Iterator.continually(in.read(buffer)).takeWhile(-1 !=).map(buffer take)
stream.foreach(println) // the byte arrays stay
iterator.foreach(println) // the byte arrays go (some time)
// run on forever
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment