Deep clone serializing/de-serializng Clone : Clone « Class « Java






Deep clone serializing/de-serializng Clone

     
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

/**
 * Utility for object cloning
 * 
 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
 */
public class CloneUtil
{
    /**
     * Provides a deep clone serializing/de-serializng <code>objToClone</code>     
     * 
     * @param objToClone The object to be cloned
     * @return The cloned object
     */
    public static final Object deepClone(Object objToClone)
    {
        try
        {
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(100);
            ObjectOutputStream objectoutputstream = new ObjectOutputStream(bytearrayoutputstream);
            objectoutputstream.writeObject(objToClone);
            byte abyte0[] = bytearrayoutputstream.toByteArray();
            objectoutputstream.close();
            ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
            ObjectInputStream objectinputstream = new ObjectInputStream(bytearrayinputstream);
            Object clone = objectinputstream.readObject();
            objectinputstream.close();
            return clone;
        }
        catch (Exception exception)
        {
            // nothing
        }
        return null;
    }

}

   
    
    
    
    
  








Related examples in the same category

1.A Cloning Example
2.Class is declared to be cloneable.
3.Arrays are automatically cloneable
4.Clone objects
5.Creating a Deep Copy
6.Shallow Copy TestShallow Copy Test
7.Deep Copy TestDeep Copy Test
8.Uses serialization to perform deep copy cloning.
9.Tests cloning to see if destination of references are also clonedTests cloning to see if destination of references are also cloned
10.Creating local copies with cloneCreating local copies with clone
11.You can insert Cloneability at any level of inheritance
12.Cloning a composed objectCloning a composed object
13.Serializable and cloneSerializable and clone
14.Go through a few gyrations to add cloning to your own classGo through a few gyrations to add cloning to your own class
15.Checking to see if a reference can be clonedChecking to see if a reference can be cloned
16.The clone operation works for only a few items in the standard Java libraryThe clone operation works for only a few items in the standard Java library
17.Demonstration of cloning
18.Simple demo of avoiding side-effects by using Object.cloneSimple demo of avoiding side-effects by using Object.clone
19.Clone an object with clone method from parent
20.Manipulate properties after clone operation
21.Deep clone ObjectDeep clone Object
22.Serializable Clone
23.Utility for object cloning
24.Clone Via Serialization
25.Clone demo
26.Implements a pool of internalized objects
27.A collection of utilities to workaround limitations of Java clone framework
28.This program demonstrates cloning
29.Returns a copy of the object, or null if the object cannot be serialized.Returns a copy of the object, or null if the object cannot be serialized.
30.Deep-copies the values from one object to the other
31.Object Deep copy