序列化实现深拷贝
SerializeUtil.java
1 package com.lei.DailyTest.D20180316; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.ObjectInputStream; 6 import java.io.ObjectOutputStream; 7 import java.io.Serializable; 8 9 import org.slf4j.Logger; 10 import org.slf4j.LoggerFactory; 11 12 13 14 public class SerializeUtil { 15 16 private static Logger logger = LoggerFactory.getLogger(SerializeUtil.class); 17 18 public static byte[] serialize(Object obj) { 19 byte[] result=new byte[0]; 20 if(obj==null) { 21 return result; 22 } 23 try { 24 ByteArrayOutputStream baos=new ByteArrayOutputStream(128); 25 ObjectOutputStream oos=null; 26 try { 27 if (!(obj instanceof Serializable)) { 28 logger.error( ByteArrayOutputStream.class.getName()+ " requires a Serializable payload " + 29 "but received an object of type [" + obj.getClass().getName() + "]"); 30 return new byte[0]; 31 } 32 oos=new ObjectOutputStream(baos); 33 oos.writeObject(obj); 34 oos.flush(); 35 result=baos.toByteArray(); 36 }catch (Exception e) { 37 logger.error("Failed to serialize, Can't write \n"+e); 38 } 39 finally { 40 if(oos!=null) 41 oos.close(); 42 logger.info("Serialize success!"); 43 } 44 }catch (Exception e) { 45 logger.error("Failed to serialize, Can't create ByteArrayOutputStream\n"+e); 46 } 47 return result; 48 } 49 50 public static Object deSerialize(byte[] bytes) { 51 Object result=null; 52 if(bytes==null || bytes.length==0) { 53 return result; 54 } 55 try { 56 ByteArrayInputStream bais=new ByteArrayInputStream(bytes); 57 ObjectInputStream ois=null; 58 try { 59 ois=new ObjectInputStream(bais); 60 result=ois.readObject(); 61 }catch (Exception e) { 62 logger.error("Failed to deSerialize\n"+e); 63 }finally { 64 if(ois!=null) 65 ois.close(); 66 logger.info("Deserialize success!"); 67 } 68 69 }catch (Exception e) { 70 logger.error("Failed to deSerialize\n"+e); 71 } 72 return result; 73 } 74 }
TestClass.java
package com.lei.DailyTest.D20180316; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class TestClass { public static void main(String[] args) { // TODO Auto-generated method stub Cat cat1=new Cat("阿三"); Cat cat2=new Cat("阿四"); List<Cat> list=new ArrayList<Cat>(); list.add(cat1); list.add(cat2); Dog dog=new Dog(18,"大黄",new Cat("小猫咪"),list); byte[] bytes=SerializeUtil.serialize(dog); Dog obj=(Dog) SerializeUtil.deSerialize(bytes); System.out.println(obj); byte[] bytes2=SerializeUtil.serialize(list); @SuppressWarnings("unchecked") List<Cat> cats=(List<Cat>) SerializeUtil.deSerialize(bytes2); System.out.println(cats); } } class Dog implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private int age; private String name; private Cat cat; private List<Cat> cats; public Dog() { super(); // TODO Auto-generated constructor stub } public Dog(int age, String name, Cat cat,List<Cat> cats) { super(); this.age = age; this.name = name; this.cat = cat; this.cats = cats; } @Override public String toString() { return "Dog [age=" + age + ", name=" + name + ", cat=" + cat + ", cats=" + cats + "]"; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public List<Cat> getCats() { return cats; } public void setCats(List<Cat> cats) { this.cats = cats; } } class Cat implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String name; public Cat() { super(); // TODO Auto-generated constructor stub } public Cat(String name) { super(); this.name = name; } @Override public String toString() { return "Cat [name=" + name + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
要注意的是要序列化的对象以及其中的对象类型的需要实现Serializable接口