Example usage for java.io ObjectInputStream ObjectInputStream

List of usage examples for java.io ObjectInputStream ObjectInputStream

Introduction

In this page you can find the example usage for java.io ObjectInputStream ObjectInputStream.

Prototype

public ObjectInputStream(InputStream in) throws IOException 

Source Link

Document

Creates an ObjectInputStream that reads from the specified InputStream.

Usage

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

/**
 * reproduce the Java Object with the byte array
 * /*w  ww  . ja v  a  2  s.  co m*/
 * @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:edu.tufts.vue.util.Encryption.java

private static synchronized Key getKey() {
    try {//from w ww .j a va2 s  . c  o  m
        if (key == null) {
            File keyFile = new File(
                    VueUtil.getDefaultUserFolder().getAbsolutePath() + File.separator + KEY_FILE);
            if (keyFile.exists()) {
                ObjectInputStream is = new ObjectInputStream(new FileInputStream(keyFile));
                key = (Key) is.readObject();
                is.close();
            } else {
                key = KeyGenerator.getInstance(algorithm).generateKey();
                ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(keyFile));
                os.writeObject(key);
                os.close();
            }
            return key;
        } else {
            return key;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }

}

From source file:org.glom.web.server.Utils.java

static public Object deepCopy(final Object oldObj) {
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;

    try {//w ww  .jav a 2  s .c  o  m
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        // serialize and pass the object
        oos.writeObject(oldObj);
        oos.flush();
        final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
        ois = new ObjectInputStream(bin);

        // return the new object
        return ois.readObject();
    } catch (final Exception e) {
        System.out.println("Exception in deepCopy:" + e);
        return null;
    } finally {
        try {
            oos.close();
            ois.close();
        } catch (final IOException e) {
            System.out.println("Exception in deepCopy during finally: " + e);
            return null;
        }
    }
}

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

@Override
public Object unwrap(Object state) {
    synchronized (file_) {
        try {/*from w ww .java2s  .co  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.topekalabs.bigmachine.lib.app.ContinuationSerializationTest.java

@Test
public void simpleSerializationTest() throws Exception {
    MutableInt context = new MutableInt();
    Continuation c = Continuation.startWith(new SimpleContinuationRunnable(), context);
    logger.debug("Is serializable: {}", c.isSerializable());

    Assert.assertEquals("The value of the context was not set properly", SimpleContinuationRunnable.FIRST_VALUE,
            context.getValue());// w  ww  .  j  a  v  a  2 s .c  o m

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(c);
    oos.close();

    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
    c = (Continuation) ois.readObject();
    ois.close();

    Continuation.continueWith(c, context);

    Assert.assertEquals("The value of the context was not set properly",
            SimpleContinuationRunnable.SECOND_VALUE, context.getValue());
}

From source file:de.tud.cs.se.flashcards.persistence.Store.java

public static FlashcardSeries openSeries(File file) throws IOException {

    ObjectInputStream oin = null;
    try {/*from www. ja  v a 2 s  . c om*/

        FlashcardSeries series = new FlashcardSeries();

        oin = new ObjectInputStream(new FileInputStream(file));
        int size = oin.readInt();
        for (int i = 0; i < size; i++) {
            series.addCard((Flashcard) oin.readObject());
        }

        return series;
    } catch (ClassNotFoundException e) {
        // the file did contain something unexpected...
        throw new IOException(e);
    } finally {
        if (oin != null)
            IOUtils.closeQuietly(oin);
    }
}

From source file:com.manning.blogapps.chapter11.rome.DiskFeedInfoCache.java

public SyndFeedInfo getFeedInfo(URL url) {
    SyndFeedInfo info = null;/*w w  w.  j  a  va  2 s .c  o  m*/
    String fileName = cachePath + File.separator + "feed_" + url.hashCode();
    FileInputStream fis;
    try {
        fis = new FileInputStream(fileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
        info = (SyndFeedInfo) ois.readObject();
        fis.close();
    } catch (FileNotFoundException fnfe) {
        logger.debug("Cache miss for " + url.toString());
    } catch (ClassNotFoundException cnfe) {
        // Error writing to cahce is fatal
        throw new RuntimeException("Attempting to read from cache", cnfe);
    } catch (IOException fnfe) {
        // Error writing to cahce is fatal
        throw new RuntimeException("Attempting to read from cache", fnfe);
    }
    if (info == null)
        logger.info("Cache MISS!");
    return info;
}

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

/**
 * Deserialize base64.//www. ja v a2  s. c  o m
 * 
 * @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:net.carinae.dev.async.util.SerializerJavaImpl.java

/**
 * {@inheritDoc}/* ww w . j ava2 s . co  m*/
 */
@Override
public Object deserializeObject(byte[] serializedObj) {

    try {
        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedObj));
        Object obj = in.readObject();
        return obj;
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Could not deserialize", e);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Could not deserialize", e);
    }

}

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

/**
 * <p>Deserializes an <code>Object</code> from the specified stream.</p>
 * <p/>/*w w w . jav  a2 s . c  om*/
 * <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
        }
    }
}