Hello everyone,
I have an application which uses many Point2D.Double objects, and now
that I am trying to write code which saves objects from the
application, I discover the whole mess with Point2D.Double being not
serializable since 1999(!).
With a little research, I found what I thought was a workaround,
creating a custom, serializable class which extends Point2D.Double. It
is not working, though; when I save an object of the extended class, it
always reads back as (0.0, 0.0).
I have written a small program which creates one DoublePoint object,
saves it, changes it, reads it, and writes it. Note that if you run
this code, it will save a .dat file to your computer without checking
whether that file exists. I assume you do not have a file called
doublePointSaveTestFile.dat on your computer. :)
----- BEGIN CODE -----
import java.awt.geom.*;
import java.io.*;
import javax.swing.*;
public class DoublePointSaveTester {
public DoublePointSaveTester() {
DoublePoint pointToSave = new DoublePoint(1.2d, 0.3d);
System.out.println("Initialized pointToSave: " + pointToSave);
writeDoublePoint(pointToSave);
pointToSave = new DoublePoint(6.66d, 6.666d);
System.out.println("Changed pointToSave: " + pointToSave);
pointToSave = readDoublePoint();
System.out.println("Read pointToSave: " + pointToSave);
}
private void writeDoublePoint(DoublePoint pointIn) {
try {
FileOutputStream fileStream = new
FileOutputStream("doublePointSaveTestFile.dat");
ObjectOutputStream objectStream = new
ObjectOutputStream(fileStream);
objectStream.writeObject(pointIn);
objectStream.close();
} catch (IOException e) {
JOptionPane.showMessageDialog( null, e.toString() );
}
}
private DoublePoint readDoublePoint() {
DoublePoint pointRead = null;
try {
FileInputStream fileStream = new
FileInputStream("doublePointSaveTestFile.dat");
ObjectInputStream objectStream = new ObjectInputStream(fileStream);
pointRead = (DoublePoint)objectStream.readObject();
objectStream.close();
} catch (IOException e) {
JOptionPane.showMessageDialog( null, e.toString() );
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "Error - Inappropriate file
type");
}
return pointRead;
}
public static void main(String arg[]) {
DoublePointSaveTester saveTester = new DoublePointSaveTester();
}
}
class DoublePoint extends Point2D.Double implements Serializable {
public DoublePoint() {
super();
}
public DoublePoint(double x, double y) {
super(x, y);
}
}
----- END CODE -----
Output:
Initialized pointToSave: Point2D.Double[1.2, 0.3]
Changed pointToSave: Point2D.Double[6.66, 6.666]
Read pointToSave: Point2D.Double[0.0, 0.0]
Patricia Shanahan - 16 Sep 2006 13:54 GMT
> Hello everyone,
>
[quoted text clipped - 7 lines]
> is not working, though; when I save an object of the extended class, it
> always reads back as (0.0, 0.0).
...
> class DoublePoint extends Point2D.Double implements Serializable {
>
[quoted text clipped - 7 lines]
>
> }
Doesn't a serializable subsclass of a non-serializable class have to
handle serializing the superclass data explicitly?
Your program works if you add to the DoublePoint code:
private void writeObject(java.io.ObjectOutputStream out)
throws IOException{
out.writeDouble(getX());
out.writeDouble(getY());
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException{
double x = in.readDouble();
double y = in.readDouble();
setLocation(x,y);
}
output:
Initialized pointToSave: Point2D.Double[1.2, 0.3]
Changed pointToSave: Point2D.Double[6.66, 6.666]
Read pointToSave: Point2D.Double[1.2, 0.3]
Patricia
e_matthes@hotmail.com - 16 Sep 2006 15:38 GMT
> Doesn't a serializable subsclass of a non-serializable class have to
> handle serializing the superclass data explicitly?
Thank you very much! It works in the larger application as well now.
Eric
Patricia Shanahan - 16 Sep 2006 16:09 GMT
>> Doesn't a serializable subsclass of a non-serializable class have to
>> handle serializing the superclass data explicitly?
>
> Thank you very much! It works in the larger application as well now.
>
> Eric
Good.
In general, the readObject and writeObject methods should also call
in.defaultReadObject and out.defaultWriteObject, to deal with any
subclass-specific state. In your case, there is no subclass state that
needs to be serialized.
Patricia