Java Object Deep Clone deepClone(Serializable serializable)

Here you can find the source of deepClone(Serializable serializable)

Description

deep Clone

License

EUPL

Parameter

Parameter Description
serializable a parameter

Exception

Parameter Description
Exception an exception

Return

a deep cloned object includiding complete stack

Declaration

public static Serializable deepClone(Serializable serializable)
        throws Exception 

Method Source Code

//package com.java2s;
/*//from   w  ww.  j  a  v  a  2  s.  com
 * **************************************************-
 * Ingrid Server OpenSearch
 * ==================================================
 * Copyright (C) 2014 - 2017 wemove digital solutions GmbH
 * ==================================================
 * Licensed under the EUPL, Version 1.1 or ? as soon they will be
 * approved by the European Commission - subsequent versions of the
 * EUPL (the "Licence");
 * 
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * 
 * http://ec.europa.eu/idabc/eupl5
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * **************************************************#
 */

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

public class Main {
    /**
     * @param serializable
     * @return a deep cloned object includiding complete stack
     * @throws Exception
     */
    public static Serializable deepClone(Serializable serializable)
            throws Exception {
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        new ObjectOutputStream(arrayOutputStream).writeObject(serializable);
        ByteArrayInputStream bais = new ByteArrayInputStream(
                arrayOutputStream.toByteArray());
        return (Serializable) new ObjectInputStream(bais).readObject();
    }
}

Related

  1. deepClone(Object obj)
  2. deepClone(Object objToClone)
  3. deepClone(Object src)
  4. deepClone(Object src)
  5. deepClone(Serializable o)
  6. deepClone(T toClone, final ClassLoader classLoader)
  7. deepCloneObj(Object src)
  8. deepCloneOnlyIfContains(final T objectToBeClonned, final T[] objects)