Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Transmitting a non-serializable object

Thread view: 
Qu0ll - 21 Nov 2007 19:10 GMT
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.

Signature

And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

Daniel Pitts - 21 Nov 2007 19:36 GMT
> 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.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Matt Humphrey - 21 Nov 2007 21:33 GMT
...
> 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/
Mark Thornton - 21 Nov 2007 21:43 GMT
> 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
Arne Vajhøj - 23 Nov 2007 03:46 GMT
> 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
Roedy Green - 23 Nov 2007 08:03 GMT
>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.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.