Java Serialize serializedCopy(Object obj)

Here you can find the source of serializedCopy(Object obj)

Description

serialized Copy

License

Apache License

Declaration

public static Object serializedCopy(Object obj) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static Object serializedCopy(Object obj) {
        try {/*from  www .  j  a v a2 s  . co  m*/
            return deserialize(serialize(obj));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("this shouldn't happen");
        }
    }

    public static Object deserialize(byte[] bytes) throws ClassNotFoundException {
        try {
            ByteArrayInputStream input = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(input);
            return ois.readObject();
        } catch (IOException e) {
            throw new RuntimeException("error reading from byte-array!", e);
        }
    }

    public static byte[] serialize(Object obj) {
        try {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(buffer);
            oos.writeObject(obj);
            oos.close();
            return buffer.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException("error writing to byte-array!", e);
        }
    }
}

Related

  1. serializeClone(final Object obj)
  2. serializeCookie(Object object, String filePath)
  3. serializedCopy(T object)
  4. serializeDistributionType(String distributionType, String HadoopDistribution)
  5. serializeExecutor(Object executor)
  6. serializeFromString(final String pString)