Example usage for java.io ObjectInputStream close

List of usage examples for java.io ObjectInputStream close

Introduction

In this page you can find the example usage for java.io ObjectInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the input stream.

Usage

From source file:misc.TestUtils.java

@SuppressWarnings("unchecked")
public static <T extends Serializable> T cloneSerializable(T obj) {
    ObjectOutputStream out = null;
    ObjectInputStream in = null;

    try {/*from w ww.  j a  v  a 2 s .  c  om*/
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        out = new ObjectOutputStream(bout);

        out.writeObject(obj);
        out.close();

        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        in = new ObjectInputStream(bin);
        Object copy = in.readObject();

        in.close();

        return (T) copy;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }

            if (in != null) {
                in.close();
            }
        } catch (IOException ignore) {
        }
    }

    return null;
}

From source file:SerializationUtils.java

/**
 * <p>Deserializes an <code>Object</code> from the specified stream.</p>
 *
 * <p>The stream will be closed once the object is written. This
 * avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.</p>
 * //w  w w .  ja  va  2 s. c om
 * <p>The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.</p>
 *
 * @param inputStream  the serialized object input stream, must not be null
 * @return the deserialized object
 * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
 * @throws SerializationException (runtime) if the serialization fails
 */
public static Object deserialize(InputStream inputStream) {
    if (inputStream == null) {
        throw new IllegalArgumentException("The InputStream must not be null");
    }
    ObjectInputStream in = null;
    try {
        // stream closed in the finally
        in = new ObjectInputStream(inputStream);
        return in.readObject();

    } catch (ClassNotFoundException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
}

From source file:at.ac.tuwien.dsg.comot.m.common.Utils.java

public static Object deepCopy(Object oldObj) throws IOException, ClassNotFoundException {

    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;

    try {//from w w w.  j  a va 2  s  .  c om
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        oos = new ObjectOutputStream(bos);
        oos.writeObject(oldObj);
        oos.flush();

        ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));

        return ois.readObject();

    } finally {
        if (oos != null)
            oos.close();
        if (ois != null)
            ois.close();
    }
}

From source file:de.bps.webservices.clients.onyxreporter.HashMapWrapper.java

/**
 * Deserializes an object from the given Base65 encoded string.
 * //from  www. j  a  va2 s  .  c  o  m
 * @param base64EncodedSerializedObject
 * @return
 * @throws OnyxReporterException
 */
private static final Object deserialize(final String base64EncodedSerializedObject)
        throws OnyxReporterException {
    final ByteArrayInputStream bais = new ByteArrayInputStream(
            Base64.decodeBase64(base64EncodedSerializedObject));
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(bais);
        final Object o = ois.readObject();
        return o;
    } catch (final IOException e) {
        throw new OnyxReporterException("Could not deserialize object!", e);
    } catch (final ClassNotFoundException e) {
        throw new OnyxReporterException("Could not deserialize object!", e);
    } finally {
        try {
            if (ois != null) {
                ois.close();
            }
        } catch (final IOException e) {
        }
    }
}

From source file:com.gdc.nms.web.mibquery.wizard.ciscavate.cjwizard.FlatWizardSettings.java

/**
 * Deserialize a {@link FlatWizardSettings} object from the specified file.
 * // w  w w .  ja va 2s. c  om
 * @param filename
 *           The filename.
 * @return The deserialized {@link FlatWizardSettings}.
 */
static public FlatWizardSettings deserialize(String filename) {
    ObjectInputStream in = null;
    FileInputStream fin = null;
    try {
        fin = new FileInputStream(filename);
        in = new ObjectInputStream(fin);
        return (FlatWizardSettings) in.readObject();
    } catch (IOException ioe) {
        log.error("Error reading settings", ioe);
    } catch (ClassNotFoundException cnfe) {
        log.error("Couldn't instantiate seralized class", cnfe);
    } finally {
        if (null != in) {
            try {
                in.close();
            } catch (IOException ioe) {
                log.error("Error closing inputstream", ioe);
            }
        }
        if (null != fin) {
            try {
                fin.close();
            } catch (IOException ioe) {
                log.error("Error closing file", ioe);
            }
        }
    }
    return null;
}

From source file:com.smartitengineering.jetty.session.replication.impl.hbase.SessionDataObjectConverter.java

private static Object deserialize(InputStream inputStream) {
    if (inputStream == null) {
        throw new IllegalArgumentException("The InputStream must not be null");
    }// w ww.j a va 2s . c  o  m
    ObjectInputStream in = null;
    try {
        // stream closed in the finally
        in = new ClassLoaderObjectInputStream(Thread.currentThread().getContextClassLoader(), inputStream);
        return in.readObject();

    } catch (ClassNotFoundException ex) {
        throw new SerializationException(ex);
    } catch (IOException ex) {
        throw new SerializationException(ex);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
}

From source file:edu.usc.pgroup.floe.utils.Utils.java

/**
 * Deserializer for Zookeeper data. See comments for serialize function.
 *
 * @param serialized serialized byte array (default java serialized)
 *                   obtained from the serialize function.
 * @return the constructed java object. Needs to be typecasted to
 * appropriate object before using. No checks are actionCompleted here.
 *///from  w  w w  .j a  va 2 s  .  c o  m
public static Object deserialize(final byte[] serialized) {
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
        ObjectInputStream ois = new ObjectInputStream(bis);
        Object ret = ois.readObject();
        ois.close();
        return ret;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.nineash.hutsync.client.NetworkUtilities.java

/** Read the object from Base64 string. */
private static Object fromString(String s) throws IOException, ClassNotFoundException {
    byte[] data = Base64Coder.decode(s);
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
    Object o = ois.readObject();/*ww  w.  j  a  v  a  2 s  .co  m*/
    ois.close();
    return o;
}

From source file:com.beetle.framework.util.ObjectUtil.java

/**
 * ?//  w  w  w  . j  a  va 2 s .  c  om
 * 
 * @param bytes
 * @return
 */
public final static Object bytesToObj(byte[] bytes) {
    ByteArrayInputStream bai = null;
    ObjectInputStream ois;
    try {
        bai = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bai);
        Object obj = ois.readObject();
        ois.close();
        ois = null;
        return obj;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (bai != null) {
                bai.close();
                bai = null;
            }
        } catch (IOException e) {
        }
    }
}

From source file:com.clustercontrol.plugin.impl.AsyncTask.java

/**
 * ??BinarySerializable???// w ww . j ava 2 s. c  om
 * @param bytes ??Binary
 * @return Serializable
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static Serializable decodeBinary(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream iaos = null;
    ObjectInputStream ois = null;
    Object obj = null;

    try {
        iaos = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(iaos);

        obj = ois.readObject();
    } finally {
        if (iaos != null) {
            iaos.close();
        }
        if (ois != null) {
            ois.close();
        }
    }

    return (Serializable) obj;
}