📄 serialutilities.java
字号:
/* ===================================================
* JCommon : a free general purpose Java class library
* ===================================================
*
* Project Info: http://www.jfree.org/jcommon/index.html
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* --------------------
* SerialUtilities.java
* --------------------
* (C) Copyright 2000-2003, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* $Id: SerialUtilities.java,v 1.3 2003/06/13 15:46:55 mungady Exp $
*
* Changes
* -------
* 25-Mar-2003 : Version 1 (DG);
*
*/
package org.jfree.io;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* A class containing useful utility methods relating to serialization.
*
* @author David Gilbert
*/
public class SerialUtilities {
/**
* To prevent unnecessary instantiation.
*/
protected SerialUtilities() {
throw new UnsupportedOperationException();
}
/**
* Reads a <code>Paint</code> object that has been serialised by the
* {@link SerialUtilities#writePaint} method.
*
* @param stream the input stream.
*
* @return The paint object.
*
* @throws IOException if there is an I/O problem.
* @throws ClassNotFoundException if there is a problem loading a class.
*/
public static Paint readPaint(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
Paint result = null;
boolean isNull = stream.readBoolean();
if (!isNull) {
Class c = (Class) stream.readObject();
if (c.equals(Color.class)) {
result = (Paint) stream.readObject();
}
else {
result = (Paint) stream.readObject();
}
}
return result;
}
/**
* Serialises a <code>Paint</code> object.
*
* @param paint the paint object.
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
public static void writePaint(Paint paint, ObjectOutputStream stream) throws IOException {
if (paint != null) {
stream.writeBoolean(false);
if (paint instanceof Color) {
stream.writeObject(Color.class);
stream.writeObject(paint);
}
else {
stream.writeObject(paint.getClass());
stream.writeObject(paint);
}
}
else {
stream.writeBoolean(true);
}
}
/**
* Reads a <code>Stroke</code> object that has been serialised by the
* {@link SerialUtilities#writeStroke} method.
*
* @param stream the input stream.
*
* @return The stroke object.
*
* @throws IOException if there is an I/O problem.
* @throws ClassNotFoundException if there is a problem loading a class.
*/
public static Stroke readStroke(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
Stroke result = null;
boolean isNull = stream.readBoolean();
if (!isNull) {
Class c = (Class) stream.readObject();
if (c.equals(BasicStroke.class)) {
float width = stream.readFloat();
int cap = stream.readInt();
int join = stream.readInt();
float miterLimit = stream.readFloat();
float[] dash = (float[]) stream.readObject();
float dashPhase = stream.readFloat();
result = new BasicStroke(width, cap, join, miterLimit, dash, dashPhase);
}
else {
result = (Stroke) stream.readObject();
}
}
return result;
}
/**
* Serialises a <code>Stroke</code> object.
*
* @param stroke the stroke object.
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
public static void writeStroke(Stroke stroke, ObjectOutputStream stream) throws IOException {
if (stroke != null) {
stream.writeBoolean(false);
if (stroke instanceof BasicStroke) {
BasicStroke s = (BasicStroke) stroke;
stream.writeObject(BasicStroke.class);
stream.writeFloat(s.getLineWidth());
stream.writeInt(s.getEndCap());
stream.writeInt(s.getLineJoin());
stream.writeFloat(s.getMiterLimit());
stream.writeObject(s.getDashArray());
stream.writeFloat(s.getDashPhase());
}
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} method.
*
* @param stream the input stream.
*
* @return The shape object.
*
* @throws IOException if there is an I/O problem.
* @throws ClassNotFoundException if there is a problem loading a class.
*/
public static Shape readShape(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
Shape result = null;
boolean isNull = stream.readBoolean();
if (!isNull) {
Class c = (Class) stream.readObject();
if (c.equals(Line2D.class)) {
double x1 = stream.readDouble();
double y1 = stream.readDouble();
double x2 = stream.readDouble();
double y2 = stream.readDouble();
result = new Line2D.Double(x1, y1, x2, y2);
}
else if (c.equals(Rectangle2D.class)) {
double x = stream.readDouble();
double y = stream.readDouble();
double w = stream.readDouble();
double h = stream.readDouble();
result = new Rectangle2D.Double(x, y, w, h);
}
else if (c.equals(Ellipse2D.class)) {
double x = stream.readDouble();
double y = stream.readDouble();
double w = stream.readDouble();
double h = stream.readDouble();
result = new Ellipse2D.Double(x, y, w, h);
}
else {
result = (Shape) stream.readObject();
}
}
return result;
}
/**
* Serialises a <code>Shape</code> object.
*
* @param shape the shape object.
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
public static void writeShape(Shape shape, ObjectOutputStream stream) throws IOException {
if (shape != null) {
stream.writeBoolean(false);
if (shape instanceof Line2D) {
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) {
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) {
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 {
stream.writeObject(shape.getClass());
stream.writeObject(shape);
}
}
else {
stream.writeBoolean(true);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -