title: How to extract used byte array from ByteBuffer?
body: <p>The <code>java.nio.ByteBuffer</code> class has a <code>ByteBuffer.array()</code> method, however this returns an array that is the size of the buffer's capacity, and not the used capacity. Due to this, I'm having some issues.</p>
  
  <p>I have a ByteBuffer which I am allocating as some size and then I am inserting data to it.</p>
  
  <pre><code>ByteBuffer oldBuffer = ByteBuffer.allocate(SIZE);
  addHeader(oldBuffer, pendingItems);
  newBuffer.flip();
  oldBuffer.put(newBuffer);
  // as of now I am sending everything from oldBuffer
  send(address, oldBuffer.array());
  </code></pre>
  
  <p>How can I just send what is being used in <code>oldBuffer</code>. Is there any one liner to do this?</p>