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

/**
 * Deserialize an array of bytes into an object.
 * @param input Serialized stream of bytes
 * @return Deserialized object.//  ww w.java  2  s .co m
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static Serializable bytesToObject(byte input[]) throws IOException, ClassNotFoundException {
    return (Serializable) new ObjectInputStream(new ByteArrayInputStream(input)).readObject();
}

From source file:Main.java

public static Object getSerializable(byte[] bytes) {
    Object obj = null;/*  w w w  .  j ava2  s .  c o m*/
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bis);
        obj = ois.readObject();
        ois.close();
        bis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    return obj;
}

From source file:Main.java

public static Object ByteToObject(byte[] bytes) {
    Object obj = null;//from w  w  w. java  2  s  .com
    try {
        // bytearray to object
        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
        ObjectInputStream oi = new ObjectInputStream(bi);

        obj = oi.readObject();
        bi.close();
        oi.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

public static Object convertByteArrayToObject(byte[] bytes) throws IOException, ClassNotFoundException {
    ObjectInputStream is = null;//from www .j  ava 2  s .c o m
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    is = new ObjectInputStream(new BufferedInputStream(byteArrayInputStream));
    Object obj = is.readObject();
    is.close();
    return obj;

}

From source file:Main.java

public static Object ByteToObject(byte[] bytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;//from ww  w .j  av a 2 s. c  o m

    try {
        in = new ObjectInputStream(bis);
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Object obj = null;
    try {
        obj = in.readObject();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return obj;

}

From source file:Main.java

public static Object cloneObject(Object obj) {
    try {//  w w w  .  ja va 2  s .com
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(obj);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        return in.readObject();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T deserializeFromByteArray(byte[] bytes) {
    if (bytes == null) {
        return null;
    }//from  ww  w .j  a  v a  2s .  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 getObjectFromFile(String filePath) {
    FileInputStream freader;//  w  w w  .  j  a v a  2s .  co m
    Object ret = null;
    try {
        freader = new FileInputStream(filePath);
        ObjectInputStream objectInputStream = new ObjectInputStream(freader);
        ret = objectInputStream.readObject();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block   
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block   
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block   
        e.printStackTrace();
    }
    return ret;
}

From source file:Main.java

public static Object readObj(String fileName) {
    Object obj = new Object();
    try {//from  w  ww  . j a va 2  s. c om
        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 Object deserialize(byte[] bytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;//from   w w  w. j  a  v  a  2s .  c  o  m
    Object o = null;
    try {
        in = new ObjectInputStream(bis);
        o = in.readObject();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return o;
}