title: How to extend the allocated memory of a bytebuffer
body: <p>I have a bytebuffer and I put Ints,Chars etc..
  Because I do not know how much space I need I would like to dynamically growth the bytebuffer. How can this be done ?</p>
  
  <p>Example :
  - I have a Bytebuffer of 2 bytes
  - I add a character to the bytebuffer (bytebuffer is full now )
  - I like to add a Integer to the bytebuffer by extending the bytebuffer with 4 bytes. I can't allocate my bytebuffer with 6 bytes at start.</p>
  
  <pre><code>    ByteBuffer byteBuffer = ByteBuffer.allocate(2);
  
      byteBuffer.putChar('a');
      byteBuffer.putInt(1);
  </code></pre>
  
  <hr>
  
  <p>I am very impressed, on how many people are working on my question that is some minutes old. Thank you all very much and thanks also to Stackoverflow, which is a great Plattform !</p>
  
  <p>All of you asked, what I am doing. So I try to explain it here.
  My usecase:
  I have structured data represented as javaobjects (javaclasses) which I would like to store and read to/from a db. Reading should be very fast.
  What I have done so far:</p>
  
  <ul>
  <li><p>java serialized and deserialized and stored it in a blob -> works well but too slow.</p></li>
  <li><p>tried several 3rd party serializer like kryo (which is very good), but not useable in my 
  case (android).</p></li>
  </ul>
  
  <p>my new strategy :-) :
  I do my own externalizing of my class. for that I would like to construct my entire data of my class sequential as an array of bytes. This can be slow. Then I store the bytearray to a blob in my db. When reading I would like to read the bytearray at once (bytearray is about 10k). (I will have a lot of them). Then parsing the bytearray to extract the structured data. </p>
  
  <p>I thought using a bytebuffer is ideal for doing this, because of methods like putX and readX ? (X for chars, floats, int)</p>