Java Serialization Tool Class

Source: Internet
Author: User
Tags object object

Import Org.slf4j.logger;import org.slf4j.loggerfactory;import Sun.misc.base64decoder;import Sun.misc.BASE64Encoder ; Import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import java.io.objectoutputstream;/** * Serialization Tool class */public class Serializeutil {private final S    Tatic Logger Logger = Loggerfactory.getlogger (Serializeutil.class);        public static String Serialize (Object object) {bytearrayoutputstream bos = new Bytearrayoutputstream ();        ObjectOutputStream oos = null;            try {oos = new ObjectOutputStream (BOS);            Oos.writeobject (object);            Oos.flush ();        return new Base64encoder (). Encode (Bos.tobytearray ());        }catch (IOException e) {logger.error ("serialization error", E);                }finally {try {if (Oos! = null) {oos.close ();            } bos.close (); } catch (IOException e) {               E.printstacktrace ();    }} return null;        public static object Unserialize (String object) {Bytearrayinputstream bis = null;        ObjectInputStream ois = null;            try {bis = new Bytearrayinputstream (new Base64decoder (). Decodebuffer (object));            OIS = new ObjectInputStream (bis);            Object o = Ois.readobject ();        return o;        } catch (IOException e) {logger.error ("Deserialization error: IO exception", e);        } catch (ClassNotFoundException e) {logger.error ("deserialization Error: Class not found", e);                }finally {try {if (bis!=null) {bis.close ();                } if (Ois!=null) {ois.close ();            }} catch (IOException e) {e.printstacktrace ();    }} return null; }}

  

Java Serialization Tool Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.