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.pcms.core.util.ObjectUtil.java

public static byte[] serialize(Object object) {
    ObjectOutputStream oos = null;
    ByteArrayOutputStream baos = null;
    try {//from w ww.j  a va  2 s  .c om
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        byte[] bytes = baos.toByteArray();
        return bytes;
    } catch (Exception e) {
        LOG.error(e.getMessage());
    }
    return null;
}

From source file:com.talis.storage.s3.cache.RedisChunkHandler.java

public static byte[] serialize(S3Object chunk) throws IOException {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
    objStream.writeObject(chunk);
    objStream.flush();/*  w w  w  .  jav  a 2  s .c  om*/
    objStream.close();
    return byteStream.toByteArray();
}

From source file:RedisCache.java

/** Write the object to a Base64 string. */
private static String toString(Serializable o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();//  w  w w. j a  va  2 s  . c  o  m
    return new String(Base64.encodeBase64(baos.toByteArray()));
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.StorageImpl.java

private static void save() {
    try {/*from  w w w . j av  a 2s.c o m*/
        final File file = new File(System.getProperty("user.home"), "htmlunit.storage");
        final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
        out.writeObject(SINGLETON_);
        out.close();
    } catch (final IOException e) {
        LOG.info("Could not save storage", e);
    }
}

From source file:Main.java

public static boolean saveBeanToFile(String filePath, Object bean) {
    File file = new File(filePath);
    ObjectOutputStream outputStream = null;
    if (file.exists()) {
        file.delete();/* ww w  . j a  va 2s .  co m*/
    }

    try {
        outputStream = new ObjectOutputStream(new FileOutputStream(file));
        outputStream.writeObject(bean);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuiltly(outputStream);
    }

    return true;
}

From source file:Main.java

static byte[] toBlob(Serializable object) throws IOException {
    ObjectOutputStream oos = null;
    ByteArrayOutputStream bos;//from ww  w.j  a v a 2 s.co m
    byte[] result = null;
    try {
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        result = bos.toByteArray();
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:de.bps.webservices.clients.onyxreporter.HashMapWrapper.java

/**
 * Serializes the given object o and encodes the result as non-chunked base64.
 * //  w  ww.j ava2 s . com
 * @param objectToSerializeAndEncode
 * @return
 * @throws OnyxReporterException
 */
private static final String serialize(final Object objectToSerializeAndEncode) throws OnyxReporterException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(objectToSerializeAndEncode);
        oos.close();
        oos = null;
        return Base64.encodeBase64String(baos.toByteArray());
    } catch (final IOException e) {
        throw new OnyxReporterException("Could not serialize object!", e);
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
        } catch (final IOException e) {
        }
    }
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java

/**
 * Serialize a java Object into a byte array
 * //from w  ww .  j  av  a2  s .c om
 * @param objToSerialize - Java Object to be serialized
 * @return a byte array serialized from the java Object
 * @throws IngestionException
 */
public static byte[] serializeObjToBinArr(Object objToSerialize) throws IngestionException {
    ByteArrayOutputStream binOut = new ByteArrayOutputStream();
    try {
        ObjectOutputStream out = new ObjectOutputStream(binOut);
        out.writeObject(objToSerialize);
        out.close();
        binOut.close();
    } catch (IOException e) {
        throw new IngestionException(e);
    }
    return binOut.toByteArray();
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java

/**
 * Serialize a StackExchangeThread into a byte array
 * //from   ww  w .  ja v a2  s  . c o  m
 * @param threadToSerialize - StackExchangeThread to be serialized
 * @return a byte array serialized from the StackExchangeThread
 * @throws IngestionException
 */
public static byte[] serializeThreadToBinArr(StackExchangeThread threadToSerialize) throws IngestionException {
    ByteArrayOutputStream binOut = new ByteArrayOutputStream();
    try {
        ObjectOutputStream out = new ObjectOutputStream(binOut);
        out.writeObject(threadToSerialize);
        out.close();
        binOut.close();
    } catch (IOException e) {
        throw new IngestionException(e);
    }
    return binOut.toByteArray();
}

From source file:com.oprisnik.semdroid.utils.FileUtils.java

public static void writeObjectToStream(Object object, OutputStream output) throws IOException {
    ObjectOutputStream out = null;
    try {//from   w  w  w. j a  v a2  s  . co m
        out = new ObjectOutputStream(new BufferedOutputStream(output));
        out.writeObject(object);
        out.flush();
    } finally {
        IOUtils.closeQuietly(out);
    }
}