java序列化辨析

java对象序列化是一个神奇的功能,它通过让对象实现Serializable接口,并将其传递给ObjectOutputStream的writeObject方法,就能得到该对像。writeObject是怎么样实现这个功能的呢?下面分析一下这个方法的实现

writeObject首先是获取当前序列化对象的的类信息,调用的是ObjectStreamClass的lookup方法

static ObjectStreamClass lookup(Class cl, boolean all) {
....
if (entry == null) {
try {
entry = new ObjectStreamClass(cl);//创建ObjectStreamClass
} catch (Throwable th) {
entry = th;
}
if (future.set(entry)) {
Caches.localDescs.put(key, new SoftReference<Object>(entry));
} else {
// nested lookup call already set future
entry = future.get();
}
}

if (entry instanceof ObjectStreamClass) {
return (ObjectStreamClass) entry;
} else if (entry instanceof RuntimeException) {
throw (RuntimeException) entry;
} else if (entry instanceof Error) {
throw (Error) entry;
} else {
throw new InternalError("unexpected entry: " + entry);
}
}



创建ObjectStreamClass会读取序列化所需要的一些类信息,代码如下


private ObjectStreamClass(final Class cl) {
this.cl = cl;
name = cl.getName();
isProxy = Proxy.isProxyClass(cl);//是否proxy类
isEnum = Enum.class.isAssignableFrom(cl);//是否Enum
serializable = Serializable.class.isAssignableFrom(cl);//否Serializable
externalizable = Externalizable.class.isAssignableFrom(cl);//是否Externalizable

Class superCl = cl.getSuperclass();
superDesc = (superCl != null) ? lookup(superCl, false) : null;
//获取父类及父类信息
localDesc = this;

if (serializable) {//如果serializable
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
if (isEnum) {
//Enum生成suid为0且将fields设置为空ObjectStreamField的数组
suid = Long.valueOf(0);
fields = NO_FIELDS;
return null;
}
if (cl.isArray()) {
//Array fields设置为空ObjectStreamField的数组
fields = NO_FIELDS;
return null;
}

suid = getDeclaredSUID(cl);//获取类中定义的suid
try {
fields = [color=red]getSerialFields[/color](cl);
//如果是Serializable且!Externalizable.class.isAssignableFrom(cl) &&
// !Proxy.isProxyClass(cl) &&
// !cl.isInterface()
//则获取类中要定义的序列化的属性域
//可以通过serialPersistentFields来定义要序列化的属性域,或者非static 非TRANSIENT
computeFieldOffsets();//计算field占用空间的大小,属性个数
} catch (InvalidClassException e) {
serializeEx = deserializeEx = e;
fields = NO_FIELDS;
}

if (externalizable) {
//实现了externalizable 获取构造器
cons = getExternalizableConstructor(cl);
} else {
//未实现externalizable 获取构造器 私有的writeObject readbject readObjectNoData
cons = getSerializableConstructor(cl);
writeObjectMethod = getPrivateMethod(cl, "writeObject",
new Class[] { ObjectOutputStream.class },
Void.TYPE);
readObjectMethod = getPrivateMethod(cl, "readObject",
new Class[] { ObjectInputStream.class },
Void.TYPE);
readObjectNoDataMethod = getPrivateMethod(
cl, "readObjectNoData", null, Void.TYPE);
hasWriteObjectData = (writeObjectMethod != null);
}
//获取 writeReplace readResolve
writeReplaceMethod = getInheritableMethod(
cl, "writeReplace", null, Object.class);
readResolveMethod = getInheritableMethod(
cl, "readResolve", null, Object.class);
return null;
}
});
} else {//未实现serializable suid为0且将fields设置为空ObjectStreamField的数组
suid = Long.valueOf(0);
fields = NO_FIELDS;
}

try {
fieldRefl = getReflector(fields, this);
} catch (InvalidClassException ex) {
// field mismatches impossible when matching local fields vs. self
throw new InternalError();
}

if (deserializeEx == null) {
if (isEnum) {
deserializeEx = new InvalidClassException(name, "enum type");
} else if (cons == null) {
deserializeEx = new InvalidClassException(
name, "no valid constructor");
}
}
for (int i = 0; i < fields.length; i++) {
if (fields[i].getField() == null) {
defaultSerializeEx = new InvalidClassException(
name, "unmatched serializable field(s) declared");
}
}
}



最后则是根据要序列化的Object类型调用相应的方法进行序列化
看看对于普通对象的实现代码
private void writeOrdinaryObject(Object obj,
ObjectStreamClass desc,
boolean unshared)
throws IOException
{
if (extendedDebugInfo) {
debugInfoStack.push(
(depth == 1 ? "root " : "") + "object (class \"" +
obj.getClass().getName() + "\", " + obj.toString() + ")");
}
try {
desc.checkSerialize();
//检查是否可序列化

bout.writeByte(TC_OBJECT);
writeClassDesc(desc, false);
handles.assign(unshared ? null : obj);
if (desc.isExternalizable() && !desc.isProxy()) {
//Externalizable writeExternalData中会调用对象实现的writeExternal方法来完成写入
writeExternalData((Externalizable) obj);
} else {

//writeSerialData方法中会判断序列化对象有无writeObject方法
writeSerialData(obj, desc);
}
} finally {
if (extendedDebugInfo) {
debugInfoStack.pop();
}
}
}

通过以上源码分析可以知道,类必须实现Serializable或者Externalizable接口,通过实现Externalizable接口 编写私有的writeObject或者writeReplace方法,给属性添加transient或者serialPersistentFileds来控制序列化的行为


如下这个列子中我们编写私用的writeObject和readObject以帮助我们序列化中我们需要混淆totalValue的值还在反序列化中才还原

class Person implements Serializable{
public String name;
public Person spouse;
@SuppressWarnings("rawtypes")
public ArrayList children=new ArrayList();

public double totalValue;
private void writeObject(ObjectOutputStream oos) throws IOException{
totalValue=totalValue*2-1;
oos.defaultWriteObject();
}

private void readObject(ObjectInputStream ois) throws Exception{
totalValue=(totalValue-1)/2;
ois.defaultReadObject();
}

}
}




下面的代码给出了 serialPersistentFields的声明示例,即只有name这个域是要被序列化的。而当一个属性同时被serialPersistentFields和transient声明 则以serialPersistentFields为主

class Person implements Serializable{
public String name;
public Person spouse;
@SuppressWarnings("rawtypes")
public ArrayList children=new ArrayList();
public double totalValue;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("name", String.class)
};
private void writeObject(ObjectOutputStream oos) throws IOException{
totalValue=totalValue*2-1;
oos.defaultWriteObject();
}

private void readObject(ObjectInputStream ois) throws Exception{
totalValue=(totalValue-1)/2;
ois.defaultReadObject();
}
}


[b]
序列化的安全性[/b]

Java对象序列化之后的内容格式是公开的。所以可以很容易的从中提取出各种信息。从实现的角度来说,可以从不同的层次来加强序列化的安全性。

[list]
[*]对序列化之后的流进行加密。这可以通过CipherOutputStream来实现。
[*] 实现自己的writeObject和readObject方法,在调用defaultWriteObject之前,先对要序列化的域的值进行加密处理。
[*] 使用一个SignedObject或SealedObject来封装当前对象,用SignedObject或SealedObject进行序列化。
[*] 在从流中进行反序列化的时候,可以通过ObjectInputStream的registerValidation方法添加ObjectInputValidation接口的实现,用来验证反序列化之后得到的对象是否合法。
[/list]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值