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

public static void RestoreSharedPrefencesFromStream(ObjectInputStream input, Editor editor)
        throws IOException, ClassNotFoundException {
    int size = input.readInt();
    for (int i = 0; i < size; i++) {
        String key = input.readUTF();
        Object val = input.readObject();
        if (val instanceof Boolean)
            editor.putBoolean(key, ((Boolean) val).booleanValue());
        else if (val instanceof Float)
            editor.putFloat(key, ((Float) val).floatValue());
        else if (val instanceof Integer)
            editor.putInt(key, ((Integer) val).intValue());
        else if (val instanceof Long)
            editor.putLong(key, ((Long) val).longValue());
        else if (val instanceof String)
            editor.putString(key, ((String) val));
    }//w ww  . j av  a  2 s  .c o  m
    editor.commit();
}

From source file:brainleg.app.util.AppUtil.java

/**
 * <p>Deserializes an <code>Object</code> from the specified stream.</p>
 * <p/>/*w w w .j av a2 s  .co  m*/
 * <p>The stream will be closed once the object is written. This
 * avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.</p>
 * <p/>
 * <p>The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.</p>
 *
 * @param inputStream the serialized object input stream, must not be null
 * @return the deserialized object
 * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
 * @throws SerializationException   (runtime) if the serialization fails
 */
public static Object deserialize(InputStream inputStream) {
    if (inputStream == null) {
        throw new IllegalArgumentException("The InputStream must not be null");
    }
    ObjectInputStream in = null;
    try {
        // stream closed in the finally
        in = new ObjectInputStream(inputStream);
        return in.readObject();

    } catch (ClassNotFoundException ex) {
        throw new SerializationException(ex);
    } catch (IOException ex) {
        throw new SerializationException(ex);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java

public static StackExchangeThread deserializeThreadFromBinFile(String binFile) throws IngestionException {
    Object deserializedObj = null;
    try {//from w  ww.  jav a  2  s . co m
        InputStream binIn = new FileInputStream(binFile);
        ObjectInputStream in = new ObjectInputStream(binIn);
        deserializedObj = in.readObject();
        in.close();
        binIn.close();
    } catch (IOException | ClassNotFoundException e) {
        throw new IngestionException(e);
    }
    return (StackExchangeThread) deserializedObj;
}

From source file:Main.java

public static Object readObject(File file) {
    Object obj = null;//  w w  w  . jav  a2s.  c  o  m
    if (file != null && file.exists()) {
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;

        try {
            fileInputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(fileInputStream);

            obj = objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            Log.d("readObject", e.getMessage());
        } finally {
            try {
                if (objectInputStream != null) {
                    objectInputStream.close();
                }
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                Log.d("readObject", e.getMessage());
            }

        }
    }
    return obj;
}

From source file:com.alta189.deskbin.util.CryptUtils.java

private static boolean loadKey() {
    FileInputStream fis = null;//  w  w w.j ava  2 s .  c  o  m
    ObjectInputStream in = null;
    try {
        fis = new FileInputStream(keyFile);
        in = new ObjectInputStream(fis);
        Object obj = in.readObject();
        if (obj != null && obj instanceof SecretKey) {
            key = (SecretKey) obj;
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(in);
    }
    return false;
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java

/**
 * reproduce the Java Object with the byte array
 * //from w w  w .  j  ava 2s  . com
 * @param binCode - the byte array for that Java Object
 * @return the original Java Object before serialization
 * @throws IngestionException
 */
public static Object deserializeObjFromBinArr(byte[] binCode) throws IngestionException {
    Object deserializedObj = null;
    ByteArrayInputStream binIn = new ByteArrayInputStream(binCode);
    try {
        ObjectInputStream in = new ObjectInputStream(binIn);
        deserializedObj = in.readObject();
        in.close();
        binIn.close();
    } catch (IOException | ClassNotFoundException e) {
        throw new IngestionException(e);
    }
    return deserializedObj;
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java

/**
 * reproduce the StackExchangeThread with the byte array
 * /*from   ww w  .  j  a v a2 s  .com*/
 * @param binCode - the byte array for that StackExchangeThread
 * @return the original StackExchangeThread before serialization
 * @throws IngestionException
 */
public static StackExchangeThread deserializeThreadFromBinArr(byte[] binCode) throws IngestionException {
    Object deserializedObj = null;
    ByteArrayInputStream binIn = new ByteArrayInputStream(binCode);
    try {
        ObjectInputStream in = new ObjectInputStream(binIn);
        deserializedObj = in.readObject();
        in.close();
        binIn.close();
    } catch (IOException | ClassNotFoundException e) {
        throw new IngestionException(e);
    }
    return (StackExchangeThread) deserializedObj;
}

From source file:com.projity.exchange.LocalFileImporter.java

public static Job getImportFileJob(final FileImporter importer) {
    final Job job = new Job(importer.getJobQueue(), "importFile", //$NON-NLS-1$
            Messages.getString("LocalFileImporter.Importing"), true); //$NON-NLS-1$
    job.addRunnable(new JobRunnable("Import", 1.0f) { //$NON-NLS-1$
        public Object run() throws Exception {
            DataUtil serializer = new DataUtil();
            log.debug("Loading : " + new File(importer.getFileName()).getCanonicalPath());

            long t1 = System.currentTimeMillis();
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(importer.getFileName()));
            Object obj = in.readObject();
            if (obj instanceof String)
                obj = in.readObject(); //check version in the future
            DocumentData projectData = (DocumentData) obj;
            projectData.setMaster(true);
            projectData.setLocal(true);/*  www  .java2 s . c o  m*/
            long t2 = System.currentTimeMillis();
            log.debug("Loading...Done in " + (t2 - t1) + " ms");

            t1 = System.currentTimeMillis();
            //               project=serializer.deserializeProject(projectData,false,true,resourceMap);
            importer.setProject(serializer.deserializeLocalDocument(projectData));
            t2 = System.currentTimeMillis();
            log.debug("Deserializing...Done in " + (t2 - t1) + " ms");
            setProgress(1.0f);
            return null;
        }
    });
    return job;
}

From source file:de.bps.webservices.clients.onyxreporter.HashMapWrapper.java

/**
 * Deserializes an object from the given Base65 encoded string.
 * /*w  ww .ja v  a  2s .  c o  m*/
 * @param base64EncodedSerializedObject
 * @return
 * @throws OnyxReporterException
 */
private static final Object deserialize(final String base64EncodedSerializedObject)
        throws OnyxReporterException {
    final ByteArrayInputStream bais = new ByteArrayInputStream(
            Base64.decodeBase64(base64EncodedSerializedObject));
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(bais);
        final Object o = ois.readObject();
        return o;
    } catch (final IOException e) {
        throw new OnyxReporterException("Could not deserialize object!", e);
    } catch (final ClassNotFoundException e) {
        throw new OnyxReporterException("Could not deserialize object!", e);
    } finally {
        try {
            if (ois != null) {
                ois.close();
            }
        } catch (final IOException e) {
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.StorageImpl.java

private static void load() {
    try {/*from   www.j  a  v  a  2  s .  com*/
        final File file = new File(System.getProperty("user.home"), "htmlunit.storage");
        if (file.exists()) {
            final ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            SINGLETON_ = (StorageImpl) in.readObject();
            SINGLETON_.sessionStorage_ = new HashMap<String, Map<String, String>>();
            in.close();
        }
    } catch (final Exception e) {
        LOG.info("Could not load storage", e);
    }
}