乙個簡單的java物件序列化工具類

2021-06-10 06:53:42 字數 1942 閱讀 6215

/**

* serialized or compressed data.

* * @author dongjian

*/@suppresswarnings("unchecked")

public final class transcoder

public static t decodeobject(byte b)

public static byte encodeobject(object o)

/*** decode the string with the current character set.

*/public static string decodestring(byte data)

} catch (unsupportedencodingexception e)

return rv;

} /**

* encode a string into the current character set.

*/public static byte encodestring(string in) catch (unsupportedencodingexception e)

return rv;

} /**

* get the bytes representing the given serialized object.

*/private static byte serialize(object o)

byte rv = null;

bytearrayoutputstream bos = null;

objectoutputstream os = null;

try catch (ioexception e) finally

return rv;

} /**

* get the object represented by the given serialized bytes.

*/private static object deserialize(byte in)

} catch (ioexception e) catch (classnotfoundexception e) finally

return rv;

} /**

* compress the given array of bytes.

*/private static byte compress(byte in)

bytearrayoutputstream bos = new bytearrayoutputstream();

gzipoutputstream gz = null;

try catch (ioexception e) finally

byte rv = bos.tobytearray();

log.debug("compressed " + in.length + " bytes to " + rv.length);

return rv;

} /**

* decompress the given array of bytes.

* * @return null if the bytes cannot be decompressed

*/private static byte decompress(byte in)

} catch (ioexception e) finally

} return bos == null ? null : bos.tobytearray();

} private static final class closeutil

public static void close(closeable closeable) catch (exception e)

}} }

}

JAVA序列化工具的比較

這次由於使用原生的序列化,貌似系統很慢,當系統的快慢不僅是一方面的因素,但在集群中,2個系統,不同架構間切換顯的很慢,一開始使用kryo來解決,但最後發現不支援原生的序列化,session為了避免序列化產生的冗餘問題,設計的時候欄位都是用transient來標識,導致無法序列化和反序列化。提出問題後...

將乙個類的乙個物件序列化到檔案

序列化 將記憶體中的物件 資料 變成乙個位元組序。反序列化 將位元組序還原為乙個物件。那麼這個只需要此物件寫入檔案,然後完成序列化 反序列化就是將此物件的序列化重新反序列化 序列化 將記憶體中的物件 資料 變成乙個位元組序。反序列化 將位元組序還原為乙個物件。include include incl...

java中序列化乙個物件儲存在檔案中的簡單過程

public class student implements serializable public student int id,string name override public string tostring public int getid public void setid int ...