Example usage for java.io ObjectOutput close

List of usage examples for java.io ObjectOutput close

Introduction

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

Prototype

public void close() throws IOException;

Source Link

Document

Closes the stream.

Usage

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

/**
* Serialize a {@link org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder} to a byte array.
* @param topology//from  w ww.j a v  a  2 s.  c  om
* @return byte[]
* @throws IOException
*/
public static byte[] serializeToByteArray(Topology topology) throws IOException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(topology);

        return bos.toByteArray();

    } finally {
        if (out != null) {
            out.close();
        }
        bos.close();
    }

}

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

/**
 * Serialize a {@link org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder} to a byte array.
 * @param serializableObj//from w w w .j ava  2 s .  com
 * @return byte[] 
 * @throws IOException
 */
public static byte[] serializeToByteArray(FasterLookUpDataHolder serializableObj) throws IOException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(serializableObj);

        return bos.toByteArray();

    } finally {
        if (out != null) {
            out.close();
        }
        bos.close();
    }

}

From source file:VASSAL.tools.io.IOUtils.java

/**
 * Close an {@link ObjectOutput} unconditionally. Equivalent to
 * calling <code>o.close()</code> when <code>o</code> is nonnull.
 * {@link IOException}s are swallowed, as there is generally
 * nothing that can be done about exceptions on closing.
 *
 * @param o a (possibly <code>null</code>) <code>ObjectOutput</code>
 *//*from  ww w  . jav a  2 s .c  o m*/
public static void closeQuietly(ObjectOutput o) {
    if (o == null)
        return;

    try {
        o.close();
    } catch (IOException e) {
        // ignore
    }
}

From source file:Main.java

public static byte[] toByteArray(Object obj) {
    byte[] returnBytes = null;
    ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
    ObjectOutput objectOut = null;
    try {//w  ww.j a v a2 s  .  co 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;
}

From source file:Main.java

/**
 * Convert serializable object to bytes.
 *
 * @param object object/*from  w  w  w.jav  a2s . com*/
 * @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[] serialize(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    byte[] bytes = null;
    try {/*ww w .  j  a  va 2  s. c  o  m*/
        out = new ObjectOutputStream(bos);
        out.writeObject(o);
        bytes = bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            bos.close();
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return bytes;
}

From source file:Main.java

public static byte[] toByteArray(Serializable serializable) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {/* w ww.  ja  va2  s. c  om*/
        out = new ObjectOutputStream(bos);
        out.writeObject(serializable);
        byte[] bytes = bos.toByteArray();
        return bytes;
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        try {
            out.close();
            bos.close();
        } catch (IOException e) {
            //can be ignored;
        }

    }
}

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

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

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

private static final ByteBuffer javaObject(Object object) throws SQLException {
    byte[] bytes = null;
    try {//  w  w w .  java2  s  .  co  m
        // 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: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  ww  .  jav  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();
    }

}