Make TreeMap (de)serialization compatible with Java

This is done by implementing the readObject()/writeObject() method
pair as demanded by the serialization specification. The specifics
were reverse-engineered from serializing trivial TreeMap instances
with OpenJDK.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin 2013-10-30 13:26:08 -05:00
parent afe09e32de
commit a90b3ae574

View File

@ -10,6 +10,10 @@
package java.util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class TreeMap<K,V> implements Map<K,V> {
private final Comparator<K> comparator;
private transient TreeSet<MyEntry<K,V>> set;
@ -232,4 +236,26 @@ public class TreeMap<K,V> implements Map<K,V> {
return new Collections.ValueIterator(set.iterator());
}
}
public final static long serialVersionUID = 0x0cc1f63e2d256ae6l;
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(size());
for (Entry<K, V> entry : entrySet()) {
out.writeObject(entry.getKey());
out.writeObject(entry.getValue());
}
}
private void readObject(ObjectInputStream in) throws IOException {
in.defaultReadObject();
initializeSet();
int size = in.readInt();
for (int i = 0; i < size; i++) try {
put((K) in.readObject(), (V) in.readObject());
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
}
}