Kryo Deserialization fails with "KryoException: Buffer underflow"
Asked Answered
S

3

18

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

Stalk answered 30/6, 2014 at 6:23 Comment(1)
I'm facing same issue? Is this resolved? I tried below suggestions, did not help..Querulous
B
18

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.

Bowman answered 28/2, 2015 at 23:44 Comment(4)
Helps me a lot! In my case I had ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); and Output output1 = Output(stream1). The goal was to serialize some Object into byte array using kryo.writeClassAndObject(output, someObject);.The important thing was to call, output1.close(); before calling stream1.toByteArray(); Hope this helps someone to save a time trying to quickstart Kryo!Jones
modern kryo does flush before closeVereeniging
Which modern Kryo version you are referring to? @Vereeniging The latest one here says "Output buffers the bytes when writing to an OutputStream, so flush or close must be called after writing is complete to cause the buffered bytes to be written to the OutputStream. " github.com/EsotericSoftware/kryo#outputBumboat
@Bumboat I'm saying, that you don't have to flush explicitly before closingVereeniging
T
5

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.

Torrietorrin answered 4/1, 2018 at 21:6 Comment(0)
U
4

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.

Uncivilized answered 2/5, 2016 at 22:19 Comment(1)
This idea is echoed by the answer here: github.com/EsotericSoftware/kryo/issues/…Hutchens

© 2022 - 2024 — McMap. All rights reserved.