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:edu.harvard.i2b2.Icd9.BinResourceFromIcd9Data.java

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

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

    try {
        fis = BinResourceFromIcd9Data.class.getResourceAsStream("/icd9/Icd9CodeToNameMap.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.izforge.izpack.panels.target.TargetPanelHelper.java

/**
 * Determines if there is IzPack installation information at the specified path that is incompatible with the
 * current version of IzPack./*from   w w  w.  j  a  v a 2 s . c om*/
 * <p/>
 * To be incompatible, the file {@link InstallData#INSTALLATION_INFORMATION} must exist in the supplied directory,
 * and not contain recognised {@link Pack} instances.
 *
 * @param dir the path to check
 * @param readInstallationInformation check .installationinformation file or skip it
 * @return {@code true} if there is incompatible installation information,
 *         {@code false} if there is no installation info, or it is compatible
 */
@SuppressWarnings("unchecked")
public static boolean isIncompatibleInstallation(String dir, Boolean readInstallationInformation) {
    boolean result = false;
    File file = new File(dir, InstallData.INSTALLATION_INFORMATION);
    if (file.exists() && readInstallationInformation) {
        FileInputStream input = null;
        ObjectInputStream objectInput = null;
        try {
            input = new FileInputStream(file);
            objectInput = new ObjectInputStream(input);
            List<Object> packs = (List<Object>) objectInput.readObject();
            for (Object pack : packs) {
                if (!(pack instanceof Pack)) {
                    return true;
                }
            }
        } catch (Throwable exception) {
            logger.log(Level.FINE,
                    "Installation information at path=" + file.getPath() + " failed to deserialize", exception);
            result = true;
        } finally {
            IOUtils.closeQuietly(objectInput);
            IOUtils.closeQuietly(input);
        }
    }

    return result;
}

From source file:net.sf.taverna.t2.activities.wsdlsir.T2WSDLSOAPInvoker.java

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

From source file:org.socialhistoryservices.security.MongoTokenStore.java

private static <T> T deserialize(byte[] byteArray) {
    ObjectInputStream oip = null;
    try {// ww  w .  ja  v  a  2  s.  co  m
        oip = new ObjectInputStream(new ByteArrayInputStream(byteArray));
        return (T) oip.readObject();
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException(e);
    } finally {
        if (oip != null) {
            try {
                oip.close();
            } catch (IOException e) {
                // eat it
            }
        }
    }
}

From source file:com.asual.summer.sample.domain.Technology.java

public static Image findImage(String value) {
    if (value != null) {
        try {/*from  w ww. j  a v  a2 s.  c o  m*/
            File file = new File(new File(System.getProperty("java.io.tmpdir")), value);
            if (file.exists()) {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                return (Image) ois.readObject();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return null;
}

From source file:com.openteach.diamond.network.waverider.master.MasterState.java

public static MasterState fromByteBuffer(ByteBuffer buffer) {
    ByteArrayInputStream bin = null;
    ObjectInputStream oin = null;
    try {/*from  www  .  ja  v  a  2  s. com*/
        bin = new ByteArrayInputStream(buffer.array(), Packet.getHeaderSize() + Command.getHeaderSize(),
                buffer.remaining());
        oin = new ObjectInputStream(bin);
        return (MasterState) oin.readObject();
    } catch (IOException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } finally {
        try {
            if (oin != null) {
                oin.close();
            }
            if (bin != null) {
                bin.close();
            }
        } catch (IOException e) {
            logger.error(e);
        }
    }
}

From source file:net.myrrix.common.io.IOUtils.java

/**
 * @return object of type T that was serialized into the given file
 *//*w  w w . ja  v  a 2 s.  c o m*/
public static <T extends Serializable> T readObjectFromFile(File f, Class<T> clazz) throws IOException {
    ObjectInputStream in = new ObjectInputStream(openMaybeDecompressing(f));
    try {
        @SuppressWarnings("unchecked")
        T result = (T) in.readObject();
        return result;
    } catch (ClassNotFoundException cnfe) {
        throw new IllegalStateException(cnfe);
    } finally {
        in.close();
    }
}

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

/**
 * Reads the object from the given file.
 *
 * @param file/*from   w  w w  .  j  a v  a 2  s.c o m*/
 *            In that file is the object saved.
 * @return The object in the file or null.
 * @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.
 */
public static Object readSerializedObjectFromFile(final File file) throws IOException, ClassNotFoundException {
    Object object = null;
    FileInputStream fis = null;
    ObjectInputStream in = null;
    try {
        fis = new FileInputStream(file);
        in = new ObjectInputStream(fis);
        object = in.readObject();
        in.close();
    } finally {
        StreamExtensions.closeInputStream(in);
        StreamExtensions.closeInputStream(fis);
    }
    return object;
}

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

/**
 * Reads the object from the given file.
 *
 * @param file/*ww w.  ja  va  2  s.c o  m*/
 *            In that file is the object saved.
 * @return The object in the file or null.
 * @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.
 */
public static Object readSerializedObjectFromFile(final File file) throws IOException, ClassNotFoundException {
    Object object = null;
    FileInputStream fis = null;
    ObjectInputStream in = null;
    try {
        fis = new FileInputStream(file);
        in = new ObjectInputStream(fis);
        object = in.readObject();
        in.close();
    } finally {
        StreamUtils.closeInputStream(in);
        StreamUtils.closeInputStream(fis);
    }
    return object;
}

From source file:Main.java

public static Object clone(Object object) throws Exception {
    Object copy = null;//  w ww.  j  a v a 2s  .  c  o m

    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        oos.flush();

        ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
        ois = new ObjectInputStream(bin);
        copy = ois.readObject();
    } finally {
        if (ois != null)
            ois.close();
        if (oos != null)
            oos.close();
    }
    return copy;
}