Skip to content

Instantly share code, notes, and snippets.

@mpkocher
Forked from mprihoda/nuprocess.sc
Last active May 26, 2020 04:39
Show Gist options
  • Save mpkocher/7f7ca44516bb6cf42353283e04db93b6 to your computer and use it in GitHub Desktop.
Save mpkocher/7f7ca44516bb6cf42353283e04db93b6 to your computer and use it in GitHub Desktop.
import $ivy.`com.zaxxer:nuprocess:2.0.0`, com.zaxxer.nuprocess._
import scala.collection.JavaConverters._
import java.nio.ByteBuffer
import scala.concurrent.{Future, Promise}
class ProcessHandler extends NuAbstractProcessHandler {
private var nuProcess: NuProcess = null
override def onStart(nuProcess: NuProcess): Unit = {
this.nuProcess = nuProcess
}
override def onStdout(buffer: ByteBuffer, closed: Boolean): Unit = {
if (!closed) {
val bytes = new Array[Byte](buffer.remaining())
buffer.get(bytes);
Console.println(new String(bytes));
nuProcess.closeStdin(true);
}
}
}
val pb = new NuProcessBuilder(List("/bin/cat").asJava)
pb.setProcessListener(new ProcessHandler)
val process = pb.start()
process.writeStdin(ByteBuffer.wrap("Hello World!".getBytes("UTF-8")))
process.waitFor(0, java.util.concurrent.TimeUnit.SECONDS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment