2014-09-09 17 views
8

Potrzebuję przekonwertować bytearray na podwójne. UżywamWyjątek java.nio.BufferUnderflowException przy konwersji tablicy bajtów na podwójny

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

Jednak na starcie jestem coraz BufferUnderflowException wyjątek

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) 

Co muszę zmienić tutaj?

+1

możesz pokazać nam deklarację wartości? z jakimś kodem. –

Odpowiedz

10

ByteBuffer#getDouble() rzuca

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

Więc value musi zawierać mniej niż 8 bajtów. A double jest 64-bitowym, 8-bajtowym typem danych.

1

Twój kod będzie coś takiego:

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

Jeśli jest to powinno działać.

Pokaż nam swoje dane z tablicy value.

od wyroczni docs:

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

Aby to naprawić, trzeba się upewnić, że ByteBuffer ma w nim wystarczająco dużo danych, w celu odczytania podwójne (8 bytes).

Wygląda na to, że Here jest prostym kodem, który pokazuje, co chcesz z danymi wejściowymi i danymi wyjściowymi.

+0

Pobieram wiersz z tabeli HBase bajt [] row1 = Bytes.toBytes (rowid); Get g = new Get (row1); Wynik wyniku = table.get (g); bajt [] value = result.getValue (Bytes.toBytes ("columnfamily"), Bytes.toBytes ("columnname")); Ale przy drukowaniu System.out.print (value.length); Otrzymuję 4 –