Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do bytebuffer give buffer overflow exception when buffer is not full

I'm not sure why the following example gives buffer overflow exception. Hope someone can explain why, and how i can do it correctly.

It's as simple as this:

ByteBuffer bf = ByteBuffer.allocate(4);
bf.order(ByteOrder.BIG_ENDIAN);
bf.putInt(8);
bf.putInt(7); // Throws exception

The goal: [0,0,8,7]

Thanks in advance!

like image 487
Ikky Avatar asked Feb 12 '23 23:02

Ikky


1 Answers

An int is 4 bytes long so you should multiply 4 to the number of int you need to store in your ByteBuffer.

like image 85
Sanjeev Avatar answered Feb 15 '23 13:02

Sanjeev