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.gamesalutes.utils.ByteUtils.java

/**
 * Reads an object from the input <code>bytes</code>.
 * /*from w  w w. ja v a2  s . c o  m*/
 * @param <T> type of the object
 * @param bytes the input bytes
 * @return the read object
 * @throws Exception if error occurs during reading
 */
@SuppressWarnings("unchecked")
public static <T> T readObject(byte[] bytes) throws Exception {
    if (bytes == null)
        throw new NullPointerException("bytes");
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new ByteArrayInputStream(bytes));
        return (T) in.readObject();
    } finally {
        MiscUtils.closeStream(in);
    }
}

From source file:edu.iu.daal_naive.NaiveDaalCollectiveMapper.java

private static TrainingPartialResult deserializePartialResult(ByteArray byteArray)
        throws IOException, ClassNotFoundException {
    /* Create an input stream to deserialize the numeric table from the array */
    byte[] buffer = byteArray.get();
    ByteArrayInputStream inputByteStream = new ByteArrayInputStream(buffer);
    ObjectInputStream inputStream = new ObjectInputStream(inputByteStream);

    /* Create a numeric table object */
    TrainingPartialResult restoredDataTable = (TrainingPartialResult) inputStream.readObject();
    restoredDataTable.unpack(daal_Context);

    return restoredDataTable;
}

From source file:com.google.android.gcm.demo.server.Datastore.java

public static JSONArray getAllAlerts(int offset, int limit) {
    Cache cache;//  ww  w .ja  va2s  .  c  o m
    String cacheKey = "offset_" + offset + "_limit_" + limit;
    byte[] cacheValue = null;

    JSONArray alerts = null;
    try {
        CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
        cache = cacheFactory.createCache(Collections.emptyMap());
        cacheValue = (byte[]) cache.get(cacheKey);
        if (cacheValue == null) {
            alerts = getAllAlertsFromDataStore(offset, limit);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = null;
            try {
                oos = new ObjectOutputStream(baos);
            } catch (Exception e) {
                e.printStackTrace();
            }
            oos.writeObject(alerts);
            byte cval[] = baos.toByteArray();
            if (cval.length < 1024 * 1024) {
                Logger.getLogger(Datastore.class.getName()).log(Level.SEVERE,
                        "Alerts Size is in limits:" + cval.length);
                cache.put(cacheKey, cval);
            } else {
                Logger.getLogger(Datastore.class.getName()).log(Level.SEVERE,
                        "Length Size has exceeded:" + cval.length);
            }

        } else {
            ByteArrayInputStream bais = new ByteArrayInputStream(cacheValue);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
            } catch (Exception e) {
                e.printStackTrace();
            }
            alerts = (JSONArray) ois.readObject();
            Logger.getLogger(Datastore.class.getName()).log(Level.WARNING, "From the Cache");
        }
    } catch (CacheException e) {

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    return alerts;
}

From source file:com.gamesalutes.utils.ByteUtils.java

/**
 * Reads an object from the input <code>buf</code>.
 * /* ww w  . j  a v  a  2 s  .co  m*/
 * @param <T> type of the object
 * @param buf the input <code>ByteBuffer</code>
 * @return the read object
 * @throws Exception if error occurs during reading
 */
@SuppressWarnings("unchecked")
public static <T> T readObject(ByteBuffer buf) throws Exception {
    if (buf == null)
        throw new NullPointerException("buf");
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(Channels.newInputStream(new ByteBufferChannel(buf)));
        return (T) in.readObject();
    } finally {
        MiscUtils.closeStream(in);
    }
}

From source file:com.sec.ose.osi.sdk.protexsdk.component.ComponentAPIWrapper.java

public static void init() {
    ObjectInputStream ois;
    try {// w  w  w  .j  a v  a2s. c o  m
        ois = new ObjectInputStream(new FileInputStream(new File(COMPONENT_FILE_PATH)));
        ComponentInfo componentInfoTmp = null;
        while ((componentInfoTmp = (ComponentInfo) ois.readObject()) != null) {
            globalComponentManager.addComponent(componentInfoTmp);
        }
        ois.close();
    } catch (FileNotFoundException e) {
        return;
    } catch (IOException e) {
        return;
    } catch (ClassNotFoundException e) {
        return;
    }
}

From source file:de.tudarmstadt.ukp.dkpro.tc.core.util.TaskUtils.java

/**
 * Loads serialized Object from disk. File can be uncompressed, gzipped or bz2-compressed.
 * Compressed files must have a .gz or .bz2 suffix.
 *
 * @param serializedFile//  w  w w . j a  va2 s  . c  o  m
 * @return the deserialized Object
 * @throws IOException
 */
@SuppressWarnings({ "unchecked" })
public static <T> T deserialize(File serializedFile) throws IOException {
    FileInputStream fis = new FileInputStream(serializedFile);
    BufferedInputStream bufStr = new BufferedInputStream(fis);

    InputStream underlyingStream = null;
    if (serializedFile.getName().endsWith(".gz")) {
        underlyingStream = new GZIPInputStream(bufStr);
    } else if (serializedFile.getName().endsWith(".bz2")) {
        // skip bzip2 prefix that we added manually
        fis.read();
        fis.read();
        underlyingStream = new CBZip2InputStream(bufStr);
    } else {
        underlyingStream = bufStr;
    }

    ObjectInputStream deserializer = new ObjectInputStream(underlyingStream);

    Object deserializedObject = null;
    try {
        deserializedObject = deserializer.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException("The serialized file was probably corrupted.", e);
    } finally {
        deserializer.close();
    }
    return (T) deserializedObject;
}

From source file:com.conwet.silbops.model.JSONvsRMIPerfT.java

public static JSONObject desJSONizeObject(ByteArrayInputStream bais) throws Exception {

    ObjectInputStream inputStream = new ObjectInputStream(bais);
    JSONParser parser = new JSONParser();

    String jsonReceived = (String) inputStream.readObject();

    return (JSONObject) parser.parse(jsonReceived);
}

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

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

    try {/*from   w  w w.j  a  v a 2 s .co 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:com.conwet.silbops.model.JSONvsRMIPerfT.java

public static void desexternalizeObjects(ByteArrayInputStream bais, int nElements) {

    long start = 0, end = 0;

    try {//from   w  ww .j a  v  a  2 s .  c  o  m
        ObjectInputStream in = new ObjectInputStream(bais);

        start = System.nanoTime();

        for (int i = 0; i < nElements; i++) {
            in.readObject();
        }

        end = System.nanoTime();

        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    long elapsed = end - start;
    long elapsedPerMessage = elapsed / nElements;
    System.out.println("   Elapsed Time " + nElements + " messages - Desexternalize: " + elapsed
            + " nanoseconds (" + elapsedPerMessage + " nanoseconds/msg)");
}

From source file:com.hazelcast.stabilizer.Utils.java

public static <E> E readObject(File file) {
    try {/*from  w w w. j  a  va 2  s . co  m*/
        FileInputStream fis = new FileInputStream(file);
        try {
            ObjectInputStream in = new ObjectInputStream(fis);
            return (E) in.readObject();
        } finally {
            Utils.closeQuietly(fis);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}