Created
November 27, 2014 01:31
-
-
Save edvakf/f51fe67692bf86961bc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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