Import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.closeable;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream;import cn.zsmy.constant.constant;/** * @author SHM */public class Serializeutil {public static byte[] Serialize (Object value) {if (value = = null) {throw new NullPointerException ("Can ' t serialize null"); } byte[] RV = null; Bytearrayoutputstream BOS = NULL; ObjectOutputStream OS = null; try {bos = new Bytearrayoutputstream (); OS = new ObjectOutputStream (BOS); Os.writeobject (value); Os.close (); Bos.close (); RV = Bos.tobytearray (); } catch (Exception e) {e.printstacktrace (); Constant.MY_LOG.info ("serialize error"); } finally {close (OS); Close (BOS); } return RV; } public static Object deserialize (byTe[] in) {return deserialize (in, Object.class); } @SuppressWarnings ("Unchecked") public static <T> T deserialize (byte[] in, class<t> requiredtype) { Object RV = null; Bytearrayinputstream bis = null; ObjectInputStream is = null; try {if (in! = null) {bis = new bytearrayinputstream (in); is = new ObjectInputStream (bis); RV = Is.readobject (); }} catch (Exception e) {e.printstacktrace (); Constant.MY_LOG.info ("Deserialize error"); } finally {close (IS); Close (bis); } return (T) rv; } private static void Close (Closeable closeable) {if (closeable! = null) try {Closea Ble.close (); } catch (IOException e) {e.printstacktrace (); Constant.MY_LOG.info ("Close stream error"); } }}
Java Serialization Serializeutil