Getting a Buffer Underflow

2,839 views
Skip to first unread message

Nikolai Ormazablev

unread,
May 27, 2013, 7:23:01 PM5/27/13
to kryo-...@googlegroups.com
Hi everyone.

I'm trying to serialize an object that contains an array of other objects. I created both classes implementing KryoSerializable, and the serialization seems to work fine. But when I try to deserialize, I get a Buffer Underflow exception while getting the array. The classes are pretty simple:

public class ClassA implements KryoSerializable {
    public ClassB[] classBArray;
    public float floatValue;

    public ClassA() {
        floatValue = 0;
        cursor = 0;
    }
   
    public ClassA(ArrayList<ClassB> classBArrayList, float floatValue) {
        this.classBArray = new ClassB[classBArrayList.size()];
        classBArrayList.toArray(this.classBArray);

        this.floatValue = floatValue;
    }
   
    @Override
    public void read(Kryo kryo, Input input) {
        floatValue = input.readFloat();
        classBArray = kryo.readObject(input, ClassB[].class);
        cursor = 0;
    }

    @Override
    public void write(Kryo kryo, Output output) {
        output.writeFloat(floatValue);
        kryo.writeObject(output, classBArray);
    }
}

public class ClassB implements KryoSerializable {
    public float numberFloat1;
    public int numberInt2;
    public float numberFloat3;
   
    public ClassB(){
        this.numberFloat1 = 0.0f;
        this.numberInt2 = 0;
        this.numberFloat3 = 0.0f;
    }

    public ClassB(float first, int second, float third) {
        this.numberFloat1 = first;
        this. = second;
        this.numberFloat3 = third;
    }
   
    @Override
    public void read(Kryo kryo, Input input) {
        numberFloat1 = input.readFloat();
        numberInt2 = input.readInt();
        numberFloat3 = input.readFloat();
    }

    @Override
    public void write(Kryo kryo, Output output) {
        output.writeFloat(numberFloat1);
        output.writeInt(numberInt2);
        output.writeFloat(numberFloat3);       
    }
}

I save the level with (properly surrounded with try/catch)

ClassA classA = ... some creation stuff here ...

Output output = new Output(new FileOutputStream("some path");
kryo.writeObject(output, classA);

 And read it with

Input input = new Input(new FileInputStream("some path"));
ClassA classA = kryo.readObject(input, ClassA.class);

I get a Buffer Underflow exception while reading the elements from the ClassB array contained in the ClassA. Here is part of the exception:

Exception in thread "LWJGL Application" com.esotericsoftware.kryo.KryoException: Buffer underflow.
    at com.esotericsoftware.kryo.io.Input.require(Input.java:156)
    at com.esotericsoftware.kryo.io.Input.readInt(Input.java:325)
    at com.esotericsoftware.kryo.io.Input.readFloat(Input.java:624)
    at com.my.classpath.ClassB.read(ClassB.java:28)
    at com.esotericsoftware.kryo.serializers.DefaultSerializers$KryoSerializableSerializer.read(DefaultSerializers.java:363)
    at com.esotericsoftware.kryo.serializers.DefaultSerializers$KryoSerializableSerializer.read(DefaultSerializers.java:355)
    at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:729)
    at com.esotericsoftware.kryo.serializers.DefaultArraySerializers$ObjectArraySerializer.read(DefaultArraySerializers.java:338)
    at com.esotericsoftware.kryo.serializers.DefaultArraySerializers$ObjectArraySerializer.read(DefaultArraySerializers.java:293)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:626)

What am I missing? Thanks!

Nate

unread,
May 28, 2013, 10:48:31 AM5/28/13
to kryo-...@googlegroups.com
Hi,

Close or flush the output.

-Nate


--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users
---
You received this message because you are subscribed to the Google Groups "kryo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kryo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nikolai Ormazablev

unread,
May 28, 2013, 10:57:02 AM5/28/13
to kryo-...@googlegroups.com
Oh, I'm such a moron. Works like a charm now.

Thanks Nate, for Kryo and Spine :)
Reply all
Reply to author
Forward
0 new messages