I want to share java objects across different applications.
As long as I use the same package names in the different projects it works fine. But if I change the package names it doesn't work anymore.
I tried to solve this by extend the ObjectInputStream
class and overriding the readClassDescriptor
method.
But by doing so I get the following error:
java.io.StreamCorruptedException: invalid type code: 00
... dont know how to solve this problem.
Here is the code I use for the extended ObjectInputStream class:
public class MyObjectInputStream extends ObjectInputStream {
public static Map<String, Class> classNameMapping = initclassNameMapping();
private static Map<String, Class> initclassNameMapping(){
Map<String, Class> res = new HashMap<String, Class>();
//ipxTest is the name of the package where the objects got serialized
res.put("ipxTest.IPX", interprojectxchangeTest.IPX.class);
res.put("ipxTest.A", interprojectxchangeTest.A.class);
return Collections.unmodifiableMap(res);
}
public MyObjectInputStream(InputStream in) throws IOException {
super(in);
enableResolveObject(true);
}
protected MyObjectInputStream() throws IOException, SecurityException {
super();
enableResolveObject(true);
}
@Override
protected java.io.ObjectStreamClass readClassDescriptor()
throws IOException, ClassNotFoundException {
ObjectStreamClass desc = super.readClassDescriptor();
if (classNameMapping.containsKey(desc.getName()))
return ObjectStreamClass.lookup(classNameMapping.get(desc.getName()));
return desc;
}
}
The IPX and A classes both look equal in the different projects and have each the same serialID.