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.github.tell.codec.Base64Serializer.java

public String encodeToString(final Serializable o) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);//from  w ww . j av a  2 s  .  c om
    oos.close();
    final byte[] encoded = baos.toByteArray();
    final Base64 encoder = new Base64();
    return encoder.encodeToString(encoded);
}

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 {// w  w w. jav a  2 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:edu.cmu.cs.lti.ark.util.SerializedObjects.java

private static ObjectOutputStream getObjectOutputStream(String outFile) throws IOException {
    OutputStream file = getOutputStream(outFile);
    return new ObjectOutputStream(new BufferedOutputStream(file));
}

From source file:com.gdc.nms.web.mibquery.wizard.ciscavate.cjwizard.FlatWizardSettings.java

/**
 * Serialize the current instance of {@link FlatWizardSettings} to the given
 * filename.//from w w w  .  j ava  2 s  .c  om
 * 
 * @param filename
 *           The filename.
 */
public void serialize(String filename) {
    ObjectOutputStream out = null;
    FileOutputStream fout = null;
    try {
        fout = new FileOutputStream(filename);
        out = new ObjectOutputStream(fout);
        out.writeObject(this);
    } catch (IOException ioe) {
        log.error("Error writing settings", ioe);
    } finally {
        if (null != out) {
            try {
                out.close();
            } catch (IOException ioe) {
                log.error("Error closing output stream", ioe);
            }
        }
        if (null != fout) {
            try {
                fout.close();
            } catch (IOException ioe) {
                log.error("Error closing file", ioe);
            }
        }
    }
}

From source file:com.icantrap.collections.dawg.Dawg.java

/**
 * Writes an instance of a dawg to an OutputStream.  Once the data is written to the OutputStream, it is flushed, but
 * the stream is not closed.//from  www .j av a 2  s  .c  om
 *
 * @param os the OutputStream to write the dawg to
 * @throws IOException if writing the dawg to the stream causes an IOException
 */
public void store(OutputStream os) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(os, 8 * 1024);
    ObjectOutputStream oos = new ObjectOutputStream(bos);

    oos.writeObject(nodes);
    oos.flush();
}

From source file:net.sf.ehcache.distribution.EventMessageTest.java

/**
 * test serialization and deserialization of EventMessage.
 *//*ww  w. j a  v a  2  s .co  m*/
public void testSerialization() throws IOException, ClassNotFoundException {

    EventMessage eventMessage = new EventMessage(EventMessage.PUT, "key", new Element("key", "element"));

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bout);
    oos.writeObject(eventMessage);
    byte[] serializedValue = bout.toByteArray();
    oos.close();
    EventMessage eventMessage2 = null;
    ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
    ObjectInputStream ois = new ObjectInputStream(bin);
    eventMessage2 = (EventMessage) ois.readObject();
    ois.close();

    //Check after Serialization
    assertEquals("key", eventMessage2.getSerializableKey());
    assertEquals("element", eventMessage2.getElement().getObjectValue());
    assertEquals(EventMessage.PUT, eventMessage2.getEvent());
    assertTrue(eventMessage2.isValid());

}

From source file:id.co.nlp.MachineTranslation.Utils.Util.java

public static void serializing(String pathfile, Object object) throws FileNotFoundException, IOException {
    FileOutputStream fileOut = new FileOutputStream(pathfile);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(object);// w  w w  .ja  v  a2  s .  c  om
    out.close();
    fileOut.close();
}

From source file:com.jpeterson.littles3.bo.AllUsersGroupTest.java

/**
 * Test that an instance is serializable.
 *//*from   ww w  . ja  va  2s . c  om*/
public void test_serialization() {
    AllUsersGroup group, reconstitutedGroup;
    ByteArrayInputStream bais;
    ByteArrayOutputStream baos;
    ObjectInputStream ois;
    ObjectOutputStream oos;

    group = AllUsersGroup.getInstance();

    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);

        oos.writeObject(group);

        bais = new ByteArrayInputStream(baos.toByteArray());
        ois = new ObjectInputStream(bais);

        reconstitutedGroup = (AllUsersGroup) ois.readObject();

        assertEquals("Unexpected value", group, reconstitutedGroup);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    }
}

From source file:jatoo.weather.JaTooWeatherCache.java

public boolean save() {

    try (ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(CACHE_FILE))) {
        stream.writeObject(CACHE);//from   ww w  .  ja  v  a2  s .  co  m
    }

    catch (IOException e) {
        LOGGER.error("failed to save the cache", e);
        return false;
    }

    return true;
}

From source file:com.manning.blogapps.chapter11.rome.DiskFeedInfoCache.java

public void setFeedInfo(URL url, SyndFeedInfo feedInfo) {

    String fileName = cachePath + File.separator + "feed_" + url.hashCode();
    FileOutputStream fos;//from  w w w  .ja  v  a2s  . com
    try {
        fos = new FileOutputStream(fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(feedInfo);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        // Error writing to cahce is fatal
        throw new RuntimeException("Attempting to write to cache", e);
    }
}