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:Main.java

@SuppressWarnings("unchecked")
static <T extends Serializable> T fromBlob(Class<T> clazz, byte[] blob)
        throws IOException, ClassNotFoundException {
    T result = null;//from w  w w  .  j  a  v a 2  s .  c  o  m
    ObjectInputStream ois = null;
    try {
        ByteArrayInputStream bos = new ByteArrayInputStream(blob);
        ois = new ObjectInputStream(bos);
        result = (T) ois.readObject();
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:Main.java

public static Object readObject(Context context, String filename) {
    FileInputStream in = null;//from w  ww .  j a  va  2s.co  m
    ObjectInputStream oin = null;
    Object data = null;
    try {
        in = context.openFileInput(filename + ".odb");
        oin = new ObjectInputStream(in);
        data = oin.readObject();
        oin.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return data;
}

From source file:Main.java

/**
 * Deserialize provided string into Object
 * @param serialized String/*from w w  w .ja v a  2  s . co  m*/
 * @return Object
 * @throws IOException If unable to access Stream
 * @throws ClassNotFoundException If unable to find class
 */
public static Object deserialize(String serialized) throws IOException, ClassNotFoundException {
    if (serialized == null)
        return null;
    return new ObjectInputStream(new Base64InputStream(new ByteArrayInputStream(serialized.getBytes()), 0))
            .readObject();
}

From source file:Main.java

public static <T> List<T> deepCopy(List<T> src) {
    try {/*  w  ww .j  av a  2  s  . c o m*/
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        @SuppressWarnings("unchecked")
        List<T> dest = (List<T>) in.readObject();
        return dest;
    } catch (Exception e) {
        return null;
    }
}

From source file:BytesUtil.java

public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
    Object obj = null;//from w  w  w.ja  va  2  s.co  m
    ByteArrayInputStream bis = null;
    ObjectInputStream ois = null;
    try {
        bis = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bis);
        obj = ois.readObject();
    } finally {
        if (bis != null) {
            bis.close();
        }
        if (ois != null) {
            ois.close();
        }
    }
    return obj;
}

From source file:gnomezgrave.gsyncj.auth.ProfileSettings.java

public static ProfileSettings loadProfileSettings(String fileName) throws IOException, ClassNotFoundException {
    ObjectInputStream oi = new ObjectInputStream(new FileInputStream(fileName));
    ProfileSettings profileSettings = (ProfileSettings) oi.readObject();
    oi.close();//from  ww w .  jav  a 2 s .c  o  m
    return profileSettings;
}

From source file:Main.java

/**
 * Returns a object from the given byte array.
 * /*from   w ww.  ja va 2s . co  m*/
 * @param bytes
 *            array to convert
 * @return object
        
 */
public static Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream is = new ObjectInputStream(bais);
    return is.readObject();
}

From source file:Main.java

@SuppressWarnings("unchecked")
static boolean loadSharedPreferencesFromFile(Context context, File src) {
    boolean res = false;
    ObjectInputStream input = null;
    try {/*ww  w.  j  ava2s.  c o  m*/
        GZIPInputStream inputGZIP = new GZIPInputStream(new FileInputStream(src));
        input = new ObjectInputStream(inputGZIP);
        Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(context).edit();
        prefEdit.clear();
        Map<String, Object> entries = (Map<String, Object>) input.readObject();
        for (Entry<String, ?> entry : entries.entrySet()) {
            Object v = entry.getValue();
            String key = entry.getKey();

            if (v instanceof Boolean)
                prefEdit.putBoolean(key, ((Boolean) v).booleanValue());
            else if (v instanceof Float)
                prefEdit.putFloat(key, ((Float) v).floatValue());
            else if (v instanceof Integer)
                prefEdit.putInt(key, ((Integer) v).intValue());
            else if (v instanceof Long)
                prefEdit.putLong(key, ((Long) v).longValue());
            else if (v instanceof String)
                prefEdit.putString(key, ((String) v));
        }
        prefEdit.commit();
        res = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return res;
}

From source file:com.comichentai.serialize.SerializeUtil.java

public static <T> T deserialize(byte[] bytes, Class<T> clazz) {
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {//ww w .  j  a va  2 s .  c  o  m
        bais = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bais);
        Object object = ois.readObject();
        return clazz.cast(object);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        closeQuietly(bais);
        closeQuietly(ois);
    }
}

From source file:com.mayalogy.mayu.io.LocalDataManager.java

public static Object readFromJarAndDeserialize(String resourceName) throws ClassNotFoundException, IOException {
    InputStream is = Class.class.getResourceAsStream("/" + resourceName);
    if (is == null) { //Used for when jar is running in a servlet
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        is = classLoader.getResourceAsStream(resourceName);
    }/*from www . ja v a 2s . c  om*/
    ObjectInputStream ois = new ObjectInputStream(is);
    Object oRead = ois.readObject();
    ois.close();
    return oRead;
}