I have a class B which contains a transient field that its initialization
requires deserialization of class A. Trying to deserialize class B completely
breaks Kryo (running a single thread). I thought that maybe calling
'Kryo.getContext().reset()' would solve the issue but that didn't help.
What steps will reproduce the problem?
Run the attached code (also given below).
What is the expected output? What do you see instead?
I expect the program to end successfully. Instead I get an exception.
What version of the Kryo are you using?
1.04
Please provide any additional information below.
The code to reproduce the problem:
-------------------------------------------------------------------
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.ObjectBuffer;
public class Main {
public static <T> void serialize(T obj, String filename) throws FileNotFoundException, IOException
{
Kryo kryo = new Kryo();
kryo.setRegistrationOptional(true);
//Kryo.getContext().reset();
ObjectBuffer buffer = new ObjectBuffer(kryo, 100000);
OutputStream os = new FileOutputStream(filename);
buffer.writeClassAndObject(os, obj);
os.close();
}
@SuppressWarnings("unchecked")
public static <T> T deserialize(String filename) throws IOException, ClassNotFoundException
{
Kryo kryo = new Kryo();
kryo.setRegistrationOptional(true);
//Kryo.getContext().reset();
InputStream is = new FileInputStream(filename);
ObjectBuffer buffer = new ObjectBuffer(kryo, 100000);
Object obj = buffer.readClassAndObject(is);
is.close();
return (T) obj;
}
public static class Class1
{
private Vector<float[]> values = new Vector<float[]>();
public Class1() {
for(int i=0; i < 2; ++i) values.add(new float[i+1]);
}
public float[] getValue(int i) { return values.get(i); }
}
public static class Class2
{
private Map<String, Vector<LinkedList<Float>>> data = new HashMap<String, Vector<LinkedList<Float>>>();
private transient Class1 class_1 = readClass1();
public Class2() {
Vector<LinkedList<Float>> value1 = new Vector<LinkedList<Float>>();
value1.add(new LinkedList<Float>());
value1.lastElement().add(1.1f);
value1.lastElement().add(1.2f);
value1.add(new LinkedList<Float>());
value1.lastElement().add(2.1f);
value1.lastElement().add(2.2f);
Vector<LinkedList<Float>> value2 = new Vector<LinkedList<Float>>();
value2.add(new LinkedList<Float>());
value2.lastElement().add(1.1f);
value2.lastElement().add(1.2f);
value2.add(new LinkedList<Float>());
value2.lastElement().add(2.1f);
value2.lastElement().add(2.2f);
value2.add(new LinkedList<Float>());
value2.lastElement().add(3.1f);
value2.lastElement().add(3.2f);
data.put("key1", value1);
data.put("key2", value2);
}
public float[] getValue() { return class_1.getValue(1); }
private Class1 readClass1()
{
try {
return deserialize("d:/test_java_class_1.dat");
} catch (Exception e) {
throw new Error("Can't deserialize !", e);
}
}
}
public static void main(String[] args) throws Exception
{
com.esotericsoftware.minlog.Log.TRACE = true;
Class1 base = new Class1();
serialize(base, "d:/test_java_class_1.dat");
Class2 one = new Class2();
System.out.println(one.getValue().length);
serialize(one, "d:/test_java_class_2.dat");
Class2 two = deserialize("d:/test_java_class_2.dat");
System.out.println(two.getValue().length);
}
}
Original issue reported on code.google.com by weiss...@gmail.com on 14 Feb 2012 at 11:49
Original issue reported on code.google.com by
weiss...@gmail.com
on 14 Feb 2012 at 11:49Attachments: