java.nio.BufferUnderflowException while converting byte array to double

39,278

Solution 1

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.

Solution 2

Your code be something like this :

byte [] value = { // values };
double dvalue = ByteBuffer.wrap(value).getDouble();

If it is then it should work.

And show us your data of value array.

from the oracle docs :

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

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).

Look Here's a simple code to show what you want with input data and output.

Share:
39,278

Related videos on Youtube

Sushmita Bhattacharya
Author by

Sushmita Bhattacharya

pursuing M.Tech at IIT Bombay.

Updated on September 09, 2020

Comments

  • Sushmita Bhattacharya
    Sushmita Bhattacharya over 3 years

    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?

    • user3145373 ツ
      user3145373 ツ over 9 years
      can you show us value declaration ? with some code.
  • Sushmita Bhattacharya
    Sushmita Bhattacharya over 9 years
    I am fetching row from a HBase Table byte [] row1 = Bytes.toBytes(rowid); Get g = new Get(row1); Result result = table.get(g); byte[] value = result.getValue(Bytes.toBytes("columnfamily"), Bytes.toBytes("columnname")); But when printing System.out.print(value.length); I am getting 4