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:com.bworld.manager.ObjectSerializer.java

/**
* Serialize./*from  w w  w .  j  av a2s. c  om*/
*
* @param obj the obj
* @return the string
*/
public static String serialize(Serializable obj) {
    if (obj == null)
        return "";
    try {
        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);
    }
    return null;
}

From source file:Main.java

public static byte[] toByteArray(Serializable serializable) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;//from  ww  w  .j av  a  2s . c  o  m
    try {
        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.moz.fiji.hive.utils.FijiDataRequestSerializer.java

/**
 * Serializes a data request.//from   w w w .  j  av a  2 s  .c  om
 *
 * @param dataRequest A data request.
 * @return The serialized data request.
 * @throws IOException If there is an error.
 */
public static String serialize(FijiDataRequest dataRequest) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    try {
        oos.writeObject(dataRequest);
        oos.flush();
        return Base64.encodeBase64String(baos.toByteArray());
    } finally {
        IOUtils.closeQuietly(oos);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.keyphrases.bookindexing.util.SerializationUtils.java

/**
 * @param object/*w  ww .  j  a va2  s . c  o m*/
 *          object to be serialized
 * @param filePath
 *          the object will be written at this location, directories will be
 *          created according to path
 * @throws Exception exception
 */
public static void serializeToDisk(Serializable object, String filePath) throws Exception {
    File dir = new File(FilenameUtils.getFullPathNoEndSeparator(filePath));
    if (!dir.exists()) {
        FileUtils.forceMkdir(dir);
    } else {
        if (dir.isFile()) {
            throw new IOException("Path to dir is a file!");
        }
    }
    ObjectOutputStream objOut = null;
    objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filePath)));
    objOut.writeObject(object);
    objOut.flush();
    objOut.close();
}

From source file:Main.java

public static Object clone(Object object) throws Exception {
    Object copy = null;/*from  w w  w .  ja v  a2 s. co m*/

    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        oos.flush();

        ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
        ois = new ObjectInputStream(bin);
        copy = ois.readObject();
    } finally {
        if (ois != null)
            ois.close();
        if (oos != null)
            oos.close();
    }
    return copy;
}

From source file:com.evolveum.midpoint.util.SerializationUtil.java

public static String toString(Object object) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(object);
    objectOutputStream.close();/* w w  w  .  ja  v a  2 s .c om*/
    return new String(Base64.encodeBase64(byteArrayOutputStream.toByteArray()));
}

From source file:br.com.cantina.utils.Backup.java

public String getHash() {
    byte b[] = null;
    try (ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bout)) {
        out.writeObject(this);
        bout.flush();//from  www.  j ava 2  s .  c o m
        b = bout.toByteArray();
    } catch (IOException ex) {
        Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (b != null)
        return DigestUtils.md5Hex(b);
    return null;
}

From source file:io.smartspaces.scheduling.quartz.orientdb.internal.util.SerialUtils.java

public static byte[] serialize(Calendar calendar) throws JobPersistenceException {
    // ToDO(keith): Serialize better than Java serialization.
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    try {/*ww w .  j  ava  2  s. co m*/
        ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
        objectStream.writeObject(calendar);
        objectStream.close();
        return byteStream.toByteArray();
    } catch (IOException e) {
        throw new JobPersistenceException("Could not serialize Calendar.", e);
    }
}

From source file:com.ikon.util.Serializer.java

/**
 * @param obj/*from ww  w  .j  a va2 s . c om*/
 */
public static void write(String filename, Object obj) {
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;

    try {
        fos = new FileOutputStream(Config.HOME_DIR + File.separator + filename + ".ser");
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
    } catch (FileNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    } finally {
        IOUtils.closeQuietly(oos);
        IOUtils.closeQuietly(fos);
    }
}

From source file:jp.queuelinker.system.util.SerializeUtil.java

/**
 * Serializes an object to a byte array.
 * @param obj/*from  w ww .j a  va2  s.c  om*/
 * @return
 */
public static byte[] serializeToBytes(final Object obj) {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    try {
        ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
        objStream.writeObject(obj);
        objStream.close();
    } catch (IOException e) {
        logger.error("Serialization Error. The class " + obj.getClass().getName()
                + " may not implement Serializable interface");
    }
    return byteStream.toByteArray();
}