2014-09-09 20 views
8

Tôi cần phải chuyển đổi một dấu thăng bằng thành kép. Tôi đang sử dụngjava.nio.BufferUnderflowException khi chuyển đổi mảng byte thành double

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

Nhưng tại thời gian chạy Tôi nhận BufferUnderflowException ngoại lệ

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) 

sao tôi cần phải thay đổi ở đây là gì?

+1

bạn có thể cho chúng tôi khai báo giá trị không? với một số mã. –

Trả lời

10

ByteBuffer#getDouble() ném

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

Vì vậy value phải chứa ít hơn 8 byte. A double là kiểu dữ liệu 64 bit, 8 byte.

1

Mã của bạn có một cái gì đó như thế này:

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

Nếu đó là sau đó nó sẽ làm việc.

Và hiển thị cho chúng tôi dữ liệu của bạn về mảng value.

từ oracle docs:

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

Để khắc phục điều đó, bạn cần phải chắc chắn rằng ByteBuffer có đủ dữ liệu trong nó để đọc một đôi (8 bytes).

Hãy xem Here là một mã đơn giản để hiển thị những gì bạn muốn với dữ liệu đầu vào và đầu ra.

+0

Tôi đang tìm nạp hàng từ Bảng HBase byte [] row1 = Bytes.toBytes (rowid); Nhận g = new Get (row1); Kết quả kết quả = table.get (g); byte [] value = result.getValue (Bytes.toBytes ("columnfamily"), Bytes.toBytes ("columnname")); Nhưng khi in System.out.print (value.length); Tôi nhận được 4 –

Các vấn đề liên quan