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.usf.cutr.siri.util.SiriUtils.java

/**
 * Read the given object from Android internal storage for this app
 * //  w w  w  .  ja  v  a 2s  . com
 * @param objectType
 *            object type, defined by class constant Strings, to retrieve
 *            from cache (ObjectReader, ObjectMapper, or XmlReader)
 * 
 * @return deserialized Object, or null if object couldn't be deserialized
 */
public static synchronized Serializable readFromCache(String objectType) {

    FileInputStream fileStream = null;
    ObjectInputStream objectStream = null;

    // Holds object to be read from cache
    Serializable object = null;

    try {
        String fileName = objectType + CACHE_FILE_EXTENSION;

        File file = new File(fileName);

        cacheReadStartTime = System.nanoTime();
        fileStream = new FileInputStream(file);
        objectStream = new ObjectInputStream(fileStream);
        object = (Serializable) objectStream.readObject();
        cacheReadEndTime = System.nanoTime();

        // Get size of serialized object
        long fileSize = file.length();

        System.out.println("Read " + fileName + " from cache (" + fileSize + " bytes) in "
                + df.format(getLastCacheReadTime() / 1000000.0) + " ms.");
    } catch (FileNotFoundException e) {
        System.out
                .println("Cache miss - Jackson object '" + objectType + "' does not exist in app cache: " + e);
        return null;
    } catch (Exception e) {
        // Reset timestamps to show there was an error
        cacheReadStartTime = 0;
        cacheReadEndTime = 0;
        System.out.println("Couldn't read Jackson object '" + objectType + "' from cache: " + e);
    } finally {
        try {
            if (objectStream != null) {
                objectStream.close();
            }
            if (fileStream != null) {
                fileStream.close();
            }
        } catch (Exception e) {
            System.out.println("Error closing cache file connections: " + e);
        }
    }

    return object;
}

From source file:cerrla.LocalCrossEntropyDistribution.java

/**
 * Loads a serialised {@link LocalCrossEntropyDistribution} from file (if it
 * exists)./*  w  w w . j av a2s  . co m*/
 * 
 * @param serializedFile
 *            The serialised file.
 * @return The loaded distribution, or null.
 */
public static LocalCrossEntropyDistribution loadDistribution(File serializedFile) {
    try {
        if (serializedFile.exists()) {
            // The file exists!
            FileInputStream fis = new FileInputStream(serializedFile);
            ObjectInputStream ois = new ObjectInputStream(fis);
            LocalCrossEntropyDistribution lced = (LocalCrossEntropyDistribution) ois.readObject();
            ois.close();
            fis.close();

            // Load Local Agent Observations
            lced.localAgentObservations_ = LocalAgentObservations.loadAgentObservations(lced.getGoalCondition(),
                    lced);
            lced.policyGenerator_.rebuildCurrentData();
            lced.isSpecialising_ = true;
            lced.setState(AlgorithmState.TRAINING);

            return lced;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:MainClass.java

private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    stream.defaultReadObject();//from www  .  ja  va 2  s.  c  om
    b = (String) stream.readObject();
}

From source file:SerialCloneTest.java

public Object clone() {
    try {/*from ww w .  j  a v a 2 s .  c  o m*/
        // save the object to a byte array
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(this);
        out.close();

        // read a clone of the object from the byte array
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bin);
        Object ret = in.readObject();
        in.close();

        return ret;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.pcms.common.Common.java

public <T extends Serializable> T clone(T obj) {
    T clonedObj = null;// w w w  . ja va 2s.c  o m
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(stream);

        out.writeObject(obj);
        out.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(stream.toByteArray());
        ObjectInputStream os = new ObjectInputStream(bais);
        clonedObj = (T) os.readObject();
        stream.close();
    } catch (IOException e) {
        _log.error(e.getMessage());
    } catch (ClassNotFoundException e) {
        _log.error(e.getMessage());
    }
    return clonedObj;
}

From source file:foam.dao.index.PersistedIndex.java

@Override
public Object unwrap(Object state) {
    synchronized (file_) {
        try {//  w w w  .  j a  va 2s . c o  m
            long position = (long) state;
            fis_.getChannel().position(position);
            ObjectInputStream iis = new ObjectInputStream(fis_);
            return iis.readObject();
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }
}

From source file:com.github.tell.codec.Base64Serializer.java

public Serializable decode(final byte[] encoded) throws IOException, ClassNotFoundException {
    final byte[] decoded = Base64.decodeBase64(encoded);
    final ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
    final ObjectInputStream ois = new ObjectInputStream(bais);
    return (Serializable) ois.readObject();
}

From source file:com.github.tell.codec.Base64Serializer.java

public Serializable decodeFromString(final String encoded) throws IOException, ClassNotFoundException {
    final byte[] decoded = Base64.decodeBase64(encoded);
    final ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
    final ObjectInputStream ois = new ObjectInputStream(bais);
    return (Serializable) ois.readObject();
}

From source file:MLModelUsageSample.java

/**
 * Deserialize to MLModel object//from  ww w  . j a  v  a  2 s  .c  o  m
 * @return A {@link org.wso2.carbon.ml.commons.domain.MLModel} object
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws URISyntaxException
 */
private MLModel deserializeMLModel(String pathToDownloadedModel)
        throws IOException, ClassNotFoundException, URISyntaxException {

    FileInputStream fileInputStream = new FileInputStream(pathToDownloadedModel);
    ObjectInputStream in = new ObjectInputStream(fileInputStream);
    MLModel mlModel = (MLModel) in.readObject();

    logger.info("Algorithm Type : " + mlModel.getAlgorithmClass());
    logger.info("Algorithm Name : " + mlModel.getAlgorithmName());
    logger.info("Response Variable : " + mlModel.getResponseVariable());
    logger.info("Features : " + mlModel.getFeatures());
    return mlModel;
}

From source file:org.sample.readerwriter.MyReader.java

@Override
public MyObject readFrom(Class<MyObject> type, Type type1, Annotation[] antns, MediaType mt,
        MultivaluedMap<String, String> mm, InputStream in) throws IOException, WebApplicationException {
    try {//from  ww  w .j a v  a  2 s  .  c  o m
        ObjectInputStream ois = new ObjectInputStream(in);
        return (MyObject) ois.readObject();
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(MyReader.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}