From the docs of java.nio.ByteBuffer:
Throws: BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer
I think that makes it pretty clear where this is Exception is coming from. In order to fix it, you need to make sure that the ByteBuffer has enough data in it in order to read a double (8 bytes) by checking remaining()
instead of hasRemaining()
which only checks for one byte:
while (mappedByteBufferOut.remaining() >= 36) {//36 = 4 * 8(double) + 1 * 4(int)