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:com.floragunn.searchguard.util.SecurityUtil.java

public static String encryptAndSerializeObject(final Serializable object, final SecretKey key) {

    if (object == null) {
        throw new IllegalArgumentException("object must not be null");
    }/*from   ww w .j  a v  a 2s.c  o m*/

    try {
        final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        final SealedObject sealedobject = new SealedObject(object, cipher);
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(sealedobject);
        final byte[] bytes = bos.toByteArray();
        return BaseEncoding.base64().encode(bytes);
    } catch (final Exception e) {
        log.error(e.toString(), e);
        throw new ElasticsearchException(e.toString());
    }
}

From source file:com.griddynamics.jagger.util.SerializationUtils.java

public static byte[] serialize(Object obj) {
    ObjectOutputStream ous = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from w  w w  .j a v a2  s .  co  m*/
        if (useJBoss) {
            ous = new JBossObjectOutputStream(baos);
        } else {
            baos = new ByteArrayOutputStream();
            ous = new ObjectOutputStream(baos);
        }
        ous.writeObject(obj);

        return baos.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException("Error during " + obj + " serialization", e);
    } finally {
        try {
            Closeables.close(ous, true);
        } catch (IOException e) {
            log.warn("IOException should not have been thrown.", e);
        }
        try {
            Closeables.close(baos, true);
        } catch (IOException e) {
            log.warn("IOException should not have been thrown.", e);
        }
    }
}

From source file:gov.va.chir.tagline.dao.FileDao.java

private static void saveFeatures(final File file, final Collection<Feature> features) throws IOException {
    final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));

    for (Feature feature : features) {
        out.writeObject(feature);
    }/*w  w w.  j a  v a  2  s  . c o  m*/

    out.close();
}

From source file:com.clustercontrol.plugin.impl.AsyncTask.java

/**
 * SerializableBinary???//w  w w . jav  a  2  s. c o m
 * @param obj Serializable
 * @return ??Binary
 * @throws IOException
 */
public static byte[] encodeBinary(Serializable obj) throws IOException {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    byte[] bytes = null;

    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        bytes = baos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (baos != null) {
            baos.close();
        }
    }

    return bytes;
}

From source file:lirmm.inria.fr.math.TestUtils.java

/**
 * Serializes an object to a bytes array and then recovers the object from the bytes array.
 * Returns the deserialized object.//from w w  w .  java  2  s.  c  o m
 *
 * @param o  object to serialize and recover
 * @return  the recovered, deserialized object
 */
public static Object serializeAndRecover(Object o) {
    try {
        // serialize the Object
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream so = new ObjectOutputStream(bos);
        so.writeObject(o);

        // deserialize the Object
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream si = new ObjectInputStream(bis);
        return si.readObject();
    } catch (IOException ioe) {
        return null;
    } catch (ClassNotFoundException cnfe) {
        return null;
    }
}

From source file:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * The Method toByteArray() serialize an Object to byte array.
 *
 * @param <T>//from   w  ww . j  av  a 2 s.  com
 *            the generic type of the given object
 * @param object
 *            The Object to convert into a byte array.
 * @return The byte array from the Object.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static <T> byte[] toByteArray(final T object) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {

        byteArrayOutputStream = new ByteArrayOutputStream(FileConstants.KILOBYTE);

        byteArrayOutputStream.reset();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(object);
        objectOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    } finally {
        StreamExtensions.closeOutputStream(byteArrayOutputStream);
        StreamExtensions.closeOutputStream(objectOutputStream);
    }
}

From source file:de.alpharogroup.io.SerializedObjectUtils.java

/**
 * The Method toByteArray() serialize an Object to byte array.
 *
 * @param <T>//from w w  w . java2  s.  c om
 *            the generic type of the given object
 * @param object
 *            The Object to convert into a byte array.
 * @return The byte array from the Object.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static <T> byte[] toByteArray(final T object) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {

        byteArrayOutputStream = new ByteArrayOutputStream(FileConstants.KILOBYTE);

        byteArrayOutputStream.reset();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(object);
        objectOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    } finally {
        StreamUtils.closeOutputStream(byteArrayOutputStream);
        StreamUtils.closeOutputStream(objectOutputStream);
    }
}

From source file:com.hazelcast.simulator.utils.FileUtils.java

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

    FileOutputStream stream = null;
    ObjectOutputStream outputStream = null;
    try {//from   w  w w.j  av  a2  s.c  o m
        stream = new FileOutputStream(tmpFile);
        outputStream = new ObjectOutputStream(stream);
        outputStream.writeObject(o);
    } catch (IOException e) {
        throw new FileUtilsException(e);
    } finally {
        closeQuietly(outputStream);
        closeQuietly(stream);
    }

    rename(tmpFile, file);
}

From source file:com.feilong.commons.core.lang.ObjectUtil.java

/**
 * ?.//www. j av a  2 s.  c  om
 *
 * @param serializable
 *            the object
 * @return the int
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @see ByteArrayOutputStream#size()
 * @since 1.0.7
 */
//XXX ?check,?
public static int size(Object serializable) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(serializable);
    objectOutputStream.close();
    return byteArrayOutputStream.size();
}

From source file:com.stromberglabs.jopensurf.Surf.java

public static void saveToFile(Surf surf, String file) {
    try {/*from   w w w. j a  v a 2 s  .c  o  m*/
        ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(file));
        stream.writeObject(surf);
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}