Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created November 27, 2014 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvakf/f51fe67692bf86961bc1 to your computer and use it in GitHub Desktop.
Save edvakf/f51fe67692bf86961bc1 to your computer and use it in GitHub Desktop.
object ProcessEnumerator {
// wraps a ProcessBuilder with Play's Enumerator
// and executes the process in Future
// so that the process' output can be streamed
def apply(process: ProcessBuilder): Enumerator[String] = {
val in = new PipedInputStream()
val out = new PipedOutputStream(in)
Future(process.#>(out).run())
val reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
Enumerator.fromCallback1[String](_ => Future {
reader.readLine match {
case line: String => Some(line + "\n")
case _ => None
}
}, { () => // oncomplete
in.close()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment