EsotericSoftware / kryo

Java binary serialization and cloning: fast, efficient, automatic
BSD 3-Clause "New" or "Revised" License
6.19k stars 823 forks source link

PriorityQueueSerializer fails to load empty empty queue #865

Closed holgerbrandl closed 2 years ago

holgerbrandl commented 2 years ago

Describe the bug Consider the following example:

PriorityQueue<String> foo  = new PriorityQueue<>();

Kryo kryo = KryoKoinKt.buildKryoKoin();

File saveFile = new File("dat.bin");
Output o = new Output(new FileOutputStream(saveFile));
kryo.writeObject(o, foo);
o.close();

Input i = new Input(new FileInputStream(saveFile));

PriorityQueue<String> restored = kryo.readObject(i, PriorityQueue.class);

System.out.println(restored);

Expected: it should read the empty queue.

Observed:

Exception in thread "main" java.lang.IllegalArgumentException
    at java.base/java.util.PriorityQueue.<init>(PriorityQueue.java:171)
    at com.esotericsoftware.kryo.serializers.DefaultSerializers$PriorityQueueSerializer.createPriorityQueue(DefaultSerializers.java:746)
    at com.esotericsoftware.kryo.serializers.DefaultSerializers$PriorityQueueSerializer.create(DefaultSerializers.java:738)
    at com.esotericsoftware.kryo.serializers.DefaultSerializers$PriorityQueueSerializer.create(DefaultSerializers.java:732)
    at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:216)
    at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:44)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:774)
    at KryoPrioMap.main(KryoPrioMap.java:25)

Environment:

theigl commented 2 years ago

Good catch. I think we need to use a minimum capacity of 1.

theigl commented 2 years ago

Fixed. Please test against the lastest snapshot.

holgerbrandl commented 2 years ago

Works for me. Thanks for the quick update.