diff options
| author | Paul Buetow <paul@buetow.org> | 2008-06-01 22:45:59 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-06-01 22:45:59 +0000 |
| commit | 293c73f50f87b3d73d3947a9f79430b23ec4ddba (patch) | |
| tree | 129ece28dcda9d17bb6e0fc417d8b25bb82dac4c /sources/serialize | |
| parent | 8086082daaed71ee9105c156e3a1e8e63caf1990 (diff) | |
better serialization
Diffstat (limited to 'sources/serialize')
| -rw-r--r-- | sources/serialize/VSSerializable.java | 52 | ||||
| -rw-r--r-- | sources/serialize/VSSerialize.java | 83 |
2 files changed, 103 insertions, 32 deletions
diff --git a/sources/serialize/VSSerializable.java b/sources/serialize/VSSerializable.java new file mode 100644 index 0000000..a4affe9 --- /dev/null +++ b/sources/serialize/VSSerializable.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2008 Paul C. Buetow, vs@dev.buetow.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * All icons of the icons/ folder are under a Creative Commons + * Attribution-Noncommercial-Share Alike License a CC-by-nc-sa. + * + * The icon's homepage is http://code.google.com/p/ultimate-gnome/ + */ + +package serialize; + +import java.io.*; + +/** + * The Interface VSSerializable, + * + * @author Paul C. Buetow + */ +public interface VSSerializable { + /** + * Serializes + * + * @param serialize The serialize object + * @param objectOutputStream The object output stream + */ + public void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) + throws IOException; + /** + * Deserializes + * + * @param serialize The serialize object + * @param objectOutputStream The object input stream + */ + public void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException; +} diff --git a/sources/serialize/VSSerialize.java b/sources/serialize/VSSerialize.java index b3bd18b..b659879 100644 --- a/sources/serialize/VSSerialize.java +++ b/sources/serialize/VSSerialize.java @@ -21,12 +21,13 @@ * The icon's homepage is http://code.google.com/p/ultimate-gnome/ */ -package utils; +package serialize; import java.io.*; import java.util.HashMap; -import simulator.VSSimulator; +import prefs.*; +import simulator.*; /** * The class VSSerialize, this static class helps do deserialize @@ -47,21 +48,34 @@ public final class VSSerialize { /** For temp object storage */ private static HashMap<String,Object> objects; + /** Holds the current VSSerialize object */ + private static VSSerialize serialize; + /** * Creates the VSSerialize object. */ public VSSerialize() { - init(); + init(); } /** * Initializes the helper. */ private void init() { + this.serialize = this; objects = new HashMap<String,Object>(); } /** + * Gets the current VSSerialize object. + * + * @return The current serialize object + */ + public static VSSerialize getSerialize() { + return serialize; + } + + /** * Sets an object. * * @param key The object key @@ -120,47 +134,52 @@ public final class VSSerialize { return object; } - /** - * Saves the given simulator to the given filename. - * - * @param filename The filename - * @param simulator The simulator - */ + /** + * Saves the given simulator to the given filename. + * + * @param filename The filename + * @param simulator The simulator + */ public void saveSimulator(String filename, VSSimulator simulator) { try { - FileOutputStream fileOutputStream = - new FileOutputStream(filename); - ObjectOutputStream objectOutputStream = - new ObjectOutputStream(fileOutputStream); + FileOutputStream fileOutputStream = + new FileOutputStream(filename); + ObjectOutputStream objectOutputStream = + new ObjectOutputStream(fileOutputStream); - objectOutputStream.writeObject(simulator.getPrefs()); - simulator.serialize(this); + VSPrefs prefs = simulator.getPrefs(); + prefs.serialize(this, objectOutputStream); + simulator.serialize(this, objectOutputStream); } catch (IOException e) { e.printStackTrace(); } } - /** - * Opens a simulator from the given filename. - * - * @param filename The filename. - * @param simulatorFrame The simulator frame - * - * @return The simulator object, and null if no success - */ + /** + * Opens a simulator from the given filename. + * + * @param filename The filename. + * @param simulatorFrame The simulator frame + * + * @return The simulator object, and null if no success + */ public VSSimulator openSimulator(String filename, - VSSimulatorFrame simulatorFrame) { + VSSimulatorFrame simulatorFrame) { + VSSimulator simulator = null; try { - FileInputStream fileInputStream = - new FileInputStream(filename); - ObjectInputStream objectInputStream = - new ObjectInputStream(fileInputStream); - - VSPrefs prefs = (VSPrefs) objectInputStream.readObject(); - simulator = new VSSimulator(prefs, simulatorFrame); - simulator.deserialize(this); + FileInputStream fileInputStream = + new FileInputStream(filename); + ObjectInputStream objectInputStream = + new ObjectInputStream(fileInputStream); + + VSPrefs prefs = new VSPrefs(); + prefs.deserialize(this, objectInputStream); + this.setObject("prefs", prefs); + + simulator = new VSSimulator(prefs, simulatorFrame); + simulator.deserialize(this, objectInputStream); } catch (Exception e) { e.printStackTrace(); |
