Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.nio.BufferUnderflowException while converting byte array to double

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?

like image 647
Sushmita Bhattacharya Avatar asked Sep 09 '14 04:09

Sushmita Bhattacharya


People also ask

What is buffer underflow exception in Java?

public class BufferUnderflowException extends RuntimeException. Unchecked exception thrown when a relative get operation reaches the source buffer's limit.

What is ByteBuffer in Java?

ByteBuffer holds a sequence of integer values to be used in an I/O operation. The ByteBuffer class provides the following four categories of operations upon long buffers: Absolute and relative get method that read single bytes. Absolute and relative put methods that write single bytes.

What is the byte order of ByteBuffer?

By default, the order of a ByteBuffer object is BIG_ENDIAN. If a byte order is passed as a parameter to the order method, it modifies the byte order of the buffer and returns the buffer itself. The new byte order may be either LITTLE_ENDIAN or BIG_ENDIAN.


1 Answers

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.

like image 60
Sotirios Delimanolis Avatar answered Oct 13 '22 00:10

Sotirios Delimanolis