001/* 002 * Copyright (c) 2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.reporting.rio.util; 031 032import java.io.ByteArrayInputStream; 033import java.io.ByteArrayOutputStream; 034import java.io.IOException; 035import java.io.ObjectInputStream; 036import java.io.ObjectOutputStream; 037 038import ptolemy.data.IntToken; 039 040/** 041 * From: http://javatechniques.com/blog/faster-deep-copies-of-java-objects/ 042 * Utility for making deep copies (vs. clone()'s shallow copies) of 043 * objects. Objects are first serialized and then deserialized. Error 044 * checking is fairly minimal in this implementation. If an object is 045 * encountered that cannot be serialized (or that references an object 046 * that cannot be serialized) an error is printed to System.err and 047 * null is returned. Depending on your specific application, it might 048 * make more sense to have copy(...) re-throw the exception. 049 * 050 */ 051public class SerializeDeepCopy { 052 053 /** 054 * Returns a copy of the object, or null if the object cannot 055 * be serialized. 056 */ 057 public static Object copy(Object orig) { 058 Object obj = null; 059 try { 060 // Write the object out to a byte array 061 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 062 ObjectOutputStream out = new ObjectOutputStream(bos); 063 out.writeObject(orig); 064 out.flush(); 065 out.close(); 066 067 // Make an input stream from the byte array and read 068 // a copy of the object back in. 069 ObjectInputStream in = new ObjectInputStream( 070 new ByteArrayInputStream(bos.toByteArray())); 071 obj = in.readObject(); 072 } 073 catch(IOException e) { 074 e.printStackTrace(); 075 } 076 catch(ClassNotFoundException cnfe) { 077 cnfe.printStackTrace(); 078 } 079 return obj; 080 } 081 082 public static void main(String[] args) { 083 IntToken token; 084 try { 085 token = IntToken.NIL; 086 IntToken newToken = (IntToken) copy(token); 087 System.out.println(newToken); 088 } catch (Exception e) { 089 // TODO Auto-generated catch block 090 e.printStackTrace(); 091 } 092 093 } 094}