Java Kryo 反序列化失败并显示“KryoException:缓冲区下溢”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24484022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:29:23  来源:igfitidea点击:

Kryo Deserialization fails with "KryoException: Buffer underflow"

javaserializationiokryo

提问by Débora

I use Kryo to write Objects into byte arrays. It works fine. But when the byte arrays are converted into the Objects, it throws, com.esotericsoftware.kryo.KryoException: Buffer underflow.exception.

我使用 Kryo 将对象写入字节数组。它工作正常。但是当字节数组转换为对象时,它会抛出com.esotericsoftware.kryo.KryoException: Buffer underflow.异常。

This is my deserialization:

这是我的反序列化:

        Kryo k=new Kryo();
        Input input=new Input(byteArrayOfObject);           
        Object o=k.readObject(input,ObjectClass.class);

Furthermore, always the object type cannot be defined in my application. At the final process, the class conversion happens. Therefore,

此外,始终无法在我的应用程序中定义对象类型。在最后的过程中,发生类转换。所以,

  • How can I solve above deserialization error

  • Is there a way to create Object without giving the class into readObject(...,ClassName) ?

  • 如何解决上述反序列化错误

  • 有没有办法在不将类放入 readObject(...,ClassName) 的情况下创建 Object ?

回答by sydraz

This happened to me when I was not correctly closing the Output / Input types. You need to make sure Kryo flushes everything but doing output.flush()or output.close().

当我没有正确关闭输出/输入类型时,这发生在我身上。您需要确保 Kryo 刷新除执行output.flush()或 之外的所有内容output.close()

Second, look into kryo.writeClassAndObject(). You can then do kryo.readClassAndObject()and cast your object to the type it is supposed to be.

其次,查看kryo.writeClassAndObject(). 然后kryo.readClassAndObject(),您可以执行并将您的对象转换为它应该是的类型。

回答by Henry Hu

This happend to me when I used the serializer in multiple threads. It's not thread safe, so if you use it in that way, it may give you "Buffer underflow" or other exceptions.

当我在多个线程中使用序列化程序时,这发生在我身上。它不是线程安全的,所以如果你以这种方式使用它,它可能会给你“缓冲区下溢”或其他异常。

回答by cwash

I saw this error because of a true Java serialization issue; my class definition was not the same on the producer and consumer side (two different applications) and as a result I got this exception.

我看到这个错误是因为真正的 Java 序列化问题;我的类定义在生产者和消费者端(两个不同的应用程序)是不同的,因此我得到了这个异常。

Just wanted to leave this as an FYI if you hadn't checked that yet.

如果你还没有检查过,只是想把它留作仅供参考。