I\'m getting an error (java.io.StreamCorruptedException: invalid type code: 00) when reading in a serialised object. Here is the class that implements serializable:
Came here to find solution for Mallet classifier deserialization. Finally my problem was that I trained and serialized the model in one Eclipse project and tried to deserialize in another Eclipse project. One of the classes (among those I created) in the serialized object was only on the class path of the former project and not on the class path of the latter.
If the JVM cannot find the class when you attempt to read a serialized object, this exception will be generated. Check that updated jar/class files are available in the class path etc..
If you're running mallet from the command line, it may be looking at the unzipped mallet jars and class files from a downloaded distribution, and not at your nice new class files created in your downloaded source repository..
Don't feel too bad - I've seen this happen to others, and have done it myself (though I got a more informative "ClassNotFoundException" to help me figure it out :)
You have implemented a recursive writeObject
method: when you write an instance to an output stream, it calls the writeObject method, which writes an int, and then writes the object to the output stream, which write an int, etc.
The goal of the serialVersionUID field is to check that the objects are compatible. It's done natively by the serialization mechanism. You don't have to do anything except changing the serialVersionUID value when the class changes.
Run to get the answer:
package so.java9217010;
import java.io.*;
public class SerializeMe implements Serializable{
private String foo;
private static final long serialVersionUID = 1;
private static final int CURRENT_SERIAL_VERSION = 1;
public SerializeMe(String foo) {
this.foo = foo;
}
public SerializeMe(){
}
public SerializeMe readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
int version = in.readInt();
if (version != CURRENT_SERIAL_VERSION)
throw new ClassNotFoundException("Mismatched NaiveBayesTrainer versions: wanted " +
CURRENT_SERIAL_VERSION + ", got " +
version);
//default selections for the kind of Estimator used
return (SerializeMe) in.readObject();
// nb = test.returnNB();
}
public void writeObject(ObjectOutputStream out) throws IOException
{
out.writeInt(CURRENT_SERIAL_VERSION);
//default selections for the kind of Estimator used
out.writeObject(this);
}
public String returnNB(){
return foo;
}
public void setNB(String foo){
this.foo = foo;
}
public static void main(String[] args) throws Exception {
SerializeMe o = new SerializeMe("hello");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
o.writeObject(oout);
oout.flush();
byte[] buff = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(buff);
ObjectInputStream oin = new ObjectInputStream(bin);
SerializeMe ro = o.readObject(oin);
System.out.format(
"got: %s -- the problem is most likely in the library you are using ...\n",
ro.returnNB());
}
}