I have a class as follows:
final ByteBuffer data;
1st_constructor(arg1, arg2, arg3){
data = ByteBuffer.allocate(8);
// Use "put" method to add values to the ByteBuffer
//.... eg: 2 ints
//....
data.flip();
}
1st_constructor(arg1, arg2){
data = ByteBuffer.allocate(12);
// Use "put" method to add values to the ByteBuffer
//.... eg: 3 ints
//....
data.flip()
}
In my main class, I create an instance of this class called "data_packet" and store the contents of the ByteBuffer "data" into a byte[].
data_packet.data.get(buf,0,buf.length);
Subsequently, when I use:
data_packet.data.getInt();
I get a "BufferUnderFlow Exception". However, if I flip the buffer again prior to using getInt(), it works fine.
So my question is, why am I required to flip the buffer again? Isn't it already set to read in the constructor?
Thank you.