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 Object getObject(ByteBuffer byteBuffer) throws ClassNotFoundException, IOException {
    InputStream input = new ByteArrayInputStream(byteBuffer.array());
    ObjectInputStream oi = new ObjectInputStream(input);
    Object obj = oi.readObject();
    input.close();/*from w ww  .  j  a v  a 2  s.com*/
    oi.close();
    byteBuffer.clear();
    return obj;
}

From source file:MainClass.java

public static void desEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;//ww w . j av  a 2s .co  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 stm2obj(InputStream f) throws IOException, ClassNotFoundException {
    ObjectInputStream oo = new ObjectInputStream(f);
    try {/*from w  ww .  j  av  a 2  s.com*/
        return oo.readObject();
    } finally {
        oo.close();
    }
}

From source file:Main.java

public static Object DeserializeObject(String path) throws ClassNotFoundException, IOException {
    Object o;//w w  w  .j  a v  a2  s . c om
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(path)));
    o = ois.readObject();
    return o;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T deserializeFromByteArray(byte[] bytes) {
    if (bytes == null) {
        return null;
    }//from ww w  . j ava2  s . co m

    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    T object = null;
    try {
        ObjectInputStream ois = new ObjectInputStream(bais);
        object = (T) ois.readObject();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return object;
}

From source file:Main.java

public static Object readObj(String fileName) {
    Object obj = new Object();
    try {/*from   ww w  . ja va  2s . c  o m*/
        FileInputStream fin = new FileInputStream(fileName);
        ObjectInputStream oin = new ObjectInputStream(fin);
        obj = oin.readObject();
        fin.close();
        oin.close();
    } catch (Exception e) {
        printE("OpusTool", e);
    } finally {
        return obj;
    }
}

From source file:Main.java

public static <T> T deserializeObjectFromFile(FileInputStream input) {
    try {/*  ww w . j a v a2s  .  c  om*/
        ObjectInputStream deserializer = new ObjectInputStream(input);
        return (T) deserializer.readObject();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:MainClass.java

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

    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();
}

From source file:Main.java

/** Read the object from Base64 string. */
public static Object fromString(String s) throws IOException, ClassNotFoundException {
    byte[] data = Base64.decode(s, 0);
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
    Object o = ois.readObject();
    ois.close();/* w  w  w  .j a v a 2 s. c  o m*/
    return o;
}

From source file:Main.java

public static Object deserialize(byte[] array) throws Exception {
    //System.err.println("neser#"+new String(array));
    ObjectInputStream rin = new ObjectInputStream(new ByteArrayInputStream(array));
    Object obj = rin.readObject();
    rin.close();/*  w w  w .  j a  va 2  s.c om*/
    //System.err.println("neser##"+obj);
    return obj;
}