Java tutorial
/* * Kuali Coeus, a comprehensive research administration system for higher education. * * Copyright 2005-2015 Kuali, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.kuali.coeus.common.budget.framework.query; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.*; /** * The class is used to Deep Clone an Object * * @version 1.0 */ public class ObjectCloner { private static final Log LOG = LogFactory.getLog(ObjectCloner.class); // so that nobody can accidentally create an ObjectCloner object private ObjectCloner() { } // returns a deep copy of an object static public Object deepCopy(Object oldObj) { ObjectOutputStream oos = null; ObjectInputStream ois = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A oos = new ObjectOutputStream(bos); // B // serialize and pass the object oos.writeObject(oldObj); // C oos.flush(); // D ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E ois = new ObjectInputStream(bin); // F // return the new object Object object = ois.readObject(); oos.close(); ois.close(); return object; // G } catch (Exception e) { return null; } finally { try { if (oos != null) oos.close(); if (ois != null) ois.close(); } catch (IOException e) { LOG.error(e.getMessage(), e); } } } }