Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

BasicStroke serialization

341 views
Skip to first unread message

Tomba

unread,
Apr 4, 2006, 7:42:45 PM4/4/06
to
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

Remon van Vliet

unread,
Apr 4, 2006, 9:00:17 PM4/4/06
to

"Tomba" <to...@f1technical.net> wrote in message
news:4433046d$0$14676$ba62...@news.skynet.be...

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


Thomas Hawtin

unread,
Apr 4, 2006, 8:49:01 PM4/4/06
to
Tomba wrote:
>
> 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);
}
}
--
Unemployed English Java programmer
http://jroller.com/page/tackline/

0 new messages