Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.