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:org.apache.stratos.autoscaler.service.io.Serializer.java

public static void serialize(Object serializableObj, String filePath) throws IOException {

    File outFile = new File(filePath);
    ObjectOutput ObjOut = null;

    try {/*  w  w  w. j  a  v  a 2  s  .  c o  m*/

        if (outFile.createNewFile()) {
            log.debug("Serialization file is created at " + filePath);
        } else {
            log.debug("Serialization file is already existing at " + filePath);
        }

        ObjOut = new ObjectOutputStream(new FileOutputStream(outFile));
        ObjOut.writeObject(serializableObj);

    } catch (IOException e) {
        log.error("Failed to serialize the object " + serializableObj.toString() + " to file " + filePath, e);
        throw e;

    } finally {
        ObjOut.close();
    }

}

From source file:com.all.client.itunes.ByteObject.java

public static byte[] toBytes(Serializable content) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out;
    try {//ww w  . j a  va  2 s  .  com
        out = new ObjectOutputStream(bos);
        out.writeObject(content);
        out.close();
    } catch (Exception e) {
        log.error(e, e);
        return null;
    }
    return bos.toByteArray();
}

From source file:io.bitsquare.common.util.Utilities.java

public static byte[] serialize(Serializable object) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    byte[] result = null;
    try {//from w ww . j  a  v a 2  s.  c  om
        out = new ObjectOutputStream(bos);
        out.writeObject(object);
        out.flush();
        result = bos.toByteArray().clone();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ignore) {
        }
        try {
            bos.close();
        } catch (IOException ignore) {
        }
    }
    return result;
}

From source file:org.apache.stratos.cloud.controller.persist.Serializer.java

public static void serializeToFile(Object serializableObj, String filePath) throws IOException {

    File outFile = new File(filePath);
    ObjectOutput objOut = null;
    FileOutputStream fileOutputStream = null;

    try {/*  w ww.j  av  a 2  s  .c  o m*/

        if (outFile.createNewFile()) {
            log.debug("Serialization file is created at " + filePath);
        } else {
            log.debug("Serialization file is already existing at " + filePath);
        }
        fileOutputStream = new FileOutputStream(outFile);
        objOut = new ObjectOutputStream(fileOutputStream);
        objOut.writeObject(serializableObj);

    } catch (IOException e) {
        log.error("Failed to serialize the object " + serializableObj.toString() + " to file " + filePath, e);
        throw e;

    } finally {
        if (objOut != null) {
            objOut.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }

}

From source file:org.apache.cassandra.cql.jdbc.HandleObjects.java

private static final ByteBuffer javaObject(Object object) throws SQLException {
    byte[] bytes = null;
    try {//from w  ww.  j a va 2  s.  com
        // Serialize to a byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(object);
        out.close();

        // Get the bytes of the serialized object
        bytes = bos.toByteArray();
    } catch (IOException e) {
        throw new SQLNonTransientException("Problem serializing the Java object", e);
    }

    return ByteBuffer.wrap(bytes);
}

From source file:net.brtly.monkeyboard.api.plugin.Bundle.java

/**
 * Convert a Bundle object to a byte array that can be deserialized by
 * {@link #fromByteArray(byte[])}//from   w  ww.  j  a  v  a 2  s .co  m
 * 
 * @param b
 * @return
 * @throws IOException
 */
public static byte[] toByteArray(Bundle b) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(b);
        return bos.toByteArray();
    } finally {
        out.close();
        bos.close();
    }
}

From source file:com.hazelcast.stabilizer.Utils.java

public static void writeObject(Object o, File file) {
    File tmpFile = new File(file.getParent(), file.getName() + ".tmp");

    try {/*from  w ww . j  av  a 2 s  .c  o m*/
        final FileOutputStream fous = new FileOutputStream(tmpFile);
        try {
            ObjectOutput output = new ObjectOutputStream(fous);
            output.writeObject(o);
        } finally {
            Utils.closeQuietly(fous);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    if (!tmpFile.renameTo(file)) {
        throw new RuntimeException(
                format("Could not rename [%s] to [%s]", tmpFile.getAbsolutePath(), file.getAbsolutePath()));
    }
}

From source file:com.swingtech.commons.util.ClassUtil.java

public static byte[] serializeObject(final Object object) {
    ObjectOutput out = null;
    byte[] buf = null;

    if (object == null) {
        return null;
    }/*  w  w  w  . j  a  v a2s.co  m*/

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

        // Get the bytes of the serialized object
        buf = bos.toByteArray();
    } catch (final Exception e) {
        throw new RuntimeException("Error trying to serializeObject the following object:  " + object, e);
    }

    return buf;
}

From source file:org.apache.openjpa.kernel.DetachManager.java

/**
 * Used by classes that externalize detached state.
 *
 * @return whether to use a detached state manager
 *//* w  w w .  jav a 2 s. c o  m*/
static boolean writeDetachedState(StateManagerImpl sm, ObjectOutput out, BitSet idxs) throws IOException {
    if (!sm.isPersistent()) {
        out.writeObject(null); // state
        out.writeObject(null); // sm
        return false;
    }

    // dirty state causes flush
    flushDirty(sm);

    Broker broker = sm.getBroker();
    preDetach(broker, sm, idxs, false, true);

    // write detached state object and state manager
    DetachOptions opts = broker.getConfiguration().getDetachStateInstance();
    if (!opts.getDetachedStateManager() || !useDetachedStateManager(sm, opts)) {
        out.writeObject(getDetachedState(sm, idxs));
        out.writeObject(null);
        return false;
    }
    out.writeObject(null);
    out.writeObject(new DetachedStateManager(sm.getPersistenceCapable(), sm, idxs, opts.getAccessUnloaded(),
            broker.getMultithreaded()));
    return true;
}

From source file:MainClass.java

public void writeExternal(ObjectOutput out) throws IOException {
    System.out.println("MyBean.writeExternal");
    out.writeObject(s);
    out.writeInt(i);//from   ww w  .  j  a  v  a 2 s .c  o  m
}