java.nio.BufferUnderflowException while converting byte array to double

Sushmita Bhattacharya picture Sushmita Bhattacharya · Sep 9, 2014 · Viewed 34.9k times · Source

I need to convert a bytearray to double. I am using

double dvalue = ByteBuffer.wrap(value).getDouble();

But at the runtime I am getting BufferUnderflowException exception

Exception in thread "main" java.nio.BufferUnderflowException
    at java.nio.Buffer.nextGetIndex(Buffer.java:498)
    at java.nio.HeapByteBuffer.getDouble(HeapByteBuffer.java:508)
    at Myclass.main(Myclass.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:212)

What do I need to change here?

Answer

Sotirios Delimanolis picture Sotirios Delimanolis · Sep 9, 2014

ByteBuffer#getDouble() throws

 BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer

So value must contain less than 8 bytes. A double is a 64 bit, 8 bytes, data type.