Example usage for java.io ObjectOutput writeObject

List of usage examples for java.io ObjectOutput writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutput writeObject.

Prototype

public void writeObject(Object obj) throws IOException;

Source Link

Document

Write an object to the underlying storage or stream.

Usage

From source file:org.jfree.chart.demo.SerializationTest1.java

/**
 * Constructs a new demonstration application.
 *
 * @param title  the frame title./*  www. j  ava 2s .  c  o  m*/
 */
public SerializationTest1(final String title) {

    super(title);
    this.series = new TimeSeries("Random Data", Millisecond.class);
    TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
    JFreeChart chart = createChart(dataset);

    // SERIALIZE - DESERIALIZE for testing purposes
    JFreeChart deserializedChart = null;

    try {
        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        final ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(chart);
        out.close();
        chart = null;
        dataset = null;
        this.series = null;
        System.gc();

        final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        deserializedChart = (JFreeChart) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    final TimeSeriesCollection c = (TimeSeriesCollection) deserializedChart.getXYPlot().getDataset();
    this.series = c.getSeries(0);
    // FINISHED TEST

    final ChartPanel chartPanel = new ChartPanel(deserializedChart);
    final JButton button = new JButton("Add New Data Item");
    button.setActionCommand("ADD_DATA");
    button.addActionListener(this);

    final JPanel content = new JPanel(new BorderLayout());
    content.add(chartPanel);
    content.add(button, BorderLayout.SOUTH);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(content);

}

From source file:org.jfree.experimental.chart.annotations.junit.XYTitleAnnotationTests.java

/**
 * Serialize an instance, restore it, and check for equality.
 */// www .  j  a  v a 2 s. c  o  m
public void testSerialization() {
    TextTitle t = new TextTitle("Title");
    XYTitleAnnotation a1 = new XYTitleAnnotation(1.0, 2.0, t);
    XYTitleAnnotation a2 = null;
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(a1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        a2 = (XYTitleAnnotation) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(a1, a2);
}

From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java

public String toBase64() {
    try {//  w ww  . ja v  a 2 s. co m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ZipOutputStream zip = new ZipOutputStream(bos);
            try {
                zip.setLevel(9);
                zip.putNextEntry(new ZipEntry("entry"));
                try {
                    ObjectOutput out = new ObjectOutputStream(zip);
                    try {
                        out.writeObject(this);
                    } finally {
                        out.flush();
                    }
                } finally {
                    zip.closeEntry();
                }
                zip.flush();
                return Base64.encodeBase64URLSafeString(bos.toByteArray());
            } finally {
                zip.close();
            }
        } finally {
            bos.close();
        }
    } catch (IOException e) {
        log.log((Level.SEVERE), "IO Error while serializing member", e);
        return null;
    }
}

From source file:com.weibo.api.motan.codec.AbstractCodec.java

protected void serialize(ObjectOutput output, Object message, Serialization serialize) throws IOException {
    if (message == null) {
        output.writeObject(null);
        return;/*from  w  ww. j  ava2s .c o m*/
    }

    output.writeObject(serialize.serialize(message));
}

From source file:org.jfree.data.xy.junit.MatrixSeriesCollectionTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from w  w w .  j av  a  2s . c  om*/
public void testSerialization() {
    MatrixSeries s1 = new MatrixSeries("Series", 2, 3);
    s1.update(0, 0, 1.1);
    MatrixSeriesCollection c1 = new MatrixSeriesCollection();
    c1.addSeries(s1);
    MatrixSeriesCollection c2 = null;

    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(c1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        c2 = (MatrixSeriesCollection) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(c1, c2);
}

From source file:com.splicemachine.derby.stream.spark.SparkExportDataSetWriter.java

public void writeExternal(ObjectOutput out) throws IOException {
    out.writeUTF(directory);/*ww  w.jav a 2 s. com*/
    out.writeObject(exportFunction);
    out.writeObject(rdd);
}

From source file:org.jfree.data.xy.junit.XYBarDatasetTest.java

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from   w w w.j a v  a  2s  .c o m*/
public void testSerialization() {
    DefaultXYDataset d1 = new DefaultXYDataset();
    double[] x1 = new double[] { 1.0, 2.0, 3.0 };
    double[] y1 = new double[] { 4.0, 5.0, 6.0 };
    double[][] data1 = new double[][] { x1, y1 };
    d1.addSeries("S1", data1);
    XYBarDataset bd1 = new XYBarDataset(d1, 5.0);
    XYBarDataset bd2 = null;
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(bd1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        bd2 = (XYBarDataset) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(bd1, bd2);
}

From source file:com.hp.saas.agm.rest.client.SessionContext.java

public void save(File targetFile) {
    ObjectOutput out = null;
    try {//from   ww  w .  j a v a2  s  . co  m
        out = new ObjectOutputStream(new FileOutputStream(targetFile));
        out.writeObject(this);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                logger.log(Level.SEVERE, "IOException thrown while closing stream.", e);
            }
        }
    }

}

From source file:wvec.WordVec.java

public byte[] getBytes() throws IOException {
    byte[] byteArray;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        ObjectOutput out;
        out = new ObjectOutputStream(bos);
        out.writeObject(this);
        byteArray = bos.toByteArray();/*w w w  .  j a  va 2s . c  o  m*/
        out.close();
    }
    return byteArray;
}

From source file:com.splicemachine.derby.stream.spark.SparkScanSetBuilder.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    super.writeExternal(out);
    out.writeUTF(tableName);//  w ww .ja  v a 2  s. c  o  m
    out.writeObject(dsp);
}