title: Get an OutputStream into a String
body: <p>What's the best way to pipe the output from an java.io.OutputStream to a String in Java?</p>
  
  <p>Say I have the method:</p>
  
  <pre><code>  writeToStream(Object o, OutputStream out)
  </code></pre>
  
  <p>Which writes certain data from the object to the given stream. However, I want to get this output into a String as easily as possible.</p>
  
  <p>I'm considering writing a class like this (untested):</p>
  
  <pre><code>class StringOutputStream extends OutputStream {
  
    StringBuilder mBuf;
  
    public void write(int byte) throws IOException {
      mBuf.append((char) byte);
    }
  
    public String getString() {
      return mBuf.toString();
    }
  }
  </code></pre>
  
  <p>But is there a better way? I only want to run a test!</p>