Java serialization and deserialization

Source: Internet
Author: User

Java serialization and deserialization
Basic concepts:
Serialization is the process of converting the object state to a format that can be maintained or transmitted. In contrast to serialization, deserialization converts a stream into an object. These two processes can be combined to easily store and transmit data.


It is particularly important in network transmission. We can serialize a class and then get the object through deserialization at the other end.


For example, we can serialize an object, but this object must implement a serialization method and generate a serialization number.


This is the process of serializing and deserializing an object:

public static byte[] serializeObj(Object object){if (object == 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(object);os.writeObject(null);os.close();bos.close();rv = bos.toByteArray();} catch (IOException e) {throw new IllegalArgumentException("Non-serializable object", e);} finally {try {if (os != null) {os.flush();os.close();}if (bos != null) {bos.flush();bos.close();}} catch (IOException e) {e.printStackTrace();}}return rv;}@SuppressWarnings("unchecked")public static <T>T deserializeObj(byte[] in,Class<T> clazz) {ByteArrayInputStream bis = null;ObjectInputStream is = null;try {if (in != null) {bis = new ByteArrayInputStream(in);is = new ObjectInputStream(bis);while (true) {T o = (T) is.readObject();if (o == null) {break;} else {return o;}}}} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {try {if (is != null) {is.close();}if (bis != null) {bis.close();}} catch (IOException e) {e.printStackTrace();}}return null;}

Below is an extension (serialization and deserialization of sets ):

public static <T>byte[] serializeList(List<T> value ,Class<T> clazz) {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);for (T t : value) {os.writeObject(t);}os.writeObject(null);os.close();bos.close();rv = bos.toByteArray();} catch (IOException e) {throw new IllegalArgumentException("Non-serializable object", e);} finally {try {if (os != null) {os.flush();os.close();}if (bos != null) {bos.flush();bos.close();}} catch (IOException e) {e.printStackTrace();}}return rv;}@SuppressWarnings("unchecked")public static <T> List<T> deserializeList(byte[] in,Class<T> clazz) {List<T> list = new ArrayList<T>();ByteArrayInputStream bis = null;ObjectInputStream is = null;try {if (in != null) {bis = new ByteArrayInputStream(in);is = new ObjectInputStream(bis);while (true) {T o = (T) is.readObject();if (o == null) {break;} else {list.add(o);}}}} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {try {if (is != null) {is.close();}if (bis != null) {bis.close();}} catch (IOException e) {e.printStackTrace();}}


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.