【问题标题】:java.nio.BufferUnderflowException while converting byte array to doublejava.nio.BufferUnderflowException 将字节数组转换为双精度
【发布时间】:2014-11-02 09:54:17
【问题描述】:

我需要将一个字节数组转换为双精度。我正在使用

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

但在运行时我得到 BufferUnderflowException 异常

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)

我需要在这里更改什么?

【问题讨论】:

  • 你能告诉我们价值声明吗?用一些代码。

标签: java exception double bytearray bytebuffer


【解决方案1】:

ByteBuffer#getDouble() 抛出

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

所以value 必须包含少于 8 个字节。 double 是 64 位、8 字节的数据类型。

【讨论】:

    【解决方案2】:

    你的代码是这样的:

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

    如果是,那么它应该可以工作。

    并向我们展示您的 value 数组数据。

    来自神谕docs

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

    为了修复它,您需要确保ByteBuffer 中有足够的数据以读取双精度(8 bytes)

    看看Here 是一个简单的代码,可以显示您想要的输入数据和输出。

    【讨论】:

    • 我正在从 HBase 表中获取行 byte [] row1 = Bytes.toBytes(rowid);获取 g = new Get(row1);结果 result = table.get(g); byte[] value = result.getValue(Bytes.toBytes("columnfamily"), Bytes.toBytes("columnname"));但是当打印 System.out.print(value.length);我得到 4
    猜你喜欢
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 2012-03-07
    • 2015-10-24
    • 2011-07-07
    • 2015-09-13
    相关资源
    最近更新 更多