title: ByteArrayOutputStream.toByteArray() or read from stream?
body: <p>In my Android app I have data stored in <code>ByteArrayOutputStream</code> (for now it's about 1 MB maximum) and I want to pass it to <code>DataOutputStream</code>. </p>
  
  <p>The most obvious way is of course calling:</p>
  
  <pre><code>dataOS.write(byteArrayOS.toByteArray())
  </code></pre>
  
  <p>but on there is also a more complicated (but maybe more efficient) way of doing:</p>
  
  <pre><code>ByteArrayInputStream rdr = new ByteArrayInputStream(byteArrayOS.toByteArray());
  int read;
  byte[] buffer = new byte[1024];
  while ((read = rdr.read(buffer)) &gt; 0){
      dataOS.write(buffer,0,read);
  }
  </code></pre>
  
  <p>In future the amount of data stored in <code>ByteArrayOutputStream</code> might grow. Which one of these two is more efficient? Writing a big chunk of data at once or writing it sequentially?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The <code>DataOutputStream</code> is used for writing over network (it's created over <code>UrlConnection.getOutputStream()</code> ).</p>