title: Java/Kotlin way to redirect command output to both stdout and String
body: <p>I am translating code from a NodeJS server to a Kotlin Ktor server.</p>
  <p>The NodeJS code splits the output between a String to be handled by code, and realtime server logging :</p>
  <pre><code>const shellScript = exec(&quot;./myScript.sh&quot;,
    (error, stdout, stderr) =&gt; {
     // this happens when the script ends
     // stdout and stderr contain full script output
      if (error === null) {
        ...
      }
    });
  
  // Realtime server logging
  shellScript.stdout.on('data', (data) =&gt; { console.log(data); });
  shellScript.stderr.on('data', (data) =&gt; { console.error(data); });
  </code></pre>
  <p>According to my understanding of <code>java.lang.ProcessBuilder</code>, we would need to spawn a thread (or a coroutine) to poll inputStream and errorStream in a loop and accumulate them in a volatile field.</p>
  <p>Is there a cleaner way to do this?</p>