serialutilities.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 488 行 · 第 1/2 页

JAVA
488
字号
            }            else {                stream.writeObject(stroke.getClass());                stream.writeObject(stroke);            }        }        else {            stream.writeBoolean(true);        }    }    /**     * Reads a <code>Shape</code> object that has been serialised by the      * {@link #writeShape(Shape, ObjectOutputStream)} method.     *     * @param stream  the input stream (<code>null</code> not permitted).     *     * @return The shape object (possibly <code>null</code>).     *     * @throws IOException  if there is an I/O problem.     * @throws ClassNotFoundException  if there is a problem loading a class.     */    public static Shape readShape(final ObjectInputStream stream)        throws IOException, ClassNotFoundException {        if (stream == null) {            throw new IllegalArgumentException("Null 'stream' argument.");           }        Shape result = null;        final boolean isNull = stream.readBoolean();        if (!isNull) {            final Class c = (Class) stream.readObject();            if (c.equals(Line2D.class)) {                final double x1 = stream.readDouble();                final double y1 = stream.readDouble();                final double x2 = stream.readDouble();                final double y2 = stream.readDouble();                result = new Line2D.Double(x1, y1, x2, y2);            }            else if (c.equals(Rectangle2D.class)) {                final double x = stream.readDouble();                final double y = stream.readDouble();                final double w = stream.readDouble();                final double h = stream.readDouble();                result = new Rectangle2D.Double(x, y, w, h);            }            else if (c.equals(Ellipse2D.class)) {                final double x = stream.readDouble();                final double y = stream.readDouble();                final double w = stream.readDouble();                final double h = stream.readDouble();                result = new Ellipse2D.Double(x, y, w, h);            }            else if (c.equals(Arc2D.class)) {                final double x = stream.readDouble();                final double y = stream.readDouble();                final double w = stream.readDouble();                final double h = stream.readDouble();                final double as = stream.readDouble(); // Angle Start                final double ae = stream.readDouble(); // Angle Extent                final int at = stream.readInt();       // Arc type                result = new Arc2D.Double(x, y, w, h, as, ae, at);            }                        else if (c.equals(GeneralPath.class)) {                final GeneralPath gp = new GeneralPath();                final float[] args = new float[6];                boolean hasNext = stream.readBoolean();                while (!hasNext) {                    final int type = stream.readInt();                    for(int i=0; i<6; i++) {                        args[i] = stream.readFloat();                    }                    switch (type) {                         case PathIterator.SEG_MOVETO :                              gp.moveTo(args[0],args[1]);                            break;                        case PathIterator.SEG_LINETO :                                                       gp.lineTo(args[0],args[1]);                            break;                         case PathIterator.SEG_CUBICTO :                            gp.curveTo(                                args[0], args[1], args[2],                                 args[3], args[4], args[5]                            );                            break;                        case PathIterator.SEG_QUADTO :                            gp.quadTo(args[0], args[1], args[2], args[3]);                            break;                                          case PathIterator.SEG_CLOSE :                            //result = gp;                            break;                        default :                             throw new RuntimeException(                                "JFreeChart - No path exists"                            );                     }                     gp.setWindingRule(stream.readInt());                        hasNext = stream.readBoolean();                }                result = gp;            }            else {                result = (Shape) stream.readObject();            }        }        return result;    }    /**     * Serialises a <code>Shape</code> object.     *     * @param shape  the shape object (<code>null</code> permitted).     * @param stream  the output stream (<code>null</code> not permitted).     *     * @throws IOException if there is an I/O error.     */    public static void writeShape(final Shape shape,                                  final ObjectOutputStream stream)         throws IOException {        if (stream == null) {            throw new IllegalArgumentException("Null 'stream' argument.");           }        if (shape != null) {            stream.writeBoolean(false);            if (shape instanceof Line2D) {                final Line2D line = (Line2D) shape;                stream.writeObject(Line2D.class);                stream.writeDouble(line.getX1());                stream.writeDouble(line.getY1());                stream.writeDouble(line.getX2());                stream.writeDouble(line.getY2());            }            else if (shape instanceof Rectangle2D) {                final Rectangle2D rectangle = (Rectangle2D) shape;                stream.writeObject(Rectangle2D.class);                stream.writeDouble(rectangle.getX());                stream.writeDouble(rectangle.getY());                stream.writeDouble(rectangle.getWidth());                stream.writeDouble(rectangle.getHeight());            }            else if (shape instanceof Ellipse2D) {                final Ellipse2D ellipse = (Ellipse2D) shape;                stream.writeObject(Ellipse2D.class);                stream.writeDouble(ellipse.getX());                stream.writeDouble(ellipse.getY());                stream.writeDouble(ellipse.getWidth());                stream.writeDouble(ellipse.getHeight());            }            else if (shape instanceof Arc2D) {                final Arc2D arc = (Arc2D) shape;                stream.writeObject(Arc2D.class);                stream.writeDouble(arc.getX());                stream.writeDouble(arc.getY());                stream.writeDouble(arc.getWidth());                stream.writeDouble(arc.getHeight());                stream.writeDouble(arc.getAngleStart());                stream.writeDouble(arc.getAngleExtent());                stream.writeInt(arc.getArcType());            }            else if (shape instanceof GeneralPath) {                stream.writeObject(GeneralPath.class);                final PathIterator pi = shape.getPathIterator(null);                final float[] args = new float[6];                stream.writeBoolean(pi.isDone());                while(!pi.isDone()) {                    final int type = pi.currentSegment(args);                    stream.writeInt(type);                    // TODO: could write this to only stream the values                    // required for the segment type                    for (int i = 0; i < 6; i++) {                        stream.writeFloat(args[i]);                    }                    stream.writeInt(pi.getWindingRule());                    pi.next();                    stream.writeBoolean(pi.isDone());                }            }            else {                stream.writeObject(shape.getClass());                stream.writeObject(shape);            }        }        else {            stream.writeBoolean(true);        }    }    /**     * Reads a <code>Point2D</code> object that has been serialised by the      * {@link #writePoint2D(Point2D, ObjectOutputStream)} method.     *     * @param stream  the input stream (<code>null</code> not permitted).     *     * @return The point object (possibly <code>null</code>).     *     * @throws IOException  if there is an I/O problem.     */    public static Point2D readPoint2D(final ObjectInputStream stream)        throws IOException {        if (stream == null) {            throw new IllegalArgumentException("Null 'stream' argument.");           }        Point2D result = null;        final boolean isNull = stream.readBoolean();        if (!isNull) {            final double x = stream.readDouble();            final double y = stream.readDouble();            result = new Point2D.Double(x, y);        }        return result;    }    /**     * Serialises a <code>Point2D</code> object.     *     * @param p  the point object (<code>null</code> permitted).     * @param stream  the output stream (<code>null</code> not permitted).     *     * @throws IOException if there is an I/O error.     */    public static void writePoint2D(final Point2D p,                                    final ObjectOutputStream stream)         throws IOException {        if (stream == null) {            throw new IllegalArgumentException("Null 'stream' argument.");           }        if (p != null) {            stream.writeBoolean(false);            stream.writeDouble(p.getX());            stream.writeDouble(p.getY());        }        else {            stream.writeBoolean(true);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?