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.dragome.callbackevictor.serverside.utils.ReflectionUtils.java

public static Object cast(Object o) throws IOException, ClassNotFoundException {
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(buffer);
    oos.writeObject(o);/*from w  w w.  j  a  v a  2  s . com*/
    oos.flush();
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    return ois.readObject();
}

From source file:Main.java

public static Object getBeanFromFile(String filePath) {
    Object bean = null;/* w w w  .j  a v  a2s. c o m*/
    File file = new File(filePath);
    ObjectInputStream inputStream = null;
    if (!file.exists()) {
        return bean;
    }
    try {
        inputStream = new ObjectInputStream(new FileInputStream(file));
        bean = inputStream.readObject();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        closeQuiltly(inputStream);
    }

    return bean;
}

From source file:io.smartspaces.scheduling.quartz.orientdb.internal.util.SerialUtils.java

private static Map<String, ?> stringMapFromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    @SuppressWarnings("unchecked")
    Map<String, ?> map = (Map<String, ?>) ois.readObject();
    ois.close();//from ww w  .  j ava  2 s  .  c o m
    return map;
}

From source file:SerialIntList.java

/**
 * Use object serialization to make a "deep clone" of the object o. This
 * method serializes o and all objects it refers to, and then deserializes
 * that graph of objects, which means that everything is copied. This differs
 * from the clone() method of an object which is usually implemented to
 * produce a "shallow" clone that copies references to other objects, instead
 * of copying all referenced objects./*from  w  w  w.jav a2s  .  c om*/
 */
static Object deepclone(final Serializable o) throws IOException, ClassNotFoundException {
    // Create a connected pair of "piped" streams.
    // We'll write bytes to one, and them from the other one.
    final PipedOutputStream pipeout = new PipedOutputStream();
    PipedInputStream pipein = new PipedInputStream(pipeout);

    // Now define an independent thread to serialize the object and write
    // its bytes to the PipedOutputStream
    Thread writer = new Thread() {
        public void run() {
            ObjectOutputStream out = null;
            try {
                out = new ObjectOutputStream(pipeout);
                out.writeObject(o);
            } catch (IOException e) {
            } finally {
                try {
                    out.close();
                } catch (Exception e) {
                }
            }
        }
    };
    writer.start(); // Make the thread start serializing and writing

    // Meanwhile, in this thread, read and deserialize from the piped
    // input stream. The resulting object is a deep clone of the original.
    ObjectInputStream in = new ObjectInputStream(pipein);
    return in.readObject();
}

From source file:android.databinding.tool.util.GenerationalClassUtil.java

private static Serializable fromInputStream(InputStream inputStream)
        throws IOException, ClassNotFoundException {
    ObjectInputStream in = new ObjectInputStream(inputStream);
    return (Serializable) in.readObject();

}

From source file:com.tempescope.wunderground.WeatherLocationManager.java

public static WeatherLocationManager getManager(File file) {
    if (!file.exists()) {
        WeatherLocationManager manager = new WeatherLocationManager();
        manager.file = file;/*from   w  ww. jav  a  2s .  co m*/
        return manager;
    }
    try {
        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
        WeatherLocationManager manager = (WeatherLocationManager) in.readObject();
        in.close();
        manager.file = file;
        return manager;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:com.mnt.base.util.SerializeUtil.java

public static Object deSerialize(byte[] source) {

    Object result;/*from   w ww. jav a 2  s.  co m*/

    if (CommonUtil.isEmpty(source)) {
        result = null;
    } else {
        ByteArrayInputStream bais = new ByteArrayInputStream(source);
        try {
            ObjectInputStream ois = new ObjectInputStream(bais);
            result = ois.readObject();
            ois.close();
        } catch (Exception e) {
            log.error("error while open byte array input stream.", e);
            result = null;
        }
    }

    return result;
}

From source file:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java

public static Key readKeyFromFile(String keyFileName, boolean publ) throws Exception {
    InputStream in = ResourceUtil.getResourceAsStream(keyFileName, RSAEncoder.class.getClassLoader());
    ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
    try {//from  ww w .  ja v a2 s.  co  m
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        KeySpec keySpec;
        if (publ) {
            keySpec = new RSAPublicKeySpec(m, e);
        } else {
            keySpec = new RSAPrivateKeySpec(m, e);
        }
        KeyFactory fact = KeyFactory.getInstance("RSA");
        if (publ) {
            PublicKey pubKey = fact.generatePublic(keySpec);
            return pubKey;
        } else {
            PrivateKey privKey = fact.generatePrivate(keySpec);
            return privKey;
        }
    } catch (Exception e) {
        throw new RuntimeException("Error reading key from file", e);
    } finally {
        oin.close();
    }
}

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

public static Object readSerializedObject(String inputFile) {
    ObjectInputStream input = null;
    Object recoveredObject = null;
    try {//from w w  w.j  av a2s. c om
        input = getObjectInputStream(inputFile);
        recoveredObject = input.readObject();
    } catch (Exception ex) {
        // TODO: NONONONONO! stop swallowing errors!
        ex.printStackTrace();
    } finally {
        closeQuietly(input);
    }
    return recoveredObject;
}

From source file:com.hp.saas.agm.rest.client.SessionContext.java

public static SessionContext load(File sourceFile) {
    ObjectInputStream in = null;
    try {/*w  w w . j  a v a 2 s  .c om*/
        in = new ObjectInputStream(new FileInputStream(sourceFile));
        return (SessionContext) in.readObject();
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (InvalidClassException e) {
        throw new InvalidFormatException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                logger.log(Level.SEVERE, "IOException thrown while closing stream.", e);
            }
        }
    }
}