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 <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(src);/*from   ww w. ja v a 2  s .co m*/

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

From source file:Main.java

/**
 * Receives an object through bluetooth.
 *///from   w  w  w.j  a  v  a  2  s. c  o  m
public static Object bluetoothReadObject(BluetoothSocket socket) {
    ObjectInputStream oIS;
    try {
        oIS = new ObjectInputStream(socket.getInputStream());
        Object object = oIS.readObject();
        return object;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

private static Map<UUID, String> readPageNameHashesFromFile(File folder) {
    Map<UUID, String> map = new HashMap<UUID, String>();

    try {/*  ww w. j av a  2  s  .com*/
        FileInputStream fis = new FileInputStream(folder.getAbsolutePath() + fileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
        map = (Map<UUID, String>) ois.readObject();
        ois.close();
        printMap();
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("Read from file", e.toString());
    }

    return map;
}

From source file:Main.java

public static Object byteToObject(byte[] bytes) throws Exception {
    ObjectInputStream ois = null;
    try {/*  w  ww.  j a va 2  s . c om*/
        ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        return ois.readObject();
    } finally {
        if (ois != null)
            ois.close();
    }
}

From source file:Main.java

/**
 * Redintegrate a serialized object.//from ww w  .java 2s . co m
 * @param serial a byte array of the serialized object.
 * @return the original object or null if an error occurs.
 */
public static Object deserialize(byte[] serial) {
    ByteArrayInputStream bais = new ByteArrayInputStream(serial);
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } catch (IOException e) {
        return null;
    } catch (ClassNotFoundException e) {
        return null;
    } finally {
        try {
            if (ois != null)
                ois.close();
        } catch (IOException e) {
        }
    }
}

From source file:javaslang.jackson.datatype.deserialize.SerializableDeserializer.java

@SuppressWarnings("unchecked")
private static <T> T deserialize(byte[] objectData) {
    try {/*from w w  w.ja v  a  2  s .c o m*/
        ObjectInputStream stream = new ObjectInputStream(new ByteArrayInputStream(objectData));
        return (T) stream.readObject();
    } catch (Exception x) {
        throw new IllegalStateException("Error deserializing object", x);
    }
}

From source file:com.test.todolist.ObjectSerializer.java

public static Object deserialize(String str) throws IOException {
    if (str == null || str.length() == 0)
        return null;
    try {/*from   w ww.j a  v  a 2  s .  c om*/
        ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
        ObjectInputStream objStream = new ObjectInputStream(serialObj);
        return objStream.readObject();
    } catch (Exception e) {
        return "exception";
    }
}

From source file:Main.java

public static Serializable toSerializable(byte[] bytes) {
    if (bytes == null) {
        return null;
    }//  w w w .  j  ava 2  s.c om
    try {
        final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        final ObjectInputStream ois = new ObjectInputStream(bais);
        try {
            return (Serializable) ois.readObject();
        } finally {
            ois.close();
        }
    } catch (Exception e) {
        String msg = "Failed to convert the object to binary: bytes.length=" + bytes.length;
        throw new IllegalStateException(msg, e);
    }
}

From source file:com.trin.nilmobile.serializer.ObjectSerializer.java

public static Object deserialize(String str) throws IOException {
    if (str == null || str.length() == 0)
        return null;
    try {/*ww  w.  j a v a 2 s  . c  om*/
        ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
        ObjectInputStream objStream = new ObjectInputStream(serialObj);
        return objStream.readObject();
    } catch (Exception e) {
        throw new IOException("Deserialization error: " + e.getMessage(), e);
    }
}

From source file:edu.virginia.jtd5qe.twitter.ObjectSerializer.java

public static Object deserialize(String str) throws IOException {
    if (str == null || str.length() == 0)
        return null;
    try {//from  www  . ja v a  2  s.  c  o m
        ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
        ObjectInputStream objStream = new ObjectInputStream(serialObj);
        return objStream.readObject();
    } catch (Exception e) {
        throw WrappedIOException.wrap("Deserialization error: " + e.getMessage(), e);
    }
}