1

I'm trying to implement a function that starts a process and returns its stdout as InputStream.

def getStuff(): InputStream = ???

Seems pretty easy to do in Java but I can't figure out how to do it using sys.process in Scala.

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
synapse
  • 5,588
  • 6
  • 35
  • 65
  • Maybe this question/answers can help you? https://stackoverflow.com/questions/15411728/scala-process-capture-standard-out-and-exit-code – GamingFelix Sep 30 '19 at 12:42

1 Answers1

4

You can pipe the output of a command to OutputSteam with #>. Then you just need to copy OutputStream to InputStream:

import scala.sys.process._
import java.io._
import scala.io.Source

def getStuff(): InputStream = {
  val os   =  new ByteArrayOutputStream
  ("echo 'Hello'" #> os).!
  new ByteArrayInputStream(os.toByteArray());
}

Source.fromInputStream(getStuff()).mkString //"Hello"
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76