Java Is Serializable Object isSerializable(Object ob)

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

Description

Checks if a given object is serializable.

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 boolean isSerializable(Object ob) 

Method Source Code


//package com.java2s;
/*//from  w  w w  .ja va2s .  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 {
    /**
     * Checks if a given object is serializable.
     *
     * @param ob the actual object we want to test
     * @return true if the object is serializable.
     */
    // XXX AT: since class loader isolation, this module is not aware anymore of
    // nuxeo.ear classes => ClassCastException can be thrown is tested object is
    // a DocumentModel for instance.
    public static boolean isSerializable(Object ob) {
        if (!(ob instanceof Serializable)) {
            return false;
        }
        try {
            ob = serializeUnserialize(ob);
            return ob != null;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 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. isSerializable(final Object c)
  2. isSerializable(Object o)
  3. isSerializable(Object o)
  4. isSerializable(Object o)
  5. isSerializable(Object o)
  6. isSerializable(Object obj)
  7. isSerializable(Object obj)
  8. isSerializable(Object object)
  9. isSerializableType(Class type)