Java Serialize serializeUnserialize(Object ob)

Here you can find the source of serializeUnserialize(Object ob)

Description

Serializes and unserializes back an object to test whether it is correctly rebuilt (to be used in unit tests as sanity checks).

License

Open Source License

Parameter

Parameter Description
ob the actual object we want to test

Return

true if the object is serializable.

Declaration

public static Object serializeUnserialize(Object ob) throws Exception 

Method Source Code


//package com.java2s;
/*/*from  w w  w. ja v  a  2s  .c  o m*/
 * Copyright (c) 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id: SerializableHelper.java 28515 2008-01-06 20:37:29Z sfermigier $
 */

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

public class Main {
    /**
     * Serializes and unserializes back an object to test whether it is correctly
     * rebuilt (to be used in unit tests as sanity checks).
     *
     * @param ob the actual object we want to test
     * @return true if the object is serializable.
     */
    public static Object serializeUnserialize(Object ob) throws Exception {
        Serializable in = (Serializable) ob;
        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
        ObjectOutputStream outStream = new ObjectOutputStream(byteOutStream);
        outStream.writeObject(in);
        ByteArrayInputStream byteInStream = new ByteArrayInputStream(byteOutStream.toByteArray());
        ObjectInputStream inStream = new ObjectInputStream(byteInStream);
        return inStream.readObject();
    }
}

Related

  1. serializeToFile(Object obj, String fileName)
  2. serializeToFile(Object object, File file)
  3. serializeToFile(Serializable s, String path)
  4. serializeToString(Object o)
  5. serializeToSurefireDirectory(final Class testClass, final Object object)