Ok, I'm pretty new to Java, so I apologize if this question is stupid.
I have a ByteBuffer object that contains a value that can be any number of bytes in length, with a buffer size set to length. What is the most efficient way to read a value in a buffer into the corresponding primitive type?
The code below is representative of my question.
long returnValue;
ByteBuffer bb = GetBuffer(blah);
if (bb.capacity() > 4)
{
returnValue = (long) <how to get value from the buffer here?>
}
else if (bb.capacity() > 2)
{
returnValue = (long) <and here...>
}
Calling getLong () in the buffer throws an exception if the buffer limit is less than 8. Suppose I could build a long one from individual bytes, but this seems unnecessarily complicated. Is there a better way?
Thank you so much!
source
share