title: How to use ByteArrayOutputStream and DataOutputStream simultaneously in Java?
body: <p>I'm having quite a problem here, and I think it is because I don't understand very much how I should use the API provided by Java.</p>
  <p>I need to write an <code>int</code> and a <code>byte[]</code> into a <code>byte[]</code>.</p>
  <p>I thought of using a <code>DataOutputStream</code> to solve the data writing with <code>writeInt(int i)</code> and <code>write(byte[] b)</code>, and to be able to put that into a byte array, I should use <code>ByteArrayOutputStream</code> method <code>toByteArray().</code></p>
  <p>I understand that this classes use the Wrapper pattern, so I had two options:</p>
  <pre><code>DataOutputStream w = new DataOutputStream(new ByteArrayOutputStream());
  </code></pre>
  <p>or</p>
  <pre><code>ByteArrayOutputStream w = new ByteArrayOutputStream(new DataOutputStream());
  </code></pre>
  <p>but in both cases, I &quot;loose&quot; a method. in the first case, I can't access the <code>toByteArray()</code> method, and in the second, I can't access the <code>writeInt()</code> method.</p>
  <p>How should I use this classes together?</p>