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.exalttech.trex.util.Util.java

/**
 * De-serialize string to object/*from  ww w  .ja va2 s  .c o m*/
 *
 * @param serializedStriing
 * @return
 */
public static Object deserializeStringToObject(String serializedStriing) {
    try {
        byte[] data = Base64.getDecoder().decode(serializedStriing);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
        Object o = ois.readObject();
        ois.close();
        return o;
    } catch (IOException | ClassNotFoundException ex) {
        LOG.error("Error deserializing string to object", ex);
        return null;
    }
}

From source file:MSUmpire.DIA.TargetMatchScoring.java

public static TargetMatchScoring LibraryMatchReadJS(String Filename, String LibID)
        throws FileNotFoundException {

    if (!new File(FilenameUtils.getFullPath(Filename) + FilenameUtils.getBaseName(Filename) + "_" + LibID
            + "_LibMatch.serFS").exists()) {
        return null;
    }//from   ww w.jav a 2 s  . c  o  m
    TargetMatchScoring match = null;
    try {
        Logger.getRootLogger()
                .info("Loading Target library match results to file:" + FilenameUtils.getFullPath(Filename)
                        + FilenameUtils.getBaseName(Filename) + "_" + LibID + "_LibMatch.ser...");
        FileInputStream fileIn = new FileInputStream(FilenameUtils.getFullPath(Filename)
                + FilenameUtils.getBaseName(Filename) + "_" + LibID + "_LibMatch.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        match = (TargetMatchScoring) in.readObject();
        in.close();
        fileIn.close();
    } catch (Exception ex) {
        Logger.getRootLogger().error(ExceptionUtils.getStackTrace(ex));
        return null;
    }
    return match;
}

From source file:backtype.storm.utils.Utils.java

/**
 * Deserialized with ClassLoader/* w ww. jav  a2s.co  m*/
 * 
 * @param serialized
 * @param loader
 * @return
 */
public static Object javaDeserializeWithCL(byte[] serialized, URLClassLoader loader) {
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
        Object ret = null;
        if (loader != null) {
            ClassLoaderObjectInputStream cis = new ClassLoaderObjectInputStream(loader, bis);
            ret = cis.readObject();
            cis.close();
        } else {
            ObjectInputStream ois = new ObjectInputStream(bis);
            ret = ois.readObject();
            ois.close();
        }
        return ret;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sf.jasperreports.engine.util.JRLoader.java

/**
 *
 *///from  w w  w  .java 2s.  c o  m
public static Object loadObject(JasperReportsContext jasperReportsContext, URL url) throws JRException {
    Object obj = null;

    InputStream is = null;
    ObjectInputStream ois = null;

    try {
        is = url.openStream();
        ois = new ContextClassLoaderObjectInputStream(jasperReportsContext, is);
        obj = ois.readObject();
    } catch (IOException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_OBJECT_FROM_URL_LOADING_ERROR, new Object[] { url }, e);
    } catch (ClassNotFoundException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_CLASS_NOT_FOUND_FROM_URL, new Object[] { url }, e);
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
            }
        }

        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }

    return obj;
}

From source file:gov.nih.nci.caarray.util.CaArrayUtils.java

/**
 * Deserializes an object from a gzipped serialized representation.
 * // w  ww  .  j a va 2  s.co m
 * @param is the InputStream which will produce the gzipped serialized representation of an object
 * @return the deserialized object
 */
public static Serializable deserialize(InputStream is) {
    GZIPInputStream gzipInputStream = null;
    ObjectInputStream objectInputStream = null;
    Serializable object = null;
    try {
        gzipInputStream = new GZIPInputStream(is);
        objectInputStream = new ObjectInputStream(gzipInputStream);
        object = (Serializable) objectInputStream.readObject();
    } catch (final IOException e) {
        throw new IllegalStateException("Couldn't read object", e);
    } catch (final ClassNotFoundException e) {
        throw new IllegalStateException("Couldn't read object", e);
    } finally {
        IOUtils.closeQuietly(objectInputStream);
        IOUtils.closeQuietly(gzipInputStream);
    }
    return object;
}

From source file:Base64.java

/**
 * Attempts to decode Base64 data and deserialize a Java
 * Object within. Returns <tt>null</tt> if there was an error.
 *
 * @param encodedObject The Base64 data to decode
 * @return The decoded and deserialized object
 * @since 1.4//w w  w . j a  v a  2 s  . co  m
 */
public static Object decodeToObject(String encodedObject) {

    byte[] objBytes = decode(encodedObject);

    java.io.ByteArrayInputStream bais = null;
    java.io.ObjectInputStream ois = null;

    try {
        bais = new java.io.ByteArrayInputStream(objBytes);
        ois = new java.io.ObjectInputStream(bais);

        return ois.readObject();
    } // end try
    catch (java.io.IOException e) {

        e.printStackTrace();
        return null;
    } // end catch
    catch (ClassNotFoundException e) {

        e.printStackTrace();
        return null;
    } // end catch
    finally {
        try {
            bais.close();
            ois.close();
        } catch (Exception e) {
        }
    } // end finally
}

From source file:com.heliosapm.tsdblite.json.JSON.java

private static Object jdeserialize(final byte[] bytes) {
    if (bytes == null || bytes.length == 0)
        return null;
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {/*  w w  w.j  av a  2s  .c  o  m*/
        bais = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        if (ois != null)
            try {
                ois.close();
            } catch (Exception x) {
                /* No Op */}
        if (bais != null)
            try {
                bais.close();
            } catch (Exception x) {
                /* No Op */}
    }
}

From source file:me.StevenLawson.TotalFreedomMod.TFM_Util.java

@SuppressWarnings("unchecked")
public static Map<String, Boolean> getSavedFlags() {
    Map<String, Boolean> flags = null;

    File input = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SAVED_FLAGS_FILENAME);
    if (input.exists()) {
        try {/*  ww w . j  ava2s  . co  m*/
            FileInputStream fis = new FileInputStream(input);
            ObjectInputStream ois = new ObjectInputStream(fis);
            flags = (HashMap<String, Boolean>) ois.readObject();
            ois.close();
            fis.close();
        } catch (Exception ex) {
            TFM_Log.severe(ex);
        }
    }

    return flags;
}

From source file:com.bitranger.parknshop.util.ObjUtils.java

public static Object fromBytes(byte[] plainObj) {

    Object var = null;
    ByteArrayInputStream baIS = new ByteArrayInputStream(plainObj);
    ObjectInputStream objIS = null;
    try {//  ww w . j a  v  a  2 s .c o  m
        objIS = new ObjectInputStream(baIS);
        var = objIS.readObject();

    } catch (Exception e) {
        throw new RuntimeException(e.getMessage() + "\n\tCAUSE: " + e.getCause(), e);
    } finally {
        try {
            baIS.close();
            if (objIS != null) {
                objIS.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage() + "\n\tCAUSE: " + e.getCause(), e);
        }
    }
    return var;
}

From source file:net.sf.jasperreports.engine.util.JRLoader.java

/**
 *
 *//* w  ww .  j av a2s  . c  om*/
public static Object loadObject(JasperReportsContext jasperReportsContext, File file) throws JRException {
    if (!file.exists() || !file.isFile()) {
        throw new JRException(new FileNotFoundException(String.valueOf(file)));
    }

    Object obj = null;

    FileInputStream fis = null;
    ObjectInputStream ois = null;

    try {
        fis = new FileInputStream(file);
        BufferedInputStream bufferedIn = new BufferedInputStream(fis);
        ois = new ContextClassLoaderObjectInputStream(jasperReportsContext, bufferedIn);
        obj = ois.readObject();
    } catch (IOException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_OBJECT_FROM_FILE_LOADING_ERROR, new Object[] { file }, e);
    } catch (ClassNotFoundException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_CLASS_NOT_FOUND_FROM_FILE, new Object[] { file }, e);
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
            }
        }

        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }

    return obj;
}