Transmitting a non-serializable object

Q

Qu0ll

I have a java.awt.geom.Area object that I need to transmit over the network
but when I try to do so using a ObjectOutputStream and NIO it complains that
the object is not serializable. Which makes sense (seeing it isn't) but how
do I transmit this object? Is there a trick to it? I tried wrapping it in
a serializable object but it didn't like that either.

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
D

Daniel Pitts

Qu0ll said:
I have a java.awt.geom.Area object that I need to transmit over the
network but when I try to do so using a ObjectOutputStream and NIO it
complains that the object is not serializable. Which makes sense
(seeing it isn't) but how do I transmit this object? Is there a trick
to it? I tried wrapping it in a serializable object but it didn't like
that either.
Wrapping a non-serializable object in a serializable one is like trying
to attach wheels to a bolted down picnic table and expecting it to roll
easier :) Anyway, you figured that out on your own.

There is no way to make Area serializable unless you make your own type
of Area that is serializable. Also, any non-transient reference within
your serializable area class has to be serializable. It becomes a
large mess. My suggestion, if its at all possible, is to instead
serialize the data you need to recreate the Area.

I'm actually surprised that most of the Shape classes aren't
serializable, but there you have it. Either lack of foresight, or
specific design decision. You might open a feature request to sun, but
I doubt it'll get much traction.
 
M

Matt Humphrey

....
I'm actually surprised that most of the Shape classes aren't serializable,
but there you have it. Either lack of foresight, or specific design
decision. You might open a feature request to sun, but I doubt it'll get
much traction.

It may be possible to store the segments produced by Area's path iterator
and later reconstitute the area with them. Get the values from the path
iterator and store in your own kind of object. To reconstruct, pass the
segments to GeneralPath move/curve/line operations. This little program
shows how to read the segments. I don't pick off the winding rule here.

public static void main (String args []) {
Area area = new Area ();
area.add(new Area (new Rectangle2D.Double (0, 0, 20.0, 30.0)));
area.add(new Area (new Ellipse2D.Double (-10, -10, 20.0, 20.0)));

PathIterator pi = area.getPathIterator(new AffineTransform ());
double [] pt= new double [6];
while (! pi.isDone()) {
int code = pi.currentSegment(pt);
switch (code) {
case PathIterator.SEG_CLOSE:
System.out.println ("Close "); break;
case PathIterator.SEG_CUBICTO:
System.out.println ("Cubic to (" + pt[0] + "," + pt[1]
+ ") (" + pt[2] + "," + pt[3]
+ ") (" + pt[4] + "," + pt[5] + ")"); break;
case PathIterator.SEG_LINETO:
System.out.println ("Line to (" + pt[0] + "," + pt[1] +
")"); break;
case PathIterator.SEG_MOVETO:
System.out.println ("Move to (" + pt[0] + "," + pt[1] +
")"); break;
case PathIterator.SEG_QUADTO:
System.out.println ("Quad to (" + pt[0] + "," + pt[1]
+ ") (" + pt[2] + "," + pt[3] + ")"); break;
default: System.out.println ("??");
}

pi.next ();
}

Matt Humphrey http://www.iviz.com/
 
M

Mark Thornton

Daniel said:
Qu0ll wrote:
I'm actually surprised that most of the Shape classes aren't
serializable, but there you have it. Either lack of foresight, or
specific design decision. You might open a feature request to sun, but
I doubt it'll get much traction.

As of Java 6 most of the non abstract shapes are Serializable, Area
being one of the few exceptions. The previous lack of Serilizability was
an oversight, which regrettably took rather too long to fix.

Mark Thornton
 
A

Arne Vajhøj

Qu0ll said:
I have a java.awt.geom.Area object that I need to transmit over the
network but when I try to do so using a ObjectOutputStream and NIO it
complains that the object is not serializable. Which makes sense
(seeing it isn't) but how do I transmit this object? Is there a trick
to it? I tried wrapping it in a serializable object but it didn't like
that either.

Maybe you can use Ted Neward's Serializable Adapter approach:

http://www.javageeks.com/Papers/SerializableAdapter/SerializableAdapter.html

Arne
 
R

Roedy Green

I have a java.awt.geom.Area object that I need to transmit over the network
but when I try to do so using a ObjectOutputStream and NIO it complains that
the object is not serializable.

Ideas:

try subclassing it and marking that subclass serializable.

Extract all the juice out of an Area and plop it into a
SerializableArea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top