Example usage for java.io ObjectInputStream readObject

List of usage examples for java.io ObjectInputStream readObject

Introduction

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

Prototype

public final Object readObject() throws IOException, ClassNotFoundException 

Source Link

Document

Read an object from the ObjectInputStream.

Usage

From source file:com.ikon.util.Serializer.java

/**
 * @param obj//ww  w  .  j av  a2 s.c  o  m
 */
public static Object read(String filename) {
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    Object obj = null;

    try {
        fis = new FileInputStream(Config.HOME_DIR + File.separator + filename + ".ser");
        ois = new ObjectInputStream(fis);
        obj = ois.readObject();
    } catch (FileNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    } catch (ClassNotFoundException e) {
        log.error(e.getMessage());
    } finally {
        IOUtils.closeQuietly(ois);
        IOUtils.closeQuietly(fis);
    }

    return obj;
}

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. j a va  2s . c  o m*/
 * <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:de.zib.gndms.gritserv.delegation.DelegationAux.java

public static EndpointReferenceType extractDelegationEPR(ContextT con) throws Exception {

    ContextTEntry[] entries = con.getEntry();
    ArrayList<ContextTEntry> al = new ArrayList<ContextTEntry>(entries.length);
    EndpointReferenceType epr = null;//w w w .j  av a  2 s.c om

    for (ContextTEntry e : entries) {
        if (e.getKey().equals(DELEGATION_EPR_KEY)) {
            //epr = eprFormXML( e.get_value().toString( ) );
            final String uuepr = e.get_value().toString();
            logger.debug("encoded delegation epr: " + uuepr);

            final Base64 b64 = new Base64(4000, new byte[] {}, true);
            byte[] ba = b64.decode(uuepr);
            ByteArrayInputStream bis = new ByteArrayInputStream(ba);
            ObjectInputStream ois = new ObjectInputStream(bis);
            String eprs = (String) ois.readObject();
            InputSource is = new InputSource(new StringReader(eprs));

            epr = (EndpointReferenceType) ObjectDeserializer.deserialize(is, EndpointReferenceType.class);
        } else
            al.add(e);
    }

    ContextTEntry[] r = al.toArray(new ContextTEntry[al.size()]);

    con.setEntry(r);

    return epr;
}

From source file:com.google.caja.precajole.StaticPrecajoleMap.java

private static Object deserialize(byte[] serial) {
    if (serial == null) {
        return null;
    }/* w  ww .jav a2  s.  c  o m*/
    try {
        ByteArrayInputStream b = new ByteArrayInputStream(serial);
        ObjectInputStream i = new ObjectInputStream(b);
        return i.readObject();
    } catch (IOException e) {
        return null;
    } catch (ClassNotFoundException e) {
        return null;
    }
}

From source file:edu.harvard.i2b2.loinc.BinResourceFromLoincData.java

public static HashMap<String, String> deSerializeLoincCodeToNameMap() throws IOException {

    HashMap<String, String> map = null;
    ObjectInputStream ois = null;
    InputStream fis = null;// w  w  w .  j  a  va  2  s.  c  o m

    try {
        fis = BinResourceFromLoincData.class.getResourceAsStream("/loinc/loincCodeToNameMap.bin");
        ois = new ObjectInputStream(fis);
        map = (HashMap<String, String>) ois.readObject();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        ois.close();
        fis.close();
    }

    return map;
}

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

/**
 * Deserialize a {@link FlatWizardSettings} object from the specified file.
 * /*from ww w. j av  a 2s  .co m*/
 * @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:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * Copys the given Object and returns the copy from the object or null if the object can't be
 * serialized./*w  w w  .  j a  v a  2  s  . c  o  m*/
 *
 * @param <T>
 *            the generic type of the given object
 * @param orig
 *            The object to copy.
 * @return Returns a copy from the original object.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws ClassNotFoundException
 *             is thrown when a class is not found in the classloader or no definition for the
 *             class with the specified name could be found.
 */
@SuppressWarnings("unchecked")
public static <T extends Serializable> T copySerializedObject(final T orig)
        throws IOException, ClassNotFoundException {
    T object = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(orig);
        objectOutputStream.flush();
        objectOutputStream.close();
        final ByteArrayInputStream bis = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        final ObjectInputStream ois = new ObjectInputStream(bis);
        object = (T) ois.readObject();
    } finally {
        StreamExtensions.closeOutputStream(byteArrayOutputStream);
        StreamExtensions.closeOutputStream(objectOutputStream);
    }
    return object;
}

From source file:de.alpharogroup.io.SerializedObjectUtils.java

/**
 * Copys the given Object and returns the copy from the object or null if the object can't be
 * serialized.//from w w w  .j  a v a  2s  . co m
 *
 * @param <T>
 *            the generic type of the given object
 * @param orig
 *            The object to copy.
 * @return Returns a copy from the original object.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws ClassNotFoundException
 *             is thrown when a class is not found in the classloader or no definition for the
 *             class with the specified name could be found.
 */
@SuppressWarnings("unchecked")
public static <T extends Serializable> T copySerializedObject(final T orig)
        throws IOException, ClassNotFoundException {
    T object = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(orig);
        objectOutputStream.flush();
        objectOutputStream.close();
        final ByteArrayInputStream bis = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        final ObjectInputStream ois = new ObjectInputStream(bis);
        object = (T) ois.readObject();
    } finally {
        StreamUtils.closeOutputStream(byteArrayOutputStream);
        StreamUtils.closeOutputStream(objectOutputStream);
    }
    return object;
}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static PublicKey getPublic() {
    if (!verificaChave()) {
        geraChave();//from  ww  w  .ja v  a  2 s  . co  m
    }
    ObjectInputStream inputStream;
    try {
        inputStream = new ObjectInputStream(new FileInputStream(path + PATH_CHAVE_PUBLICA_SERVER));
        return (PublicKey) inputStream.readObject();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static PrivateKey getPrivate() {
    if (!verificaChave()) {
        geraChave();//w  ww.  j  av a2 s . c o  m
    }
    ObjectInputStream inputStream;
    try {
        inputStream = new ObjectInputStream(new FileInputStream(path + PATH_CHAVE_PRIVADA_SERVER));
        return (PrivateKey) inputStream.readObject();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}