title: How to avoid BufferUnderflowException
body: <p>I'm using this function to convert bytearray to int. </p>
  
  <pre><code>public static int byteArrayToInt(byte[] b) {
      ByteBuffer bb = ByteBuffer.wrap(b);
      bb.order(ByteOrder.LITTLE_ENDIAN);
      return bb.getInt();
  }
  </code></pre>
  
  <p>Unfortunately I'm getting  java.nio.BufferUnderflowException at the return statement. I'm calling this function many times by the way. I read that it happens when more bytes are being read than the buffer size. But can anyone tell me how to avoid it?</p>
  
  <p>Is there a way to copy my original bytearray to new bytearray of size 4 and pass this bytearray to the function?</p>
  
  <p>UPDATE:
  I tried this in  main method: </p>
  
  <pre><code>byte[] bbTemp=new byte[4];
  bbTemp = originalbyteArray;
  </code></pre>
  
  <p>And passed bbTemp to the above function, but it doesn't work. Any idea why?</p>