今天我们重点来看看Hessian是如何实现
序列化的(这些序列化类可以脱离hessian源码包,单独拿出来使用),参考源码
版本为3.0.20
首先我们看下序列化一个对象的代码:
// 这里可以是任何形式的OutputStream,现以FileOutputStream为例
OutputStream os = new FileOutputStream("hessianOutput");
// 创建 HessianOutput对象
AbstractHessianOutput out = new HessianOutput(os);
// 设置序列化工厂
out.setSerializerFactory(new SerializerFactory());
// 序列化 obj对象(obj可以是一个简单对象比如Integer,String,也可以是一个自定义类,同时也可以// 是数组,枚举,集合等等)
out.writeObject(obj);
我们来逐步分析下上面这段代码:
1.out.setSerializerFactory(new SerializerFactory());
首先SerializerFactory是什么?顾名思义 SerializerFactory是一个序列化工厂,里面包含了序列化(反序列化)各种类型的Serializer(DeSerializer)类,我们看下在创建SerializerFactory时,它做了写什么:(由于序列化工厂包含序列化与反序列化,为了表达方面的方便,我这里只说序列化)
// 静态代码快
static {
// 创建序列化和反序列化map,至于其中的Key-Value 我想大家也能猜到,key就是某个需要序列化的class,value就是序列化该class的Serializer类
_serializerMap = new HashMap();
_deserializerMap = new HashMap();
_typeMap = new HashMap();
// 初始化序列化和反序列化map (对于“简单类型”都使用BasicSerializer作为它的序列化类)
addBasic(void.class, "void", BasicSerializer.NULL);
addBasic(Boolean.class, "boolean", BasicSerializer.BOOLEAN);
addBasic(Byte.class, "byte", BasicSerializer.BYTE);
addBasic(Short.class, "short", BasicSerializer.SHORT);
addBasic(Integer.class, "int", BasicSerializer.INTEGER);
addBasic(Long.class, "long", BasicSerializer.LONG);
addBasic(Float.class, "float", BasicSerializer.FLOAT);
addBasic(Double.class, "double", BasicSerializer.DOUBLE);
addBasic(Character.class, "char", BasicSerializer.CHARACTER);
addBasic(String.class, "string", BasicSerializer.STRING);
addBasic(Object.class, "object", BasicSerializer.OBJECT);
addBasic(java.util.Date.class, "date", BasicSerializer.DATE);
addBasic(boolean.class, "boolean", BasicSerializer.BOOLEAN);
addBasic(byte.class, "byte", BasicSerializer.BYTE);
addBasic(short.class, "short", BasicSerializer.SHORT);
addBasic(int.class, "int", BasicSerializer.INTEGER);
addBasic(long.class, "long", BasicSerializer.LONG);
addBasic(float.class, "float", BasicSerializer.FLOAT);
addBasic(double.class, "double", BasicSerializer.DOUBLE);
addBasic(char.class, "char", BasicSerializer.CHARACTER);
addBasic(boolean[].class, "[boolean", BasicSerializer.BOOLEAN_ARRAY);
addBasic(byte[].class, "[byte", BasicSerializer.BYTE_ARRAY);
addBasic(short[].class, "[short", BasicSerializer.SHORT_ARRAY);
addBasic(int[].class, "[int", BasicSerializer.INTEGER_ARRAY);
addBasic(long[].class, "[long", BasicSerializer.LONG_ARRAY);
addBasic(float[].class, "[float", BasicSerializer.FLOAT_ARRAY);
addBasic(double[].class, "[double", BasicSerializer.DOUBLE_ARRAY);
addBasic(char[].class, "[char", BasicSerializer.CHARACTER_ARRAY);
addBasic(String[].class, "[string", BasicSerializer.STRING_ARRAY);
addBasic(Object[].class, "[object", BasicSerializer.OBJECT_ARRAY);
_serializerMap.put(Class.class, new ClassSerializer());
_deserializerMap.put(Class.class, new ClassDeserializer());
_deserializerMap.put(Number.class, new BasicDeserializer(BasicSerializer.NUMBER));
_serializerMap.put(BigDecimal.class, new StringValueSerializer());
try {
_deserializerMap.put(BigDecimal.class,
new StringValueDeserializer(BigDecimal.class));
} catch (Throwable e) {
}
// 后面代码省略,内容基本同上
}
addBasic 做了写什么呢?
// addBasic 做的很简单 以 addBasic(Integer.class, "int", BasicSerializer.INTEGER); 为例子实际上就是_serializerMap.put(Integer.class, new BasicSerializer(BasicSerializer.INTEGER));
private static void addBasic(Class cl, String typeName, int type)
{
// 放入key-value
_serializerMap.put(cl, new BasicSerializer(type));
Deserializer deserializer = new BasicDeserializer(type);
_deserializerMap.put(cl, deserializer);
_typeMap.put(typeName, deserializer);
}
2.out.writeObject(obj);
HessianOutput 继承 AbstractHessianOutput 且 writeObject是 父类中的一个抽象方法,我们看下在HessianOutput中 writeObject做了写什么:
public void writeObject(Object object)
throws IOException
{
if (object == null) {
writeNull();
return;
}
Serializer serializer;
//从序列化工厂得到能够序列化该对象的序列化类
serializer = _serializerFactory.getSerializer(object.getClass());
//使用具体的序列化类序列化对象
serializer.writeObject(object, this);
}
我们看下 _serializerFactory.getSerializer(object.getClass());的实现
public Serializer getSerializer(Class cl)
throws HessianProtocolException
{
Serializer serializer;
// 如果是简单类型,那么直接就能获取到(备注:这里能够取到的都是之前在SerializerFactory的static静态代码块中初始化的那些key)
serializer = (Serializer) _serializerMap.get(cl);
if (serializer != null)
return serializer;
// 从缓存中取 (为什么这里有个缓存的东西,看后面代码就立刻明了)
if (_cachedSerializerMap != null) {
serializer = (Serializer) _cachedSerializerMap.get(cl);
if (serializer != null)
return serializer;
}
// 省略部分代码....
// 获取其他类型的序列化类
if (serializer != null) {
}
else if (HessianRemoteObject.class.isAssignableFrom(cl))
serializer = new RemoteSerializer();
else if (BurlapRemoteObject.class.isAssignableFrom(cl))
serializer = new RemoteSerializer();
else if (Map.class.isAssignableFrom(cl))
serializer = new MapSerializer();
else if (Collection.class.isAssignableFrom(cl)) {
if (_collectionSerializer == null) {
_collectionSerializer = new CollectionSerializer();
}
serializer = _collectionSerializer;
}
else if (cl.isArray())
serializer = new ArraySerializer();
else if (Throwable.class.isAssignableFrom(cl))
serializer = new ThrowableSerializer(cl);
else if (InputStream.class.isAssignableFrom(cl))
serializer = new InputStreamSerializer();
else if (Iterator.class.isAssignableFrom(cl))
serializer = IteratorSerializer.create();
else if (Enumeration.class.isAssignableFrom(cl))
serializer = EnumerationSerializer.create();
else if (Calendar.class.isAssignableFrom(cl))
serializer = CalendarSerializer.create();
else if (_enumClass != null && _enumClass.isAssignableFrom(cl))
serializer = new EnumSerializer(cl);
if (serializer == null)
// 使用默认序列化类(JavaSerializer),一般是序列化我们自定义类的
serializer = getDefaultSerializer(cl);
if (_cachedSerializerMap == null)
_cachedSerializerMap = new HashMap(8);
// 放入缓存map中,下次获取同样的序列化类就比较方便,不需要再经过那么多判断了
_cachedSerializerMap.put(cl, serializer);
return serializer;
}
3.serializer.writeObject(object, this);
这里使用刚才获取到的Serializer类(可能是JavaSerializer,也可能是BasicSerializer或者其他序列化类)来序列化对象,具体实现留待下次分析
总结以下序列化的过程就是:
- 大小: 57.7 KB