001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
006 * 
007 * http://www.object-refinery.com/orsoncharts/index.html
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * Orson Charts home page:
028 * 
029 * http://www.object-refinery.com/orsoncharts/index.html
030 * 
031 */
032
033package com.orsoncharts.util;
034
035import java.awt.BasicStroke;
036import java.awt.Color;
037import java.awt.GradientPaint;
038import java.awt.Paint;
039import java.awt.Stroke;
040import java.io.IOException;
041import java.io.ObjectInputStream;
042import java.io.ObjectOutputStream;
043import java.io.Serializable;
044
045/**
046 * Serialization support methods.
047 */
048public class SerialUtils {
049    
050    private SerialUtils() {
051        // no need to instantiate this class
052    }
053    
054    /**
055     * Reads a {@code Paint} object that has been serialized by the
056     * {@link SerialUtils#writePaint(Paint, ObjectOutputStream)} method.
057     *
058     * @param stream  the input stream ({@code null} not permitted).
059     *
060     * @return The paint object (possibly {@code null}).
061     *
062     * @throws IOException  if there is an I/O problem.
063     * @throws ClassNotFoundException  if there is a problem loading a class.
064     */
065    public static Paint readPaint(ObjectInputStream stream) throws IOException,
066            ClassNotFoundException {
067
068        ArgChecks.nullNotPermitted(stream, "stream");
069        Paint result = null;
070        boolean isNull = stream.readBoolean();
071        if (!isNull) {
072            Class<?> c = (Class<?>) stream.readObject();
073            if (Serializable.class.isAssignableFrom(c)) {
074                result = (Paint) stream.readObject();
075            } else if (c.equals(GradientPaint.class)) {
076                float x1 = stream.readFloat();
077                float y1 = stream.readFloat();
078                Color c1 = (Color) stream.readObject();
079                float x2 = stream.readFloat();
080                float y2 = stream.readFloat();
081                Color c2 = (Color) stream.readObject();
082                boolean isCyclic = stream.readBoolean();
083                result = new GradientPaint(x1, y1, c1, x2, y2, c2, isCyclic);
084            }
085        }
086        return result;
087    }
088
089    /**
090     * Serializes a {@code Paint} object.
091     *
092     * @param paint  the paint object ({@code null} permitted).
093     * @param stream  the output stream ({@code null} not permitted).
094     *
095     * @throws IOException if there is an I/O error.
096     */
097    public static void writePaint(Paint paint, ObjectOutputStream stream)
098        throws IOException {
099
100        ArgChecks.nullNotPermitted(stream, "stream");
101        if (paint != null) {
102            stream.writeBoolean(false);
103            stream.writeObject(paint.getClass());
104            if (paint instanceof Serializable) {
105                stream.writeObject(paint);
106            } else if (paint instanceof GradientPaint) {
107                GradientPaint gp = (GradientPaint) paint;
108                stream.writeFloat((float) gp.getPoint1().getX());
109                stream.writeFloat((float) gp.getPoint1().getY());
110                stream.writeObject(gp.getColor1());
111                stream.writeFloat((float) gp.getPoint2().getX());
112                stream.writeFloat((float) gp.getPoint2().getY());
113                stream.writeObject(gp.getColor2());
114                stream.writeBoolean(gp.isCyclic());
115            }
116        } else {
117            stream.writeBoolean(true);
118        }
119    }
120
121    /**
122     * Reads a {@code Stroke} object that has been serialized by the
123     * {@link SerialUtils#writeStroke(Stroke, ObjectOutputStream)} method.
124     *
125     * @param stream  the input stream ({@code null} not permitted).
126     *
127     * @return The stroke object (possibly {@code null}).
128     *
129     * @throws IOException  if there is an I/O problem.
130     * @throws ClassNotFoundException  if there is a problem loading a class.
131     */
132    public static Stroke readStroke(ObjectInputStream stream)
133        throws IOException, ClassNotFoundException {
134
135        ArgChecks.nullNotPermitted(stream, "stream");
136        Stroke result = null;
137        boolean isNull = stream.readBoolean();
138        if (!isNull) {
139            Class<?> c = (Class<?>) stream.readObject();
140            if (c.equals(BasicStroke.class)) {
141                float width = stream.readFloat();
142                int cap = stream.readInt();
143                int join = stream.readInt();
144                float miterLimit = stream.readFloat();
145                float[] dash = (float[]) stream.readObject();
146                float dashPhase = stream.readFloat();
147                result = new BasicStroke(width, cap, join, miterLimit, dash, 
148                        dashPhase);
149            } else {
150                result = (Stroke) stream.readObject();
151            }
152        }
153        return result;
154    }
155
156    /**
157     * Serializes a {@code Stroke} object.  This code handles the
158     * {@code BasicStroke} class which is the only {@code Stroke}
159     * implementation provided by the JDK (and isn't directly
160     * {@code Serializable}).
161     *
162     * @param stroke  the stroke object ({@code null} permitted).
163     * @param stream  the output stream ({@code null} not permitted).
164     *
165     * @throws IOException if there is an I/O error.
166     */
167    public static void writeStroke(Stroke stroke, ObjectOutputStream stream)
168            throws IOException {
169
170        ArgChecks.nullNotPermitted(stream, "stream");
171        if (stroke != null) {
172            stream.writeBoolean(false);
173            if (stroke instanceof BasicStroke) {
174                BasicStroke s = (BasicStroke) stroke;
175                stream.writeObject(BasicStroke.class);
176                stream.writeFloat(s.getLineWidth());
177                stream.writeInt(s.getEndCap());
178                stream.writeInt(s.getLineJoin());
179                stream.writeFloat(s.getMiterLimit());
180                stream.writeObject(s.getDashArray());
181                stream.writeFloat(s.getDashPhase());
182            } else {
183                stream.writeObject(stroke.getClass());
184                stream.writeObject(stroke);
185            }
186        } else {
187            stream.writeBoolean(true);
188        }
189    }
190
191}