Example usage for java.io ObjectOutput writeObject

List of usage examples for java.io ObjectOutput writeObject

Introduction

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

Prototype

public void writeObject(Object obj) throws IOException;

Source Link

Document

Write an object to the underlying storage or stream.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object object = new JButton("push me");

    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
    out.writeObject(object);
    out.close();/*from ww  w.j a  v a 2  s  .c  o  m*/

    // Serialize to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    out = new ObjectOutputStream(bos);
    out.writeObject(object);
    out.close();

    // Get the bytes of the serialized object
    byte[] buf = bos.toByteArray();
    System.out.println(new String(buf));
}

From source file:FreezeAlien.java

public static void main(String[] args) throws Exception {
    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("X.file"));
    Alien zorcon = new Alien();
    out.writeObject(zorcon);
}

From source file:Main.java

public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {//from  w ww . j a  v a2 s.co  m
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.close();

        byte[] buf = bos.toByteArray();

        return buf;
    } catch (IOException ioe) {
        //         Log.e("serializeObject", "error", ioe);

        return null;
    }
}

From source file:Main.java

public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {//  w  w w .j a v  a  2 s. c  o  m
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.close();

        byte[] buf = bos.toByteArray();

        return buf;
    } catch (IOException ioe) {
        Log.e("serializeObject", "error", ioe);

        return null;
    }
}

From source file:Main.java

public static void saveObject(Context context, String fileName, Object obj) throws IOException {
    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutput out = null;
    try {//from   www  .jav a  2 s  . c  o  m
        out = new ObjectOutputStream(fos);
        out.writeObject(obj);
        out.flush();
    } finally {
        out.close();
        fos.close();
    }

}

From source file:org.apache.hadoop.mapred.lib.instanceapi.SerializableUtil.java

public static void serializeObject(Configuration conf, String objectPropertyName, Object object)
        throws IOException {
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(outBytes);
    out.writeObject(object);
    out.close();//www.ja v a 2 s. c o m
    byte[] base64 = Base64.encodeBase64(outBytes.toByteArray());
    conf.set(objectPropertyName, new String(base64));
}

From source file:Main.java

public static void serialize(Object object, OutputStream outputStream) {
    try {/*  www. ja  va 2s . c  o m*/
        final OutputStream buffer = new BufferedOutputStream(outputStream);
        final ObjectOutput output = new ObjectOutputStream(buffer);
        try {
            output.writeObject(object);
        } finally {
            output.close();
        }
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] ObjectToByte(Object obj) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ObjectOutput out = null;

    try {/*  www  .  ja  v a 2 s.c  o m*/
        out = new ObjectOutputStream(bos);

        out.writeObject(obj);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte[] Bytes = bos.toByteArray();

    return Bytes;

}

From source file:Main.java

/**
 * Convert serializable object to bytes.
 *
 * @param object object//from w w w  . jav  a 2  s . c om
 * @return bytes array
 */
public static byte[] toBytes(Object object) {
    byte[] result = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(object);
        result = bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
            }
        }
        if (bos != null) {
            try {
                bos.close();
            } catch (Exception e) {
            }
        }
    }
    return result;
}

From source file:Main.java

public static byte[] toByteArray(Object obj) {
    byte[] returnBytes = null;
    ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
    ObjectOutput objectOut = null;
    try {/*ww  w  . ja va  2  s  . c o  m*/
        objectOut = new ObjectOutputStream(byteArrayOutStream);
        objectOut.writeObject(obj);
        returnBytes = byteArrayOutStream.toByteArray();
    } catch (java.io.IOException ioe) {
        return null;
    } finally {
        try {
            objectOut.close();
            byteArrayOutStream.close();
        } catch (java.io.IOException ioe) {

        }
    }
    return returnBytes;
}