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")
public static <T> T readObjectFromFile(File file) {
    ObjectInputStream ois = null;
    T object = null;/*from   w  w  w  . java  2 s .  c om*/
    try {
        FileInputStream fis = new FileInputStream(file);
        ois = new ObjectInputStream(fis);
        object = (T) ois.readObject();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return object;
}

From source file:Main.java

public static Object deSerialization(String str) throws IOException, ClassNotFoundException {
    byte[] mobileBytes = Base64.decode(str.getBytes(), Base64.DEFAULT);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    Object object = (Object) objectInputStream.readObject();
    objectInputStream.close();//from ww w  . j  a  va2  s .com

    return object;
}

From source file:Main.java

/**
 * deserialization from file//from w  w w . j a  v  a  2s.  c om
 * 
 * @param filePath
 * @return
 * @throws RuntimeException if an error occurs
 */
public static Object deserialization(String filePath) {
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new FileInputStream(filePath));
        Object o = in.readObject();
        in.close();
        return o;
    } catch (FileNotFoundException e) {
        throw new RuntimeException("FileNotFoundException occurred. ", e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("ClassNotFoundException occurred. ", e);
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:Main.java

public static ObjectInputStream readObjectStream(Context context, String name)
        throws StreamCorruptedException, IOException {
    InputStream s = context.openFileInput(name);

    // FIXME - add buffering?
    return new ObjectInputStream(s);
}

From source file:Main.java

public static synchronized ArrayList<double[]> readArrayListFromFile(String filename) {
    ObjectInputStream input;/*from   w ww  .  ja  v  a  2 s  . c  o  m*/
    ArrayList<double[]> x = null;
    try {
        input = new ObjectInputStream(new FileInputStream(new File(filename)));
        Object obj = input.readObject();
        if (obj != null)
            x = (ArrayList<double[]>) input.readObject();
        input.close();
        return x;
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    return x;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T readSerializedObject(Context context, String fileName)
        throws FileNotFoundException, IOException, ClassNotFoundException {
    FileInputStream fis = context.openFileInput(fileName);
    ObjectInputStream in = new ObjectInputStream(fis);

    T out = (T) in.readObject();// www  . j  a  va  2 s.c  o m

    in.close();
    fis.close();
    return out;
}

From source file:Main.java

public static List String2SceneList(String SceneListString) throws IOException, ClassNotFoundException {

    byte[] mobileBytes = Base64.decode(SceneListString.getBytes(), Base64.DEFAULT);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    List SceneList = (List) objectInputStream.readObject();
    objectInputStream.close();/*from  ww  w.  j a  va2s  .c om*/

    return SceneList;
}

From source file:MainClass.java

public static void desEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;//from w w  w .  j  a v a  2s.  c  o  m

    ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser"));
    key = (SecretKey) keyFile.readObject();
    keyFile.close();
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key);
    CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher);
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2));
    int i;
    do {
        i = in.read();
        if (i != -1)
            out.write(i);
    } while (i > 0);
    in.close();
    out.close();
}

From source file:Main.java

public static Object getBeanFromFile(String filePath) {
    Object bean = null;/*from w w w. ja v  a2 s  . c om*/
    File file = new File(filePath);
    ObjectInputStream inputStream = null;
    if (!file.exists()) {
        return bean;
    }
    try {
        inputStream = new ObjectInputStream(new FileInputStream(file));
        bean = inputStream.readObject();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        closeQuiltly(inputStream);
    }

    return bean;
}

From source file:MainClass.java

public static void blowfishEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;/*  w w w .  j  a v  a  2  s.c  om*/

    ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("BlowfishKey.ser"));
    key = (SecretKey) keyFile.readObject();
    keyFile.close();
    Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key);
    CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher);
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2));
    int i;
    do {
        i = in.read();
        if (i != -1)
            out.write(i);
    } while (i > 0);
    in.close();
    out.close();
}