/*
 * $Id: ObjectCloner.java,v 1.2 2006/03/23 11:44:31 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ObjectCloner.java,v $
 * Revision 1.2  2006/03/23 11:44:31  marbobo
 * + Mögliche NPE abgefangen
 *
 * Revision 1.1  2005/02/15 09:26:53  bigmac
 * Refactorings
 *
 * Revision 1.4  2004/11/24 09:26:03  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.3  2004/04/06 15:25:41  koppy
 * *** empty log message ***
 *
 * Revision 1.2  2003/12/24 10:12:00  bigmac
 * Projektweite Import-Optimierung
 *
 * Revision 1.1  2003/08/18 21:11:44  marbobo
 * Package verschoben
 *
 * Revision 1.3  2003/07/30 13:49:48  marbobo
 * +Kommentierung von Methoden und des Quellcodes
 * -unnötige Imports entfernt
 *
 */
package vkc.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import vkc.misc.VKCLogManager;

import net.matuschek.util.Base64;

/**
 * Hilfsklasse zum Kopieren (Klonen) eines beliebigen Objektes  
 * 
 * @author marbobo
 * @version 1.0
 */
public class ObjectCloner {
   // so that nobody can accidentally create an ObjectCloner object
   private ObjectCloner(){
   }

   /**
	* Diese Methode kopiert (klont) vollständig das gegebene Objekt
	* und gibt die Kopie zurück
	*
	* @param oldObj Objekt das kopiert werden soll
	*
	* @return die vollständige Kopie eines Objektes
	*
	* @throws Exception Fehler-Objekt
	*/
    public static final Object deepCopy(Object oldObj) throws Exception {
        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
            return ois.readObject(); // G
        } catch (Exception e) {
        	VKCLogManager.errorLog(ObjectCloner.class, "1", "Exception in ObjectCloner!", e);
      	
        	return null;
        } finally {
        	if (oos != null) {
                oos.close();
        	}
        	
        	if (ois != null) {
                ois.close();
        	}
        }
    }

	
	public static final String toString(Object object) throws IOException {
	    ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
	
		oos.writeObject(object);
		oos.flush();
		oos.close();
		
		return new String(Base64.encode(bos.toByteArray()));
	}
	
	public static final Object toObject(String string) throws IOException, ClassNotFoundException {
		ByteArrayInputStream bin = new ByteArrayInputStream(Base64.decode(string));
		ObjectInputStream ois = new ObjectInputStream(bin);
		
		Object o = ois.readObject();
		
		ois.close();
		
		return o;
    }
}