重构后更改序列化Java对象的路径/类名

时间:2012-04-21 14:25:51

标签: java android serialization refactoring deserialization

我启动了一个使用序列化对象myfirstpath.UserState保存其状态的应用。

现在我想将此对象的路径更改为mycleanpath.UserState(同一对象,仅路径更改)。这适用于首次安装应用程序的新用户,但对于更新应用程序的用户,他们将失去状态。

有没有办法将序列化的obect myfirstpath.UserState加载到mycleanpath.UserState中? (当然不在我的源代码中保留myfirstpath.UserState)。

2 个答案:

答案 0 :(得分:3)

我写了一小段代码来搜索/替换包含序列化数据的文件中的旧路径/新路径。我在加载之前转换文件,这样我就可以将序列化类移动到新路径,而无需在旧路径上保留此类的副本。这就是你如何使用它:

File baseDirectory = applicationContext.getFilesDir();
File file = new File( baseDirectory, "settings.data" );
if (file.exists()) {
    //We have to convert it to newsettings.Data
    byte[] convertedBytes = common.utils.SerializeTools.changePathInSerializedFile(file, "old.path.data", "new.path.data");

    //Write converted file
    File newFile = new File( baseDirectory, "newsettings.data" );
    FileOutputStream fos = new FileOutputStream(newFile);
    fos.write(convertedBytes);
    fos.close();

    //Remove old file
    file.delete();
}

这是SerializeTools.java的代码。我在这篇精彩的博文http://www.javaworld.com/community/node/2915中学习了java Serialize格式。

package common.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class SerializeTools {

static public byte[] changePathInSerializedFile(File f, String fromPath, String toPath) throws IOException {
    byte[] buffer = new byte[(int)f.length()];
    FileInputStream in = new FileInputStream(f);
    in.read(buffer);
    in.close();
    return SerializeTools.changePathInSerializedData(buffer,fromPath,toPath);
}

static public byte[] changePathInSerializedData(byte[] buffer, String fromPath, String toPath) throws IOException {
    byte[] search = fromPath.getBytes("UTF-8");
    byte[] replace = toPath.getBytes("UTF-8");

    ByteArrayOutputStream f = new ByteArrayOutputStream();

    for (int i=0;i<buffer.length;i++) {
        //Search 2 bytes ahead to let us modify the 2 bytes length of the class name (see Serialize format http://www.javaworld.com/community/node/2915 )
        boolean found=false;
        int searchMaxIndex=i+search.length+2;
        if (searchMaxIndex<=buffer.length) {
            found=true;
            for (int j=i+2;j<searchMaxIndex;j++) {
                if (search[j-i-2]!=buffer[j]) {
                    found=false;
                    break;
                }
            }
        }
        if (found) {
            int high=((int)(buffer[i])&0xff);
            int low=((int)(buffer[i+1])&0xff);
            int classNameLength=(high<<8)+low;
            classNameLength+=replace.length-search.length;
            //Write new length
            f.write((classNameLength>>8)&0xff);
            f.write((classNameLength)&0xff);
            //Write replacement path
            f.write(replace);
            i=searchMaxIndex-1;
        } else {
            f.write(buffer[i]);
        }
    }

    f.flush();
    f.close();

    return f.toByteArray();
}

}

答案 1 :(得分:1)

您必须在readResolve课程中实施Serializable Javadoc中提到的方法myfirstpath.UserState。此readResolve必须返回mycleanpath.UserState对象。