3

Okay, I'm pretty new to Java, so I apologize if this question is silly.

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?

The code below is representative of the question I have.

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...>
}
// etc...

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?

Thanks much!

2 Answers 2

2

ByteBuffer has some special "get" methods, like getLong(int index):

if (bb.capacity() >= 8)
{
    returnValue = bb.getLong();
}
Sign up to request clarification or add additional context in comments.

3 Comments

"Calling getLong() on the buffer results in an exception if the limit of the buffer is less than 8" (that is, I suppose he already know about getLong)
@aioobe - ah, yes, your right. Didn't pay attention and just copied the the code from the question.
Yeah. easy to do. I think the question was on how to create a long out of "too few bytes" though since he has if (bb.capacity() > 2) returnValue = (long) <and here...>. (I'm not the down-voter though!!)
1

Calling getLong() on the buffer results in an exception if the limit of the buffer is less than 8.

This is because a long is 8 bytes. See Primitive Data Types.

If you want to create a long out of an arbitrary number of bytes, I suggest that you simply pad it by inserting zeros and use getLong(). (See example below.)

If you want to create a long out of precisely 4 bytes, you could do something like (long) bb.getInt().

Finally, unless you use ByteBuffer.remaining() instead of ByteBuffer.capacity() I suggest you use the absolute get method for long: ByteBuffer.getLong(0).

I suppose I could construct a long from the individual bytes, but that seems unnecessarily complicated. Is there a better way?

Yes, there is a better way. Here is an example program that should get you started:

import java.nio.ByteBuffer;

public class Main {

    static ByteBuffer longBuf = ByteBuffer.allocate(8);

    public static long getLong(ByteBuffer bb) {
        // Fill with eight 0-bytes and set position.
        longBuf.putLong(0, 0).position(8 - bb.remaining());

        // Put the remaining bytes from bb, and get the resulting long.
        return longBuf.put(bb).getLong(0);
    }

    public static void main(String[] args) {

        ByteBuffer bb = ByteBuffer.allocate(10);

        // Add 2 bytes
        bb.put((byte) 5);
        bb.put((byte) 7);

        // Prepare to read
        bb.flip();

        long l = getLong(bb);
        System.out.println(Long.toBinaryString(l)); // Prints 10100000111

        // Correct since, 00000101 00000111
        //               |--------|--------|
        //                       5        7
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.