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:Main.java

/**
 * Decompresses the given byte[] and returns the Object.
 * //from   w  ww . j  ava  2  s  .c  o m
 * @param <T> Type of expected Object
 * @param bytes compressed byte[]
 * @param bufferSize size of buffer to be used (in bytes)
 * 
 * @return decompressed Object
 * 
 * @throws IOException if failed to decompress
 * @throws ClassCastException if cannot cast to specified type
 */
@SuppressWarnings("unchecked")
/* Ignore Unchecked Cast Warning */
public static <T> T decompress(byte[] bytes, int bufferSize) throws IOException, ClassCastException {

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferSize);
        Inflater inflater = new Inflater();
        inflater.setInput(bytes);

        // Decompress
        byte[] buf = new byte[bufferSize];
        while (!inflater.finished()) {
            int count = inflater.inflate(buf);
            bos.write(buf, 0, count);
        }
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));

        return (T) ois.readObject();
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:Main.java

/**
 * deserialization from file// ww  w  . j a  v  a2s . c  o  m
 * 
 * @param filePath
 * @return
 * @throws RuntimeException if an error occurs
 */
public static Object deserialization(String filePath) {
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new FileInputStream(filePath));
        Object o = in.readObject();
        in.close();
        return o;
    } catch (FileNotFoundException e) {
        throw new RuntimeException("FileNotFoundException occurred. ", e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("ClassNotFoundException occurred. ", e);
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:asia.stampy.common.serialization.SerializationUtils.java

/**
 * Deserialize base64.//from   w  w w.j  a v  a  2 s.c  om
 * 
 * @param s
 *          the s
 * @return the object
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 * @throws ClassNotFoundException
 *           the class not found exception
 */
public static Object deserializeBase64(String s) throws IOException, ClassNotFoundException {
    DESERIALIZE_LOCK.lock();
    try {
        byte[] bytes = Base64.decodeBase64(s);

        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));

        return ois.readObject();
    } finally {
        DESERIALIZE_LOCK.unlock();
    }
}

From source file:com.koda.common.lcm.Tool.java

public static Object undoObject(String obj) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(undoStuff(obj));
    ObjectInputStream ois = new ObjectInputStream(is);
    try {//from w ww  .  ja  va  2 s.  c o  m
        return ois.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    }
}

From source file:org.terracotta.management.cli.rest.HttpServices.java

private static Object load(String filename) throws IOException, ClassNotFoundException {
    File path = new File(System.getProperty("user.home"), ".tc/mgmt");
    File file = new File(path, filename);

    if (!file.exists()) {
        return null;
    }//from w ww. j  av  a2 s  .  c o  m

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
    try {
        return ois.readObject();
    } finally {
        ois.close();
    }
}

From source file:com.bworld.manager.ObjectSerializer.java

/**
 * Deserialize./* w  w w . j a v a 2 s  .  com*/
 *
 * @param str the str
 * @return the object
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Object deserialize(String str) throws IOException {
    if (str == null || str.length() == 0)
        return null;
    try {
        ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
        ObjectInputStream objStream = new ObjectInputStream(serialObj);
        return objStream.readObject();
    } catch (Exception e) {
        //   throw WrappedIOException.wrap("Deserialization error: " + e.getMessage(), e);
    }
    return str;
}

From source file:MSUmpire.SpectrumParser.DIA_Setting.java

public static DIA_Setting ReadDIASettingSerialization(String filepath) {
    if (!new File(FilenameUtils.getFullPath(filepath) + FilenameUtils.getBaseName(filepath) + "_diasetting.ser")
            .exists()) {//from   www  . j  a v  a  2 s . c o m
        return null;
    }
    try {
        Logger.getRootLogger().debug("Reading DIA setting from file:" + FilenameUtils.getFullPath(filepath)
                + FilenameUtils.getBaseName(filepath) + "_diasetting.ser...");

        FileInputStream fileIn = new FileInputStream(
                FilenameUtils.getFullPath(filepath) + FilenameUtils.getBaseName(filepath) + "_diasetting.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        DIA_Setting setting = (DIA_Setting) in.readObject();
        in.close();
        fileIn.close();
        return setting;

    } catch (Exception ex) {
        Logger.getRootLogger().error(ExceptionUtils.getStackTrace(ex));
        return null;
    }
}

From source file:edu.cmu.cs.lti.ark.util.SerializedObjects.java

@SuppressWarnings("unchecked")
public static <T> T readObject(InputSupplier<ObjectInputStream> inputSupplier)
        throws IOException, ClassNotFoundException {
    final ObjectInputStream input = inputSupplier.getInput();
    try {/*  w  w w. j  a  va 2s.  c  om*/
        return (T) input.readObject();
    } finally {
        closeQuietly(input);
    }
}

From source file:Main.java

public static Object readObject(String path, int fileStatus) {
    if (!TextUtils.isEmpty(path) && fileStatus == FILE_EXISTS) {
        try {/*from   w  ww.  j  av  a 2 s  . c  o  m*/
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
            Object mObject = in.readObject();
            in.close();
            return mObject;
        } catch (Exception e) {

        }
    }
    return null;
}

From source file:jp.queuelinker.system.util.SerializeUtil.java

/**
 * @param file//from w  w  w  . ja v  a  2  s.com
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static Object restoreObject(final File file) throws IOException, ClassNotFoundException {
    ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
    return input.readObject();
}