Kryo Deserialization fails with “KryoException: Buffer underflow”

て烟熏妆下的殇ゞ 提交于 2019-12-04 17:11:31

问题


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.

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


回答1:


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

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




回答2:


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.

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




回答3:


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.



来源:https://stackoverflow.com/questions/24484022/kryo-deserialization-fails-with-kryoexception-buffer-underflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!