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

/**
 * Serialize an object.//  w  w w.  j a v a 2s  .co  m
 * @param obj a serializable object.
 * @return a byte array of the serialized object or null if an error occurs.
 */
public static byte[] serialize(Object obj) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
        baos.flush();
        return baos.toByteArray();
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (oos != null)
                oos.close();
        } catch (IOException e) {
        }
    }
}

From source file:id.co.nlp.MachineTranslation.Utils.Util.java

public static void serializing(String pathfile, Object object) throws FileNotFoundException, IOException {
    FileOutputStream fileOut = new FileOutputStream(pathfile);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(object);
    out.close();/*w  w w .  ja  va  2  s . c o  m*/
    fileOut.close();
}

From source file:net.dontdrinkandroot.utils.lang.SerializationUtils.java

@SuppressWarnings("unchecked")
public static <T extends Serializable> T fastClone(T object) {

    Object obj = null;//from   w w  w  .  j a v  a 2s . co  m
    try {

        /* Write the object out to a byte array */
        FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(fbos);
        out.writeObject(object);
        out.flush();
        out.close();

        /* Retrieve an input stream from the byte array and read a copy of the object back in. */
        ObjectInputStream in = new ObjectInputStream(fbos.getInputStream());
        obj = in.readObject();

    } catch (IOException e) {
        throw new SerializationException(e);
    } catch (ClassNotFoundException cnfe) {
        throw new SerializationException(cnfe);
    }

    return (T) obj;
}

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

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    try {//from w  w  w  .j a  va  2s. c o m
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        return "exception";
    }
}

From source file:com.antbrains.crf.hadoop.InstanceGenerator.java

public static String object2String(Object o) throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream so = new ObjectOutputStream(bo);
    so.writeObject(o);
    so.flush();/*from   www  .j a  v a  2s.  c  om*/
    byte[] arr = Base64.encodeBase64(bo.toByteArray());
    return new String(arr, "UTF8");
}

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

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    try {//w w w .j  ava  2s  . co  m
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        throw new IOException("Serialization error: " + e.getMessage(), e);
    }
}

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

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    try {/*ww w  . j a v  a2 s  .  com*/
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        throw WrappedIOException.wrap("Serialization error: " + e.getMessage(), e);
    }
}

From source file:homemade.apps.framework.homerlibs.utils.ObjectSerializer.java

public static String serialize(Serializable obj) {
    if (obj == null)
        return "";
    try {//w  w w .j  a  v  a2 s  . c o m
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mErrorReturn;
}

From source file:asia.stampy.common.serialization.SerializationUtils.java

/**
 * Serialize base64.//from   w w w .j a  v a 2  s  .  c  o  m
 * 
 * @param o
 *          the o
 * @return the string
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
public static String serializeBase64(Object o) throws IOException {
    SERIALIZE_LOCK.lock();
    try {
        if (o instanceof byte[])
            return Base64.encodeBase64String((byte[]) o);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);

        oos.writeObject(o);

        return Base64.encodeBase64String(baos.toByteArray());
    } finally {
        SERIALIZE_LOCK.unlock();
    }
}

From source file:com.ecyrd.jspwiki.util.Serializer.java

/**
 * Serializes a Map and formats it into a Base64-encoded String. For ease of serialization, the Map contents
 * are first copied into a HashMap, then serialized into a byte array that is encoded as a Base64 String.
 * @param map the Map to serialize//from   w  w w . j  a v  a 2  s  .  c o  m
 * @return a String representing the serialized form of the Map
 * @throws IOException If serialization cannot be done
 */
public static String serializeToBase64(Map<String, Serializable> map) throws IOException {
    // Load the Map contents into a defensive HashMap
    HashMap<String, Serializable> serialMap = new HashMap<String, Serializable>();
    serialMap.putAll(map);

    // Serialize the Map to an output stream
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bytesOut);
    out.writeObject(serialMap);
    out.close();

    // Transform to Base64-encoded String
    byte[] result = Base64.encodeBase64(bytesOut.toByteArray());
    return new String(result);
}