title: java : Returning stream or resulting bytes
body: <p>I have need to return the byte array for the <code>ByteArrayOutputStream</code> from the called method.  I see two ways to achieve the same thing: firstly to return <code>ByteArrayOutputStream</code> &amp; use <code>toByteArray()</code> method, and secondly use <code>baos.toByteArray()</code> and return the byte array.</p>
  
  <p>Which one should I use?</p>
  
  <p>To illustrate by example:</p>
  
  <p><strong>Method 1</strong></p>
  
  <pre><code>void parentMethod(){
          bytes [] result = process();
      }
      void byte[] process(){
          ByteArrayOutputStream baos;
          .....
          .....
          .....
          baos.toByteArray();
      }
  </code></pre>
  
  <p><strong>Method 2</strong></p>
  
  <pre><code>void parentMethod(){
          ByteArrayOutputStream baos = process();     
      }
      void ByteArrayOutputStream process(){
          ByteArrayOutputStream baos;
          .....
          .....
          .....
          return baos;
      }
  </code></pre>