maroph icon

Java Object Serialization/Deserialization

maroph | PRO | 10/17/12 11:27:20 AM UTC | 0 ⭐ | 387 👁️ | Never ⏰ | []
Java 5 |

0 B

|

None

|

0 👍

/

0 👎

package sample;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
 
/*
 * A simple class to show how to serialize/deserialize an object instance to/from byte array
 * and how to check whether the deserialized object instance is equal to the object instance
 * that was used for serialization (method checkEqualsAfterSerializeDeserialize)
 */
public class SerialUtil {
    public static byte[] serialize(Serializable serializable) throws IOException {
        if (serializable == null) {
            throw new IOException("serializable == null");
        }
        
        ByteArrayOutputStream baos = null; 
        ObjectOutputStream    oos  = null;
        try {
            baos = new ByteArrayOutputStream();
            oos  = new ObjectOutputStream(baos);
            oos.writeObject(serializable);
            oos.flush();
            baos.flush();
            byte[] ba = baos.toByteArray();
            return ba; 
        }
        catch (IOException ioex) {
            throw ioex;
        }
        catch (Throwable t) {
            throw new IOException(t);
        }
        finally {
            if (oos != null) {
                try { oos.close(); } catch (Exception ex) { /* */ }
            }
            if (baos != null) {
                try { baos.close(); } catch (Exception ex) { /* */ }
            }
        }
    }
    
    
    public static Object deserialize(byte[] baSerializedObject) throws IOException {
        if (baSerializedObject == null) {
            throw new IOException("baSerializedObject == null");
        }
        
        ByteArrayInputStream bais = null; 
        ObjectInputStream    ois  = null; 
        try {
            bais = new ByteArrayInputStream(baSerializedObject); 
            ois  = new ObjectInputStream(bais);
            Object deserializedObject = ois.readObject();
            return deserializedObject;
        }
        catch (IOException ioex) {
            throw ioex;
        }
        catch (Throwable t) {
            throw new IOException(t);
        }
        finally {
            if (ois != null) {
                try { ois.close(); } catch (Exception ex) { /* */ }
            }
            if (bais != null) {
                try { bais.close(); } catch (Exception ex) { /* */ }
            }
        }
    }
    
 
    public static boolean checkEqualsAfterSerializeDeserialize(Serializable serializable) throws IOException {
        if (serializable == null) {
            throw new IOException("serializable == null");
        }
        
        byte[] ba = serialize(serializable);
        Object deserializedObject = deserialize(ba);
        if (deserializedObject == null) {
            return false;
        }
        if (!serializable.getClass().isAssignableFrom(deserializedObject.getClass())) {
            return false;
        }
        
        boolean bret = deserializedObject.getClass().cast(serializable).equals(serializable);
        return bret;
    }
}

Comments