title: What is a thread-safe ByteArrayOutputStream?
body: <p>I'd like to read from a process's output and error streams and merge them into one stream of text. My program is in groovy and reads like this:</p>
  
  <pre><code>def mergeStream = new ByteArrayOutputStream()
  process.waitForProcessOutput(mergeStream, mergeStream)
  </code></pre>
  
  <p>The problem is that ByteArrayOutputStream isn't thread safe and waitForProcessOutput() generates two threads which append to mergeStream. Is there a thread-safe variant that I can use? How else do you recommend that I control access to mergeStream? It looks like in practice characters are sometimes dropped with this implementation.</p>