Java InputStream Deserialize to Object deserialize(InputStream in, Class cls)

Here you can find the source of deserialize(InputStream in, Class cls)

Description

Deserializes the object contained in the given input stream.

License

LGPL

Parameter

Parameter Description
in The input stream to deserialize an object from.
cls The expected class of the deserialized object.

Declaration

static public Object deserialize(InputStream in, Class cls) throws IOException 

Method Source Code


//package com.java2s;
/*//w  w  w .ja v  a2s  .  c om
 * 11/19/04      1.0 moved to LGPL.
 * 12/12/99      Initial version.   mdm@techie.com
 *-----------------------------------------------------------------------
 *   This program 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 program 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 program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------
 */

import java.io.*;

public class Main {
    /**
     * Deserializes the object contained in the given input stream.
     * @param in   The input stream to deserialize an object from.
     * @param cls   The expected class of the deserialized object. 
     */
    static public Object deserialize(InputStream in, Class cls) throws IOException {
        if (cls == null)
            throw new NullPointerException("cls");

        Object obj = deserialize(in, cls);
        if (!cls.isInstance(obj)) {
            throw new InvalidObjectException("type of deserialized instance not of required class.");
        }

        return obj;
    }

    /**
     * Deserializes an object from the given <code>InputStream</code>.
     * The deserialization is delegated to an <code>
     * ObjectInputStream</code> instance. 
     * 
     * @param in   The <code>InputStream</code> to deserialize an object
     *            from.
     * 
     * @return The object deserialized from the stream. 
     * @exception IOException is thrown if there was a problem reading
     *      the underlying stream, or an object could not be deserialized
     *      from the stream.
     * 
     * @see java.io.ObjectInputStream
     */
    static public Object deserialize(InputStream in) throws IOException {
        if (in == null)
            throw new NullPointerException("in");

        ObjectInputStream objIn = new ObjectInputStream(in);

        Object obj;

        try {
            obj = objIn.readObject();
        } catch (ClassNotFoundException ex) {
            throw new InvalidClassException(ex.toString());
        }

        return obj;
    }
}

Related

  1. deserialize(InputStream in)
  2. deserialize(InputStream in)
  3. deserialize(InputStream inputStream)
  4. deserialize(InputStream inputStream)
  5. deserialize(InputStream inputStream)
  6. deserialize(InputStream is)