title: Why do bytebuffer give buffer overflow exception when buffer is not full
body: <p>I'm not sure why the following example gives buffer overflow exception. Hope someone can explain why, and how i can do it correctly.</p>
  
  <p>It's as simple as this:</p>
  
  <pre><code>ByteBuffer bf = ByteBuffer.allocate(4);
  bf.order(ByteOrder.BIG_ENDIAN);
  bf.putInt(8);
  bf.putInt(7); // Throws exception
  </code></pre>
  
  <p>The goal: [0,0,8,7]</p>
  
  <p>Thanks in advance!</p>