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.github.gfernandez598.swf.conversation.optforrepl.ReplicationOptimizedSessionBindingConversationManagerTest.java

private byte[] passivate(SharedAttributeMap<Object> session) throws Exception {
    // session is serialized out
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(bout);
    oout.writeObject(session);
    return bout.toByteArray();
}

From source file:com.netflix.priam.identity.DoubleRing.java

/**
 * Backup the current state in case of failure
 *///from  ww  w .  jav  a 2 s .c o m
public void backup() throws IOException {
    // writing to the backup file.
    TMP_BACKUP_FILE = File.createTempFile("Backup-instance-data", ".dat");
    OutputStream out = new FileOutputStream(TMP_BACKUP_FILE);
    ObjectOutputStream stream = new ObjectOutputStream(out);
    try {
        stream.writeObject(filteredRemote(factory.getAllIds(config.getAppName())));
        logger.info("Wrote the backup of the instances to: " + TMP_BACKUP_FILE.getAbsolutePath());
    } finally {
        IOUtils.closeQuietly(stream);
        IOUtils.closeQuietly(out);
    }
}

From source file:com.jaspersoft.jasperserver.core.util.TolerantObjectWrapper.java

private void writeObject(ObjectOutputStream stream) throws ClassNotFoundException, IOException {
    if (testIsSerializable(obj)) {
        stream.writeObject(obj);
    } else {/*www .java 2s  . com*/
        if (log.isDebugEnabled())
            log.debug("Non serialized object on session " + obj.getClass());
        stream.writeObject(new MissingObject("MissingValue"));
    }

}

From source file:common.services.generic.GenericCacheService.java

@Override
public boolean saveToFile() {
    if (cached_object == null)
        return false;
    try {//from w ww .  java2s  .c om
        File f = new File(path, "cache.tmp");
        if (f.exists())
            f.delete();
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        ObjectOutputStream so = new ObjectOutputStream(fo);
        so.writeObject(cached_object);
        so.flush();
        so.close();
        fo.close();
        return true;
    } catch (IOException ex) {
        logger.error("failed to save cache path = " + path, ex);
        return false;
    }
}

From source file:com.limegroup.gnutella.licenses.WeedLicenseTest.java

public void testSerializeAndDeserialize() throws Exception {
    License l = new StubWeedLicense("3", "4", "page");
    assertEquals(null, l.getLicense());/*from w w w.j  a v  a2s. c  o  m*/
    assertEquals("http://weedshare.com/company/policies/summary_usage_rights.aspx",
            l.getLicenseDeed(null).toString());
    assertEquals("http://www.weedshare.com/license/verify_usage_rights.aspx?versionid=4&contentid=3",
            l.getLicenseURI().toString());
    assertEquals("Details unknown.", l.getLicenseDescription(null));
    assertFalse(l.isVerified());
    assertFalse(l.isValid(null));

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(l);

    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream in = new ObjectInputStream(bin);
    l = (License) in.readObject();
    assertEquals(null, l.getLicense()); // CHANGE -- not serialized (but unused anyway)
    assertEquals("http://weedshare.com/company/policies/summary_usage_rights.aspx",
            l.getLicenseDeed(null).toString());
    assertEquals(null, l.getLicenseURI()); // CHANGE -- not serialized
    assertEquals("Details unknown.", l.getLicenseDescription(null));
    assertTrue(l.isVerified()); // CHANGE -- becomes verified
    assertFalse(l.isValid(null));

    // Now try with a full out parsed License.
    l = new StubWeedLicense("3", "4", xml(true, "Sammy B", "Blues In G", "$4.20"));
    l.verify(null);
    assertEquals(null, l.getLicense());
    assertTrue(l.isVerified());
    assertTrue(l.isValid(null));
    assertEquals("http://weedshare.com/company/policies/summary_usage_rights.aspx",
            l.getLicenseDeed(null).toString());
    assertEquals("Artist: Sammy B\nTitle: Blues In G\nPrice: $4.20", l.getLicenseDescription(null));

    bout = new ByteArrayOutputStream();
    out = new ObjectOutputStream(bout);
    out.writeObject(l);

    bin = new ByteArrayInputStream(bout.toByteArray());
    in = new ObjectInputStream(bin);
    l = (License) in.readObject();
    assertEquals(null, l.getLicense()); // CHANGE -- not serialized (but unused)
    assertTrue(l.isVerified());
    assertTrue(l.isValid(null));
    assertEquals("http://weedshare.com/company/policies/summary_usage_rights.aspx",
            l.getLicenseDeed(null).toString());
    assertEquals("Artist: Sammy B\nTitle: Blues In G\nPrice: $4.20", l.getLicenseDescription(null));
}

From source file:net.nicholaswilliams.java.licensing.ObjectSerializer.java

/**
 * Serializes the {@link Serializable} object passed and returns it as a byte array.
 *
 * @param object The object to serialize
 * @return the byte stream with the object serialized in it.
 * @throws ObjectSerializationException if an I/O exception occurs while serializing the object.
 *//* ww w  . j  a v  a2s . c o  m*/
public final byte[] writeObject(Serializable object) throws ObjectSerializationException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    ObjectOutputStream stream = null;
    try {
        stream = new ObjectOutputStream(bytes);
        stream.writeObject(object);
    } catch (IOException e) {
        throw new ObjectSerializationException(e);
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException ignore) {
        }
    }

    return bytes.toByteArray();
}

From source file:edu.uga.cs.fluxbuster.clustering.hierarchicalclustering.Dendrogram.java

/**
 * Creates a deep copy of any object./*from   ww  w. j a va  2s .  c  om*/
 *
 * @param o the object to copy
 * @return the copy
 */
public Object deepCopy(Object o) {
    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(o);
        out.close();

        byte[] data = byteOut.toByteArray();
        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));
        Object copy = in.readObject();
        in.close();

        return copy;
    } catch (Exception e) {
        if (log.isErrorEnabled()) {
            log.error("Error performing deep copy.", e);
        }
    }

    return null;
}

From source file:edu.yale.cs.hadoopdb.connector.DBInputSplit.java

/**
 * Serializes DBChunk //from   w  ww  . java 2s  . c o  m
 */
private void serializeChunk(DBChunk chunk, DataOutput out) throws IOException {
    ByteArrayOutputStream byte_stream = new ByteArrayOutputStream();
    ObjectOutputStream object_stream = new ObjectOutputStream(byte_stream);
    object_stream.writeObject(chunk);
    object_stream.close();

    byte[] buf = byte_stream.toByteArray();
    BytesWritable bw = new BytesWritable(buf);
    bw.write(out);
}

From source file:net.larry1123.util.api.world.blocks.BlockPropertyStorage.java

public JSONObject toJSONObject() {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("keyName", getName());
    try {//from www .  jav  a  2  s .c  o m
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(getValue());
        String base64Object = Base64.encodeBase64String(byteArrayOutputStream.toByteArray());
        jsonObject.put("value", base64Object);
    } catch (IOException e) {
        e.printStackTrace();
        // TODO throw
    }
    jsonObject.put("blockTypeMachineName", blockTypeMachineName);
    jsonObject.put("blockTypeData", blockTypeData);
    return jsonObject;
}

From source file:natalia.dymnikova.cluster.scheduler.impl.Codec.java

public byte[] packObject(final Object funct) {
    try {/*from ww  w .  jav a2s . c o  m*/
        // TODO make sense to reuse Codec and inner ObjectOutputStream at least for a single RemoteObservable
        final ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        final ObjectOutputStream os = new ObjectOutputStream(arrayOutputStream);
        os.writeObject(funct);
        os.close();
        return arrayOutputStream.toByteArray();
    } catch (final IOException e) {
        throw propagateUnchecked(e);
    }
}