Question

I try to use the java libary ByteBuffer and wrote following code example:

    ByteBuffer buf = ByteBuffer.allocate(32);
    buf.putInt(4);
    buf.putInt(8);
    buf.putInt(12);
    buf.putInt(16);
    buf.putInt(20);
    buf.putInt(24);
    buf.putInt(28);
    buf.putInt(32);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    byte[] temp = new byte[32];
    buf.get(temp);

For some reason it throws a BufferUnderflowException in the last line.

I don't know why, can somebody explain me what I am doing wrong?

Was it helpful?

Solution

As described in java docs

Relative get method. ...

Throws: BufferUnderflowException If the buffer's current position is not smaller than its limit

Find more here

OTHER TIPS

Take a look at http://mindprod.com/jgloss/bytebuffer.html

You must call ByteBuffer.flip to convert from filling the buffer via physical I/O to emptying it via ByteBuffer.get

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top