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 / April 2006

Tip: Looking for answers? Try searching our database.

BasicStroke serialization

Thread view: 
Tomba - 05 Apr 2006 00:42 GMT
Hi,

is there anyone here who has already made a serializable BasicStroke class?

I'm making a graphical program and for saving I came across several
problems as BasicStroke is not serializable. Furthermore I also need to
save and load java.awt.geom.GeneralPath

Can this be found somewhere?
And why are these classes not serializable in the java classes themself?

Thanks,
Steven
Thomas Hawtin - 05 Apr 2006 01:49 GMT
> is there anyone here who has already made a serializable BasicStroke class?

No I hadn't, but just for fun I though I would do so.

The general form of extending immutable classes for serialisation is to
store the data from readObject in an extra transient field and use
readResolve to replace with an instance created using the correct
constructor. If the class doesn't have an accessible no-args
constructor, you can insert an intermediate class. In this example I
have also used writeReplace to remove the field from instances of the
class while not involved in serialisation.

Note, if you subclass SerializableBasicStroke, it wont write or read any
data.

> I'm making a graphical program and for saving I came across several
> problems as BasicStroke is not serializable. Furthermore I also need to
> save and load java.awt.geom.GeneralPath

You should be able to do much the same thing with GeneralPath. However,
it will be a bit more involved.

> Can this be found somewhere?
> And why are these classes not serializable in the java classes themself?

See evaluation to bugs 4305099 "BasicStroke is not serializable" (5
votes, open) and 4263142 "RFE: Point2D.Double and Point2D.Float should
be Serializable" (18 votes, fixed in mustang).

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4305099
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4263142

Tom Hawtin

/*
Copyright 2006 Thomas Hawtin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public class SerializableBasicStroke
extends java.awt.BasicStroke
implements java.io.Serializable
{
    private static class Serial implements java.io.Serializable {
        static final long serialVersionUID = 5538700973722429161L+1;
        private transient SerializableBasicStroke replacement;

        Serial(SerializableBasicStroke replacement) {
            this.replacement = replacement;
        }

        private void writeObject(
            java.io.ObjectOutputStream out
        ) throws java.io.IOException {
            out.writeFloat(replacement.getLineWidth());
            out.writeInt(replacement.getEndCap());
            out.writeInt(replacement.getLineJoin());
            out.writeFloat(replacement.getMiterLimit());
            out.writeUnshared(replacement.getDashArray());
            out.writeFloat(replacement.getDashPhase());
        }
        private void readObject(
            java.io.ObjectInputStream in
        ) throws java.io.IOException, java.lang.ClassNotFoundException {
            try {
                this.replacement = new SerializableBasicStroke(
                    in.readFloat(),             // lineWidth
                    in.readInt(),               // endCap
                    in.readInt(),               // lineJoin
                    in.readFloat(),             // miterLimit
                    (float[])in.readUnshared(), // dashArray
                    in.readFloat()              // dashPhase
                );
            } catch (IllegalArgumentException exc) {
                java.io.InvalidObjectException wrapper =
                    new java.io.InvalidObjectException(exc.getMessage());
                wrapper.initCause(exc);
                throw wrapper;
            }
        }

        private Object readResolve() throws java.io.ObjectStreamException {
            return replacement;
        }
    }

    public static java.awt.BasicStroke serializable(
        java.awt.BasicStroke target
    ) {
        return (target instanceof java.io.Serializable) ?
            target :
            new SerializableBasicStroke(
                target.getLineWidth(),
                target.getEndCap(),
                target.getLineJoin(),
                target.getMiterLimit(),
                target.getDashArray(),
                target.getDashPhase()
            );
    }

    public SerializableBasicStroke() {
        super();
    }

    public SerializableBasicStroke(
        float lineWidth
    ) {
        super(
            lineWidth
        );
    }

    public SerializableBasicStroke(
        float lineWidth,
        int endCap,
        int lineJoin
    ) {
        super(
            lineWidth,
            endCap,
            lineJoin
        );
    }

    public SerializableBasicStroke(
        float lineWidth,
        int endCap,
        int lineJoin,
        float miterLimit
    ) {
        super(
            lineWidth,
            endCap,
            lineJoin,
            miterLimit
        );
    }

    public SerializableBasicStroke(
        float lineWidth,
        int endCap,
        int lineJoin,
        float miterLimit,
        float[] dashArray,
        float dashPhase
    ) {
        super(
            lineWidth,
            endCap,
            lineJoin,
            miterLimit,
            dashArray,
            dashPhase
        );
    }

    private Object writeReplace() throws java.io.ObjectStreamException {
        return new Serial(this);
    }
}
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Remon van Vliet - 05 Apr 2006 02:00 GMT
> Hi,
>
[quoted text clipped - 10 lines]
> Thanks,
> Steven

Ever considered simply saving and loading the class field values of the
classes you mentioned?


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.