1:
46:
47: package ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71:
72:
77: public class SerialUtilities {
78:
79:
82: private SerialUtilities() {
83: }
84:
85:
93: public static boolean isSerializable(final Class c) {
94:
106: return (Serializable.class.isAssignableFrom(c));
107: }
108:
109:
120: public static Paint readPaint(final ObjectInputStream stream)
121: throws IOException, ClassNotFoundException {
122:
123: if (stream == null) {
124: throw new IllegalArgumentException("Null 'stream' argument.");
125: }
126: Paint result = null;
127: final boolean isNull = stream.readBoolean();
128: if (!isNull) {
129: final Class c = (Class) stream.readObject();
130: if (isSerializable(c)) {
131: result = (Paint) stream.readObject();
132: }
133: else if (c.equals(GradientPaint.class)) {
134: final float x1 = stream.readFloat();
135: final float y1 = stream.readFloat();
136: final Color c1 = (Color) stream.readObject();
137: final float x2 = stream.readFloat();
138: final float y2 = stream.readFloat();
139: final Color c2 = (Color) stream.readObject();
140: final boolean isCyclic = stream.readBoolean();
141: result = new GradientPaint(x1, y1, c1, x2, y2, c2, isCyclic);
142: }
143: }
144: return result;
145:
146: }
147:
148:
156: public static void writePaint(final Paint paint,
157: final ObjectOutputStream stream)
158: throws IOException {
159:
160: if (stream == null) {
161: throw new IllegalArgumentException("Null 'stream' argument.");
162: }
163: if (paint != null) {
164: stream.writeBoolean(false);
165: stream.writeObject(paint.getClass());
166: if (paint instanceof Serializable) {
167: stream.writeObject(paint);
168: }
169: else if (paint instanceof GradientPaint) {
170: final GradientPaint gp = (GradientPaint) paint;
171: stream.writeFloat((float) gp.getPoint1().getX());
172: stream.writeFloat((float) gp.getPoint1().getY());
173: stream.writeObject(gp.getColor1());
174: stream.writeFloat((float) gp.getPoint2().getX());
175: stream.writeFloat((float) gp.getPoint2().getY());
176: stream.writeObject(gp.getColor2());
177: stream.writeBoolean(gp.isCyclic());
178: }
179: }
180: else {
181: stream.writeBoolean(true);
182: }
183:
184: }
185:
186:
197: public static Stroke readStroke(final ObjectInputStream stream)
198: throws IOException, ClassNotFoundException {
199:
200: if (stream == null) {
201: throw new IllegalArgumentException("Null 'stream' argument.");
202: }
203: Stroke result = null;
204: final boolean isNull = stream.readBoolean();
205: if (!isNull) {
206: final Class c = (Class) stream.readObject();
207: if (c.equals(BasicStroke.class)) {
208: final float width = stream.readFloat();
209: final int cap = stream.readInt();
210: final int join = stream.readInt();
211: final float miterLimit = stream.readFloat();
212: final float[] dash = (float[]) stream.readObject();
213: final float dashPhase = stream.readFloat();
214: result = new BasicStroke(
215: width, cap, join, miterLimit, dash, dashPhase
216: );
217: }
218: else {
219: result = (Stroke) stream.readObject();
220: }
221: }
222: return result;
223:
224: }
225:
226:
237: public static void writeStroke(final Stroke stroke,
238: final ObjectOutputStream stream)
239: throws IOException {
240:
241: if (stream == null) {
242: throw new IllegalArgumentException("Null 'stream' argument.");
243: }
244: if (stroke != null) {
245: stream.writeBoolean(false);
246: if (stroke instanceof BasicStroke) {
247: final BasicStroke s = (BasicStroke) stroke;
248: stream.writeObject(BasicStroke.class);
249: stream.writeFloat(s.getLineWidth());
250: stream.writeInt(s.getEndCap());
251: stream.writeInt(s.getLineJoin());
252: stream.writeFloat(s.getMiterLimit());
253: stream.writeObject(s.getDashArray());
254: stream.writeFloat(s.getDashPhase());
255: }
256: else {
257: stream.writeObject(stroke.getClass());
258: stream.writeObject(stroke);
259: }
260: }
261: else {
262: stream.writeBoolean(true);
263: }
264: }
265:
266:
277: public static Shape readShape(final ObjectInputStream stream)
278: throws IOException, ClassNotFoundException {
279:
280: if (stream == null) {
281: throw new IllegalArgumentException("Null 'stream' argument.");
282: }
283: Shape result = null;
284: final boolean isNull = stream.readBoolean();
285: if (!isNull) {
286: final Class c = (Class) stream.readObject();
287: if (c.equals(Line2D.class)) {
288: final double x1 = stream.readDouble();
289: final double y1 = stream.readDouble();
290: final double x2 = stream.readDouble();
291: final double y2 = stream.readDouble();
292: result = new Line2D.Double(x1, y1, x2, y2);
293: }
294: else if (c.equals(Rectangle2D.class)) {
295: final double x = stream.readDouble();
296: final double y = stream.readDouble();
297: final double w = stream.readDouble();
298: final double h = stream.readDouble();
299: result = new Rectangle2D.Double(x, y, w, h);
300: }
301: else if (c.equals(Ellipse2D.class)) {
302: final double x = stream.readDouble();
303: final double y = stream.readDouble();
304: final double w = stream.readDouble();
305: final double h = stream.readDouble();
306: result = new Ellipse2D.Double(x, y, w, h);
307: }
308: else if (c.equals(Arc2D.class)) {
309: final double x = stream.readDouble();
310: final double y = stream.readDouble();
311: final double w = stream.readDouble();
312: final double h = stream.readDouble();
313: final double as = stream.readDouble();
314: final double ae = stream.readDouble();
315: final int at = stream.readInt();
316: result = new Arc2D.Double(x, y, w, h, as, ae, at);
317: }
318: else if (c.equals(GeneralPath.class)) {
319: final GeneralPath gp = new GeneralPath();
320: final float[] args = new float[6];
321: boolean hasNext = stream.readBoolean();
322: while (!hasNext) {
323: final int type = stream.readInt();
324: for (int i = 0; i < 6; i++) {
325: args[i] = stream.readFloat();
326: }
327: switch (type) {
328: case PathIterator.SEG_MOVETO :
329: gp.moveTo(args[0], args[1]);
330: break;
331: case PathIterator.SEG_LINETO :
332: gp.lineTo(args[0], args[1]);
333: break;
334: case PathIterator.SEG_CUBICTO :
335: gp.curveTo(args[0], args[1], args[2],
336: args[3], args[4], args[5]);
337: break;
338: case PathIterator.SEG_QUADTO :
339: gp.quadTo(args[0], args[1], args[2], args[3]);
340: break;
341: case PathIterator.SEG_CLOSE :
342: gp.closePath();
343: break;
344: default :
345: throw new RuntimeException(
346: "JFreeChart - No path exists");
347: }
348: gp.setWindingRule(stream.readInt());
349: hasNext = stream.readBoolean();
350: }
351: result = gp;
352: }
353: else {
354: result = (Shape) stream.readObject();
355: }
356: }
357: return result;
358:
359: }
360:
361:
369: public static void writeShape(final Shape shape,
370: final ObjectOutputStream stream)
371: throws IOException {
372:
373: if (stream == null) {
374: throw new IllegalArgumentException("Null 'stream' argument.");
375: }
376: if (shape != null) {
377: stream.writeBoolean(false);
378: if (shape instanceof Line2D) {
379: final Line2D line = (Line2D) shape;
380: stream.writeObject(Line2D.class);
381: stream.writeDouble(line.getX1());
382: stream.writeDouble(line.getY1());
383: stream.writeDouble(line.getX2());
384: stream.writeDouble(line.getY2());
385: }
386: else if (shape instanceof Rectangle2D) {
387: final Rectangle2D rectangle = (Rectangle2D) shape;
388: stream.writeObject(Rectangle2D.class);
389: stream.writeDouble(rectangle.getX());
390: stream.writeDouble(rectangle.getY());
391: stream.writeDouble(rectangle.getWidth());
392: stream.writeDouble(rectangle.getHeight());
393: }
394: else if (shape instanceof Ellipse2D) {
395: final Ellipse2D ellipse = (Ellipse2D) shape;
396: stream.writeObject(Ellipse2D.class);
397: stream.writeDouble(ellipse.getX());
398: stream.writeDouble(ellipse.getY());
399: stream.writeDouble(ellipse.getWidth());
400: stream.writeDouble(ellipse.getHeight());
401: }
402: else if (shape instanceof Arc2D) {
403: final Arc2D arc = (Arc2D) shape;
404: stream.writeObject(Arc2D.class);
405: stream.writeDouble(arc.getX());
406: stream.writeDouble(arc.getY());
407: stream.writeDouble(arc.getWidth());
408: stream.writeDouble(arc.getHeight());
409: stream.writeDouble(arc.getAngleStart());
410: stream.writeDouble(arc.getAngleExtent());
411: stream.writeInt(arc.getArcType());
412: }
413: else if (shape instanceof GeneralPath) {
414: stream.writeObject(GeneralPath.class);
415: final PathIterator pi = shape.getPathIterator(null);
416: final float[] args = new float[6];
417: stream.writeBoolean(pi.isDone());
418: while (!pi.isDone()) {
419: final int type = pi.currentSegment(args);
420: stream.writeInt(type);
421:
422:
423: for (int i = 0; i < 6; i++) {
424: stream.writeFloat(args[i]);
425: }
426: stream.writeInt(pi.getWindingRule());
427: pi.next();
428: stream.writeBoolean(pi.isDone());
429: }
430: }
431: else {
432: stream.writeObject(shape.getClass());
433: stream.writeObject(shape);
434: }
435: }
436: else {
437: stream.writeBoolean(true);
438: }
439: }
440:
441:
451: public static Point2D readPoint2D(final ObjectInputStream stream)
452: throws IOException {
453:
454: if (stream == null) {
455: throw new IllegalArgumentException("Null 'stream' argument.");
456: }
457: Point2D result = null;
458: final boolean isNull = stream.readBoolean();
459: if (!isNull) {
460: final double x = stream.readDouble();
461: final double y = stream.readDouble();
462: result = new Point2D.Double(x, y);
463: }
464: return result;
465:
466: }
467:
468:
476: public static void writePoint2D(final Point2D p,
477: final ObjectOutputStream stream)
478: throws IOException {
479:
480: if (stream == null) {
481: throw new IllegalArgumentException("Null 'stream' argument.");
482: }
483: if (p != null) {
484: stream.writeBoolean(false);
485: stream.writeDouble(p.getX());
486: stream.writeDouble(p.getY());
487: }
488: else {
489: stream.writeBoolean(true);
490: }
491: }
492:
493:
505: public static AttributedString readAttributedString(
506: ObjectInputStream stream)
507: throws IOException, ClassNotFoundException {
508:
509: if (stream == null) {
510: throw new IllegalArgumentException("Null 'stream' argument.");
511: }
512: AttributedString result = null;
513: final boolean isNull = stream.readBoolean();
514: if (!isNull) {
515:
516: String plainStr = (String) stream.readObject();
517: result = new AttributedString(plainStr);
518: char c = stream.readChar();
519: int start = 0;
520: while (c != CharacterIterator.DONE) {
521: int limit = stream.readInt();
522: Map atts = (Map) stream.readObject();
523: result.addAttributes(atts, start, limit);
524: start = limit;
525: c = stream.readChar();
526: }
527: }
528: return result;
529: }
530:
531:
539: public static void writeAttributedString(AttributedString as,
540: ObjectOutputStream stream) throws IOException {
541:
542: if (stream == null) {
543: throw new IllegalArgumentException("Null 'stream' argument.");
544: }
545: if (as != null) {
546: stream.writeBoolean(false);
547: AttributedCharacterIterator aci = as.getIterator();
548:
549:
550: StringBuffer plainStr = new StringBuffer();
551: char current = aci.first();
552: while (current != CharacterIterator.DONE) {
553: plainStr = plainStr.append(current);
554: current = aci.next();
555: }
556: stream.writeObject(plainStr.toString());
557:
558:
559: current = aci.first();
560: int begin = aci.getBeginIndex();
561: while (current != CharacterIterator.DONE) {
562:
563:
564:
565: stream.writeChar(current);
566:
567:
568: int limit = aci.getRunLimit();
569: stream.writeInt(limit - begin);
570:
571:
572: Map atts = new HashMap(aci.getAttributes());
573: stream.writeObject(atts);
574: current = aci.setIndex(limit);
575: }
576:
577:
578: stream.writeChar(CharacterIterator.DONE);
579: }
580: else {
581:
582: stream.writeBoolean(true);
583: }
584:
585: }
586:
587: }