Example usage for org.apache.commons.io.input ClassLoaderObjectInputStream readObject

List of usage examples for org.apache.commons.io.input ClassLoaderObjectInputStream readObject

Introduction

In this page you can find the example usage for org.apache.commons.io.input ClassLoaderObjectInputStream readObject.

Prototype

public final Object readObject() throws IOException, ClassNotFoundException 

Source Link

Document

Read an object from the ObjectInputStream.

Usage

From source file:com.dev.pygmy.util.Utils.java

/**
 * Loads a saved game from the device's memory
 * @return saved match if found, else returns null
 *//*w w  w.j a v a  2  s. c  o m*/
public static PygmyGame loadGameHistory(String path) {
    File file = new File(path);
    PygmyGame game = null;

    if (file.exists()) {
        try {
            ClassLoaderObjectInputStream ois = new ClassLoaderObjectInputStream(PygmyLoader.getClassLoader(),
                    new FileInputStream(file));
            game = (PygmyGame) ois.readObject();
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return game;
}

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

/**
 * Deserialized with ClassLoader//from www. j a va 2s  .com
 * 
 * @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.datenwerke.transloader.clone.SerializationCloningStrategy.java

private Object deserialize(byte[] serializedOriginal, ClassLoader targetLoader)
        throws ClassNotFoundException, IOException {
    ByteArrayInputStream byteStream = new ByteArrayInputStream(serializedOriginal);
    ClassLoaderObjectInputStream objectStream = new ClassLoaderObjectInputStream(targetLoader, byteStream);
    return objectStream.readObject();
}

From source file:backtype.storm.serialization.SerializableSerializer.java

@Override
public Object read(Kryo kryo, Input input, Class c) {
    int len = input.readInt();
    byte[] ser = new byte[len];
    input.readBytes(ser);//from   w  w w.  j  a va2s .  c o  m
    ByteArrayInputStream bis = new ByteArrayInputStream(ser);
    try {
        ClassLoaderObjectInputStream ois = new ClassLoaderObjectInputStream(kryo.getClassLoader(), bis);
        return ois.readObject();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:gov.nasa.ensemble.common.CommonUtils.java

/**
 * Copy an object via Externalizable. Copying a set of interconnected objects this way will maintain all the connections. This method will also properly copy and connect loops.
 * //from  ww  w .  ja  v a2  s . co  m
 * @param object
 * @return not supplied
 */
public static <T extends Externalizable> T copy(T object) {
    ClassLoaderObjectInputStream objectInputStream = null;
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(object);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        objectInputStream = new ClassLoaderObjectInputStream(object.getClass().getClassLoader(),
                byteArrayInputStream);
        // ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        @SuppressWarnings("unchecked")
        T copy = (T) objectInputStream.readObject();
        return copy;
    } catch (IOException e) {
        Logger.getLogger(CommonUtils.class).error("copy failed", e);
    } catch (ClassNotFoundException e) {
        Logger.getLogger(CommonUtils.class).error("copy failed", e);
    } finally {
        if (objectInputStream != null) {
            try {
                objectInputStream.close();
            } catch (IOException e) {
                LogUtil.error(e);
            }
        }
    }
    return null;
}

From source file:org.mule.transport.tcp.protocols.CustomClassLoadingLengthProtocol.java

@Override
public Object read(InputStream is) throws IOException {
    byte[] bytes = (byte[]) super.read(is);

    if (bytes == null) {
        return null;
    } else {/*from  w  ww  . java 2 s . co m*/
        ClassLoaderObjectInputStream classLoaderIS = new ClassLoaderObjectInputStream(this.getClassLoader(),
                is);
        try {
            return classLoaderIS.readObject();
        } catch (ClassNotFoundException e) {
            logger.warn(e.getMessage());
            IOException iox = new IOException();
            iox.initCause(e);
            throw iox;
        } finally {
            classLoaderIS.close();
        }
    }
}