Allow a set of classname translations to be configured in an
ObjectInputStream to facilitate conversion of serialized data when classes are repackaged. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3642 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -24,6 +24,7 @@ package com.threerings.io;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
import com.samskivert.util.HashIntMap;
|
import com.samskivert.util.HashIntMap;
|
||||||
import com.samskivert.util.StringUtil;
|
import com.samskivert.util.StringUtil;
|
||||||
@@ -57,6 +58,19 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
_loader = loader;
|
_loader = loader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures this object input stream with a mapping from an old
|
||||||
|
* class name to a new one. Serialized instances of the old class name
|
||||||
|
* will use the new class name when unserializing.
|
||||||
|
*/
|
||||||
|
public void addTranslation (String oldname, String newname)
|
||||||
|
{
|
||||||
|
if (_translations == null) {
|
||||||
|
_translations = new HashMap();
|
||||||
|
}
|
||||||
|
_translations.put(oldname, newname);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a {@link Streamable} instance or one of the supported object
|
* Reads a {@link Streamable} instance or one of the supported object
|
||||||
* types from the input stream.
|
* types from the input stream.
|
||||||
@@ -88,9 +102,18 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
// first swap the code into positive-land
|
// first swap the code into positive-land
|
||||||
code *= -1;
|
code *= -1;
|
||||||
|
|
||||||
// read in the class metadata, create a class mapping record
|
// read in the class metadata
|
||||||
// for the class, and cache it
|
|
||||||
String cname = readUTF();
|
String cname = readUTF();
|
||||||
|
// if we have a translation (used to cope when serialized
|
||||||
|
// classes are renamed) use it
|
||||||
|
if (_translations != null) {
|
||||||
|
String tname = (String)_translations.get(cname);
|
||||||
|
if (tname != null) {
|
||||||
|
cname = tname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a class mapping record, and cache it
|
||||||
Class sclass = Class.forName(cname, true, _loader);
|
Class sclass = Class.forName(cname, true, _loader);
|
||||||
Streamer streamer = Streamer.getStreamer(sclass);
|
Streamer streamer = Streamer.getStreamer(sclass);
|
||||||
if (STREAM_DEBUG) {
|
if (STREAM_DEBUG) {
|
||||||
@@ -221,6 +244,10 @@ public class ObjectInputStream extends DataInputStream
|
|||||||
/** The class loader we use to instantiate objects. */
|
/** The class loader we use to instantiate objects. */
|
||||||
protected ClassLoader _loader = getClass().getClassLoader();
|
protected ClassLoader _loader = getClass().getClassLoader();
|
||||||
|
|
||||||
|
/** An optional set of class name translations to use when
|
||||||
|
* unserializing objects. */
|
||||||
|
protected HashMap _translations;
|
||||||
|
|
||||||
/** Used to activate verbose debug logging. */
|
/** Used to activate verbose debug logging. */
|
||||||
protected static final boolean STREAM_DEBUG = false;
|
protected static final boolean STREAM_DEBUG = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user