前言:
Java序列化是Java技術(shù)體系當(dāng)中的一個重要議題,序列化的意義在于信息的交換和存儲,通常會和io、持久化、rmi技術(shù)有關(guān)(eg:一些orm框架會要求持久化的對象類型實(shí)現(xiàn)Serializable接口)。
本文將提供Java自帶序列化機(jī)制和ProtoStuff的序列化(僅僅當(dāng)作一種數(shù)據(jù)格式)的比較,從序列化的內(nèi)容和特點(diǎn)來對二者進(jìn)行比較。
結(jié)論:1,Java序列化對象時不需要通過屬性的get set方法或其它無關(guān)序列化內(nèi)部定義的方法(比如readObject,writeObject是內(nèi)置的序列化方法),序列化也不需要get set方法支持,反序列化是構(gòu)造對象的一種手段。
2,Java序列化時類型必須完全匹配(全路徑類名+序列化id)。
3,Protostuff反序列化時并不要求類型匹配,比如包名、類名甚至是字段名,它僅僅需要序列化類型A 和反序列化類型B 的字段類型可轉(zhuǎn)換(比如int可以轉(zhuǎn)換為long)即可。
java.io.Serializable
標(biāo)識一個對象需要系列化,該對象類型需要實(shí)現(xiàn) Serializable 接口。關(guān)于序列化的認(rèn)識,可以參考IBM社區(qū)的文章《Java序列化的高級認(rèn)識》,本文直接拿該文檔的結(jié)論。
1,序列化的類型和反序列化的類型的序列化ID必須一致(遠(yuǎn)程信息交換時)。
2,靜態(tài)數(shù)據(jù)不會被序列化,Transient關(guān)鍵字修飾的字段不會被序列化。
3,對象序列化存儲時,兩次存儲相同值對象會有優(yōu)化(第二次對象寫入會只存儲引用)。
ProtostuffUtil
import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import com.dyuproject.protostuff.LinkedBuffer;import com.dyuproject.protostuff.ProtostuffIOUtil;import com.dyuproject.protostuff.Schema;import com.dyuproject.protostuff.runtime.RuntimeSchema;public class ProtostuffUtil { private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>(); private static <T> Schema<T> getSchema(Class<T> clazz) { @SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) cachedSchema.get(clazz); if (schema == null) { schema = RuntimeSchema.getSchema(clazz); if (schema != null) { cachedSchema.put(clazz, schema); } } return schema; } /** * 序列化 * * @param obj * @return */ public static <T> byte[] serializer(T obj) { @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { Schema<T> schema = getSchema(clazz); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } } /** * 反序列化 * * @param data * @param clazz * @return */ public static <T> T deserializer(byte[] data, Class<T> clazz) { try { T obj = clazz.newInstance(); Schema<T> schema = getSchema(clazz); ProtostuffIOUtil.mergeFrom(data, obj, schema); return obj; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }}
ProtostuffTest
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.Random;import org.apache.commons.lang.StringUtils;public class ProtostuffTest { /** 產(chǎn)生一個隨機(jī)的字符串*/ public static String randomString(int length) { String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; int strlen = str.length(); Random random = new Random(); StringBuffer buf = new StringBuffer(); for (int i = 0; i < length; i++) { int num = random.nextInt(strlen); buf.append(str.charAt(num)); } return buf.toString(); } private static ResourceObj getObj(String name, String path, int contentSize) { ResourceObj obj = new ResourceObj(name, path, ""); obj.setContent(randomString(contentSize)); return obj; } private static long speedTest(int contentSize, int times) { ResourceObj obj = getObj("lb.conf", "/home/admin/conf/lb", contentSize); long start = System.currentTimeMillis(); for (int i = 0; i < times; i++) { byte[] bytes = ProtostuffUtil.serializer(obj); ProtostuffUtil.deserializer(bytes, ResourceObj.class); } long end = System.currentTimeMillis(); return end - start; } private static long speedTestOrg(int contentSize, int times) throws IOException, ClassNotFoundException { ResourceObj obj = getObj("lb.conf", "/home/admin/conf/lb", contentSize); long start = System.currentTimeMillis(); for (int i = 0; i < times; i++) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(obj); byte[] bytes = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); ois.readObject(); } long end = System.currentTimeMillis(); return end - start; } public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println(speedTestOrg(9999999, 1)); System.out.println(speedTest(9999999, 1)); } private static void test() { ResourceObj obj = getObj("lb.conf", "/home/admin/conf/lb", 88888); byte[] bytes = ProtostuffUtil.serializer(obj); ResourceObj obj2 = ProtostuffUtil.deserializer(bytes, ResourceObj.class); System.out.println(obj2.getFilename()); System.out.println(obj2.getPath()); System.out.println(StringUtils.equals(obj.getContent(), obj2.getContent())); }}
結(jié)果
自己測試的結(jié)果:
1、在對象較小的時候,還是java自帶的序列化比較快。10M之下的時候。(沒有考慮對象的復(fù)雜度)
2、文件較大時,protostuff比較快。(protostuff-runtime方式,用protostuff可能會更快,只是沒這么方便)
Google 的protobuf是一個優(yōu)秀的序列化工具,跨語言、快速、序列化后體積小。
protobuf的一個缺點(diǎn)是需要數(shù)據(jù)結(jié)構(gòu)的預(yù)編譯過程,首先要編寫.proto格式的配置文件,再通過protobuf提供的工具生成各種語言響應(yīng)的代碼。由于java具有反射和動態(tài)代碼生成的能力,這個預(yù)編譯過程不是必須的,可以在代碼執(zhí)行時來實(shí)現(xiàn)。有個protostuff(http://code.google.com/p/protostuff/)已經(jīng)實(shí)現(xiàn)了這個功能。
protostuff基于Google protobuf,但是提供了更多的功能和更簡易的用法。其中,protostuff-runtime實(shí)現(xiàn)了無需預(yù)編譯對Java bean進(jìn)行protobuf序列化/反序列化的能力。
protostuff-runtime的局限是序列化前需預(yù)先傳入schema,反序列化不負(fù)責(zé)對象的創(chuàng)建只負(fù)責(zé)復(fù)制,因而必須提供默認(rèn)構(gòu)造函數(shù)。
此外,protostuff還可以按照protobuf的配置序列化成json/yaml/xml等格式。