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 Object cloneObject(Object obj) {
    try {//w  w  w  .  j  a  va  2 s .c  o m
        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

public static boolean writeFileByObject(String fileName, Object object) {
    try {//w  ww  . j  a  v  a2  s  . co  m
        FileOutputStream fos = new FileOutputStream(fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
        oos.flush();
        oos.close();
        fos.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static void writeObject(String root, String filename, Object object) {
    File file = new File(root, filename);
    try {//w w  w  . j  a  v  a  2s  .c o m
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
        oos.flush();
        oos.close();
    } catch (Exception e) {
        Log.e(TAG, String.format("Failed to write [%s]", file), e);
    }
}

From source file:Main.java

public static void writeObject(OutputStream o, Object obj) throws IOException {
    ObjectOutputStream oo = new ObjectOutputStream(o);
    try {//from w  ww. ja  v  a  2s  . com
        oo.writeObject(obj);
    } finally {
        oo.close();
    }
}

From source file:Main.java

/**
 * Returns a byte array from the given object.
 * /*from  www .  ja v a2  s  .  c o  m*/
 * @param object
 *            to convert
 * @return byte array from the object
        
 */
public static byte[] objectToBytes(Object object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(baos);
    os.writeObject(object);
    return baos.toByteArray();
}

From source file:Main.java

public static boolean serializeObjectToFile(Serializable object, FileOutputStream output) {
    try {/* w  w  w.j  a v a 2  s  .  com*/
        ObjectOutputStream serializer = new ObjectOutputStream(output);
        serializer.writeObject(object);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static void writeGZipCompressedObject(Object o, OutputStream out) throws IOException {
    GZIPOutputStream gos = new GZIPOutputStream(out);
    ObjectOutputStream oos = new ObjectOutputStream(gos);
    oos.writeObject(o);
    oos.flush();//from w  ww  .j av  a  2s .c o  m
    gos.finish();
}

From source file:Main.java

public static byte[] toBytes(Object object) {
    if (object == null) {
        return null;
    }/* w  w  w.jav a 2 s .  c  om*/

    ByteArrayOutputStream __baos = new ByteArrayOutputStream();
    try {
        ObjectOutputStream __oos = new ObjectOutputStream(__baos);
        __oos.writeObject(object);
    } catch (IOException ioe) {
        // Log the event here....
    }
    return __baos.toByteArray();
}

From source file:Main.java

public static byte[] ObjectToByte(Serializable obj) {
    byte[] bytes = null;
    try {/*from   ww w .  ja v a2  s.  co  m*/
        // object to bytearray
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(obj);

        bytes = bo.toByteArray();

        bo.close();
        oo.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:Main.java

public static void saveObj(String fileName, Object obj) {

    try {/*from   w  ww.  j a v a  2s  . c  om*/
        FileOutputStream fout = new FileOutputStream(fileName);
        ObjectOutputStream oout = new ObjectOutputStream(fout);
        oout.writeObject(obj);
        fout.close();
        oout.close();
    } catch (Exception e) {
        printE("OpusTool", e);
    }
}