Example usage for java.io ObjectOutputStream ObjectOutputStream

List of usage examples for java.io ObjectOutputStream ObjectOutputStream

Introduction

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

Prototype

public ObjectOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates an ObjectOutputStream that writes to the specified OutputStream.

Usage

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);/*from  w ww . j  a  va 2 s .  c  o  m*/
    return outputStream.toByteArray();
}

From source file:Main.java

/**
 * Sends an object through bluetooth./*from  w w  w .  j a v a 2 s. c  om*/
 */
public static boolean bluetoothWriteObject(Object object, BluetoothSocket socket) {
    try {
        ObjectOutputStream oOS = new ObjectOutputStream(socket.getOutputStream());
        oOS.writeObject(object);
        oOS.flush();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

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

From source file:Main.java

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

    ObjectOutput out = null;/*  w  ww .  java  2 s . c  om*/

    try {
        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

public static byte[] toBytes(Object object) {
    if (object == null) {
        return null;
    }// w w w  .j a v  a 2s  .  c  o m

    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 String objectToString(Serializable serializableObject) throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(serializableObject);
    objectOutputStream.close();//from  ww w .j  a  va 2 s . c  om
    return new String(Base64.encode(byteArrayOutputStream.toByteArray(), 0));
}

From source file:Main.java

/**
 * Serialize an object into bytes.//from   ww w.  java2  s  .  c  o  m
 * @param o Object to be serialized.
 * @return Serialized stream of bytes.
 * @throws IOException
 */
public static byte[] objectToBytes(Serializable o) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(out);
    oo.writeObject(o);
    oo.close();
    return out.toByteArray();
}

From source file:Main.java

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

    try {//from  w  w  w  .j  a  va2  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:SerializationUtils.java

public static byte[] serialize(Object obj) {
    try {//w  w  w. j a va2s. c o m
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(buffer);
        oos.writeObject(obj);
        oos.close();
        return buffer.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("error writing to byte-array!");
    }
}

From source file:Main.java

public static void saveSerializableObjectToFile(Object object, FileOutputStream fileOut) {
    ObjectOutputStream out = null;
    try {// w  w  w . j av  a2 s.  c  o  m
        out = new ObjectOutputStream(fileOut);
        out.writeObject(object);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}