gaob13 / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

TreeMap deserialization error #112

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
test code:
bean for serialization
public class TreeMapSerializerTestData implements Serializable {
    private static final long serialVersionUID = 8449423540690186724L;

    public String x1 = "abc";
    private Map<String, String> treeMap;
    public String x2 = "abc";
    public String x3 = "def";

    public TreeMapSerializerTestData() {
        treeMap = new TreeMap<String, String>(new FakeComparator());
    }

    public TreeMapSerializerTestData(Map<String, String> init) {
        treeMap = new TreeMap<String, String>(new FakeComparator());
        treeMap.putAll(init);
    }

    public static class FakeComparator implements Comparator<String>, Serializable {
        private static final long serialVersionUID = 3667761299603933963L;

        @Override
        public int compare(String o1, String o2) {
            return o2.compareTo(o1);
        }
    }
}

client code:
    public void testTreeMap2() {
        TreeMapSerializerTestData tm =
                new TreeMapSerializerTestData(ImmutableMap.of("zxczxc", "qweasd", "fghfghd", "567652", "vbnvbn", "2013-04-29"));

        Kryo kryo = new Kryo();
        Output output = new Output(16384, 1024*1024);
        kryo.writeClassAndObject(output, tm);
        output.close();

        byte[] bytes = output.toBytes();
        TreeMapSerializerTestData utm = (TreeMapSerializerTestData) kryo.readClassAndObject(new Input(bytes));

        assertEquals(utm, tm);
    }

What is the expected output? What do you see instead?
I expect test passed, but get exception instead:

com.esotericsoftware.kryo.KryoException: java.lang.IndexOutOfBoundsException: 
Index: 120, Size: 2
Serialization trace:
treeMap (com.zina.serializer.beans.test.TreeMapSerializerTestData)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:626)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:221)
    at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:729)
...
Caused by: java.lang.IndexOutOfBoundsException: Index: 120, Size: 2
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at com.esotericsoftware.kryo.util.MapReferenceResolver.getReadObject(MapReferenceResolver.java:42)
    at com.esotericsoftware.kryo.Kryo.readReferenceOrNull(Kryo.java:773)
    at com.esotericsoftware.kryo.Kryo.readObjectOrNull(Kryo.java:697)
    at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:122)
    at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:17)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:648)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:605)
    ... 26 more

What version of the Kryo are you using?
2.21 has this issue, 2.20 - no

Please provide any additional information below.
Any class that contains TreeMap fails to deserialize - sometimes get 
java.lang.IndexOutOfBoundsException, sometimes class cast exception

Original issue reported on code.google.com by michael....@gmail.com on 29 Apr 2013 at 8:15

GoogleCodeExporter commented 8 years ago
I'm experiencing the same problem with using Kryo 2.21 -- and my class contains 
TreeMap:

May 09 19:47:42 ERROR - Something went wrong: 
java.lang.IndexOutOfBoundsException: Index: 118, Size: 0
java.lang.IndexOutOfBoundsException: Index: 118, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:571)
    at java.util.ArrayList.get(ArrayList.java:349)
    at com.esotericsoftware.kryo.util.MapReferenceResolver.getReadObject(MapReferenceResolver.java:42)
    at com.esotericsoftware.kryo.Kryo.readReferenceOrNull(Kryo.java:773)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:624)
    at java.lang.Thread.run(Thread.java:679)

Original comment by mat...@peel.com on 9 May 2013 at 8:00

GoogleCodeExporter commented 8 years ago
Hi Michael,

Yes. This is a bug and it is now fixed in trunk, i.e. in the v2.22-SNAPSHOT
Thanks for reporting it!

-Leo

Original comment by romixlev on 23 Jul 2013 at 1:21

GoogleCodeExporter commented 8 years ago
I am getting this same issue with 2.23-SNAPSHOT on deserialising an object 
which does not have a Map reference in it. Object values comprise only long, 
String, Double and boolean.

Original comment by meyers....@gmail.com on 24 Jan 2014 at 5:21

GoogleCodeExporter commented 8 years ago
@ian: Could you please provide a bit more information? May be provide a 
structure or your class? Or even better a small test-case? 

BTW, Kryo has moved to github. Could you please comment/file issues there?
https://github.com/EsotericSoftware/kryo

Original comment by romixlev on 24 Jan 2014 at 7:55