In Java, how do you convert a byte array into a particular primitive type, depending on the size of the array?
<p>Okay, I'm pretty new to Java, so I apologize if this question is silly.  </p>

<p>I have a ByteBuffer object that contains a value that might be any number of bytes long, with the capacity of the buffer set to the length.  What's the most efficient way to read the value in the buffer into the appropriate primitive type?  </p>

<p>The code below is representative of the question I have.</p>

<pre><code>long returnValue;
ByteBuffer bb = GetBuffer(blah);

if (bb.capacity() &gt; 4)
{
    returnValue = (long) &lt;how to get value from the buffer here?&gt;
}
else if (bb.capacity() &gt; 2)
{
    returnValue = (long) &lt;and here...&gt;
}
// etc...
</code></pre>

<p>Calling getLong() on the buffer results in an exception if the limit of the buffer is less than 8.  I suppose I could construct a long from the individual bytes, but that seems unnecessarily complicated.  Is there a better way?</p>

<p>Thanks much!</p>
