Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutputStream writeObject.

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

Usage

From source file:Main.java

public static final byte[] serialize(Object o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.flush();/*from w w  w.j  a va2  s  . co  m*/
    oos.close();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] getBytes(Object obj) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(obj);
    out.flush();/*w  ww .j a v  a 2s . com*/
    byte[] bytes = bout.toByteArray();
    bout.close();
    out.close();
    return bytes;
}

From source file:Main.java

public static byte[] objectToByte(Object obj) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(outputStream);
    os.writeObject(obj);
    return outputStream.toByteArray();
}

From source file:Main.java

public static <T> void writeSerializedObject(Context context, String fileName, T objectToWrite)
        throws IOException {
    FileOutputStream fileOut = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(objectToWrite);
    out.close();//from  w w  w  . ja v  a2s .  com
    fileOut.close();
}

From source file:Main.java

public static byte[] serialize(Object obj) throws Exception {
    //System.err.println(" ser ##"+obj);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(bout);
    oout.writeObject(obj);
    oout.flush();//from  w  w w . j  ava  2s  .c o  m
    byte array[] = bout.toByteArray();
    oout.close();
    bout.close();
    //System.err.println(" ser #"+new String(array));
    return array;
}

From source file:Main.java

public static List copyBySerialize(List src) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(src);

    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    ObjectInputStream in = new ObjectInputStream(byteIn);
    List dest = (List) in.readObject();
    return dest;/*w  w w  .j a  v a 2 s. c  om*/
}

From source file:Main.java

public static byte[] toBytes(Object obj) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);
    byte[] arr = baos.toByteArray();
    return arr;//w ww  . jav  a  2  s. c o m
}

From source file:Main.java

public static <T> byte[] serializeToByteArray(T object) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from  w w w  .ja v a2 s  . c om*/

        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return baos.toByteArray();
}

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

    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    ObjectInputStream in = new ObjectInputStream(byteIn);
    @SuppressWarnings("unchecked")
    List<T> dest = (List<T>) in.readObject();
    return dest;//from ww  w .  j  ava2 s  . c  om
}

From source file:Main.java

public static byte[] objectToStream(Object obj) {
    try {//ww w  .  j a  v  a 2 s . c o  m
        ByteArrayOutputStream buff = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(buff);
        stream.writeObject(obj);
        stream.close();
        return buff.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}