Java Object Serialize and Deserialize serializeAndGetObject(Object obj)

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

Description

serialize And Get Object

License

Apache License

Declaration

public static Object serializeAndGetObject(Object obj) throws ClassNotFoundException, IOException 

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 serializeAndGetObject(Object obj) throws ClassNotFoundException, IOException {
        return getObjectFromByteArray(getByteObject(obj));
    }//  ww  w  .j  a v a 2 s .c o m

    private static Object getObjectFromByteArray(byte[] byteArray) throws IOException, ClassNotFoundException {
        ByteArrayInputStream bs = new ByteArrayInputStream(byteArray);
        ObjectInputStream is = new ObjectInputStream(bs);
        Object serializableObject = is.readObject();
        is.close();
        return serializableObject;
    }

    private static byte[] getByteObject(Object obj) throws IOException {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bs);
        os.writeObject(obj);
        os.close();
        return bs.toByteArray();
    }
}

Related

  1. serializeAndDeserialize(T obj)
  2. serializeAndDeserialize(T object)
  3. serializeAndDeserialize(T object)
  4. serializeAndDeserialize(T object)
  5. serializeAndDeserializeObject(Object myObject)
  6. serializeAndReadObject(File envHome, Object object)
  7. serializeAndRecover(Object o)
  8. serializeAndRecover(Object o)
  9. serializeAndReturnDeserializedObject( T object)