JMSL Chart Programmer's Guide
Actions >> Serialization  Previous Page  Contents  Next Page

Serialization

The chart tree can be serialized into a stream of bytes and recreated from that stream.

Serialization is used for lightweight persistence and for communication via sockets or Remote Method Invocation (RMI). See Sun's Java Web site for more details about serialization in general.

Serialization of JMSL classes may not be compatible with future JMSL releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of JMSL.

To serialize a chart tree, create an ObjectOutputStream and then use its writeObject method to write out the Chart object. The chart tree can be reconstructed from the stream by creating a ObjectInputStream and using its readObject method to read the Chart object.

The chart tree cannot be serialized if it contains any non-serializable objects, such as attribute values. All of the JMSL chart nodes are serializable. Image objects are not serializable, but ImageIcon objects are serializable. Standard Java Paint objects, such as TexturePaint objects, are not serializable. But the Paint objects generated by the JMSL class FillPaint are serializable.

Example

When the following code is executed without any arguments, it creates a chart and serializes it to the file sample.dat. When it is executed with the single argument -read, it reads the file sample.dat and recreates the tree.


(Download Code)

import com.imsl.chart.*;
import java.io.*;
import java.awt.*;
import javax.swing.ImageIcon;

public class SampleSerialization extends JFrameChart {
    
    private void createChart() {
        Chart chart = getChart();
        AxisXY axis = new AxisXY(chart);
        chart.getBackground().setFillType(ChartNode.FILL_TYPE_PAINT);
        Paint paint = FillPaint.checkerboard(24, Color.yellow, Color.orange);
        chart.getBackground().setFillPaint(paint);
        double y[] = {4, 6, 2, 1, 8};
        Data data = new Data(axis, y);
        data.setDataType(Data.DATA_TYPE_LINE);
        data.setLineColor("blue");
     }
    
    static private java.awt.Image loadImage(String name) {
        return new ImageIcon(Data.class.getResource(name)).getImage();
    }
    
    private void writeChart() throws IOException {
        FileOutputStream fos = new FileOutputStream("sample.dat");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(getChart());
        oos.flush();
        fos.close();
    }
    
    
    private Chart readChart() throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream("sample.dat");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Chart chart = (Chart)ois.readObject();
        fis.close();
        return chart;
    }
    
    public static void main(String argv[]) throws Exception {
        if (argv.length == 0) {
            SampleSerialization sample = new SampleSerialization();
            sample.createChart();
            sample.writeChart();
            System.exit(0);
        } else if (argv[0].equals("-read")) {
            SampleSerialization sample = new SampleSerialization();
            Chart chart = sample.readChart();
            sample.setChart(chart);
            sample.setVisible(true);
        } else {
            System.out.println("usage: java SampleSerialization [-read]");
        }
    }
}


©  Visual Numerics, Inc.  All rights reserved.  Previous Page  Contents  Next Page