`

利用Serialization实现对象深拷贝

 
阅读更多
java 代码
 
  1. private PersonAggregateData copyAggregate(PersonAggregateData original) throws EventsPersonManagementException {  
  2.       
  3.     ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();  
  4.     ObjectOutputStream out = null;  
  5.     ObjectInputStream in = null;  
  6.     PersonAggregateData copy;  
  7.     try {  
  8.         out = new ObjectOutputStream(outputBytes);  
  9.         ByteArrayInputStream inputBytes;  
  10.         out.writeObject(original);  
  11.   
  12.         inputBytes = new ByteArrayInputStream(outputBytes.toByteArray());  
  13.         in = new ObjectInputStream(inputBytes);  
  14.         copy = (PersonAggregateData) in.readObject();  
  15.     } catch (IOException e) {  
  16.         throw new EventsPersonManagementException("errors occur while copying the PersonAggregateData", e);  
  17.     } catch (ClassNotFoundException e) {  
  18.         throw new EventsPersonManagementException("errors occur while copying the PersonAggregateData", e);  
  19.     } finally {  
  20.         try {  
  21.             if (out != null) {  
  22.                 out.close();  
  23.             }  
  24.             if (in != null) {  
  25.                 in.close();  
  26.             }  
  27.         } catch (IOException e) {  
  28.             throw new EventsPersonManagementException("errors occur while copying the PersonAggregateData", e);  
  29.         }  
  30.     }  
  31.     return copy;  
  32. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics