Java Reference
In-Depth Information
a method like the following to save a histogram object to a file:
static public void saveHistogram (Histogram histogram, File
file) {
FileOutputStream fos = new FileOutputStream (file);
ObjectOutputStream oos = new ObjectOutputStream (fos);
oos.writeObject (histogram);
oos.close ();
}
Similarly, to read a histogram back in from a file we could use a method as
follows:
static public Histogram getHistogram (File file) {
FileInputStream fis = new FileInputStream (file);
ObjectInputStream ois = new ObjectInputStream (fis);
Histogram histogram = (Histogram)(ois.readObject ());
ois.close ();
return histogram;
}
An complete example program using this technique is given in the Web Course
Chapter 9: Te ch section.
9.13.2 Histograms as stream destinations
We saw above how a byte array can become the destination of a stream with
the ByteArrayOutputStream class. We can apply a similar technique to
histograms: we make a histogram into the destination of a stream. Perhaps in
a data monitoring program there is extensive handling of dozens or more data
channels, each assigned its own histogram. In such a case, Histogram streams
might offer a neat approach to organizing the filling of the histograms.
In Chapter 7 we discussed the HistPanel component that displays an
instance of our Histogram class. In the code snippet shown below, the
StreamedHistPanel extends HistPanel and includes an inner class called
HistogramOutputStream that extends OutputStream . HistogramOut-
putStream overrides the write (int b) method of OutputStream with a
method that adds data to the histogram in the StreamedHistPanel object (it
can access the histogram since it is an inner class of StreamedHistPanel ).
import java.io.*;
/** This class provides provides a HistPanel destination
* for a output stream. **/
public class StreamedHistPanel extends HistPanel
{
OutputStream fHistStream;
boolean fNewDataFlag = false;
 
Search WWH ::




Custom Search