Java Is Serializable Object isSerializable(Object obj)

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

Description

Verify that an object is Serializable by attempting to serialize it.

License

Open Source License

Parameter

Parameter Description
obj An object which needs to be serialized

Return

true if the object is Serializable, false otherwise.

Declaration

public static boolean isSerializable(Object obj) 

Method Source Code

//package com.java2s;
/*//from   ww w.ja v a  2s.  c  o m
VisAD system for interactive analysis and visualization of numerical
data.  Copyright (C) 1996 - 2011 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.
    
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/

public class Main {
    /**
     * Verify that an object is Serializable by attempting to
     * serialize it.
     *
     * @param obj An object which needs to be serialized
     *
     * @return <tt>true</tt> if the object is Serializable, false otherwise.
     */
    public static boolean isSerializable(Object obj) {
        return isSerializable(obj, false);
    }

    /**
     * Verify that an object is Serializable by attempting to
     * serialize it.
     *
     * @param obj An object which needs to be serialized
     * @param printStackTraces <tt>true</tt> if the stack trace for
     *                         any exception should be printed.
     *
     * @return <tt>true</tt> if the object is Serializable, false otherwise.
     */
    public static boolean isSerializable(Object obj, boolean printStackTraces) {
        java.io.ByteArrayOutputStream outBytes;
        outBytes = new java.io.ByteArrayOutputStream();

        java.io.ObjectOutputStream outStream;
        try {
            outStream = new java.io.ObjectOutputStream(outBytes);
        } catch (java.io.IOException ioe) {
            if (printStackTraces)
                ioe.printStackTrace();
            return false;
        }

        try {
            outStream.writeObject(obj);
            outStream.flush();
        } catch (java.io.IOException ioe) {
            if (printStackTraces)
                ioe.printStackTrace();
            return false;
        }

        java.io.ByteArrayInputStream inBytes;
        inBytes = new java.io.ByteArrayInputStream(outBytes.toByteArray());

        try {
            outStream.close();
            outBytes.close();
        } catch (java.io.IOException ioe) {
            if (printStackTraces)
                ioe.printStackTrace();
            return false;
        }

        java.io.ObjectInputStream inStream;
        try {
            inStream = new java.io.ObjectInputStream(inBytes);
        } catch (java.io.IOException ioe) {
            if (printStackTraces)
                ioe.printStackTrace();
            return false;
        }

        Object obj2;
        try {
            obj2 = inStream.readObject();
        } catch (java.io.IOException ioe) {
            if (printStackTraces)
                ioe.printStackTrace();
            return false;
        } catch (ClassNotFoundException cnfe) {
            if (printStackTraces)
                cnfe.printStackTrace();
            return false;
        }

        try {
            inStream.close();
            inBytes.close();
        } catch (java.io.IOException ioe) {
            if (printStackTraces)
                ioe.printStackTrace();
            return false;
        }

        return true;
    }
}

Related

  1. isSerializable(Object o)
  2. isSerializable(Object o)
  3. isSerializable(Object o)
  4. isSerializable(Object ob)
  5. isSerializable(Object obj)
  6. isSerializable(Object object)
  7. isSerializableType(Class type)