Minor refactoring of SerializedObject.

This commit is contained in:
Marcel Kronfeld
2010-06-02 14:18:04 +00:00
parent 0b1a7e30e8
commit 0d2fe54df1
5 changed files with 337 additions and 398 deletions

View File

@@ -31,6 +31,7 @@ import javax.swing.plaf.basic.BasicComboBoxRenderer;
import eva2.server.go.tools.FileTools; import eva2.server.go.tools.FileTools;
import eva2.tools.EVAHELP; import eva2.tools.EVAHELP;
import eva2.tools.SerializedObject;
import eva2.tools.jproxy.RMIProxyLocal; import eva2.tools.jproxy.RMIProxyLocal;
/** /**
* *

View File

@@ -48,6 +48,7 @@ import javax.swing.event.ListSelectionListener;
import eva2.tools.EVAHELP; import eva2.tools.EVAHELP;
import eva2.tools.SelectedTag; import eva2.tools.SelectedTag;
import eva2.tools.SerializedObject;
/*==========================================================================* /*==========================================================================*
* CLASS DECLARATION * CLASS DECLARATION
*==========================================================================*/ *==========================================================================*/

View File

@@ -1,152 +0,0 @@
package eva2.gui;
/*
* Title: EvA2
* Description:
* Copyright: Copyright (c) 2003
* Company: University of Tuebingen, Computer Architecture
* @author Holger Ulmer, Felix Streichert, Hannes Planatscher
* @version: $Revision: 10 $
* $Date: 2006-01-18 11:02:22 +0100 (Wed, 18 Jan 2006) $
* $Author: streiche $
*/
/*==========================================================================*
* IMPORTS
*==========================================================================*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import eva2.server.modules.GOParameters;
import eva2.tools.Serializer;
/**
* This class stores an object serialized in memory. It allows compression,
* to be used to conserve memory (for example, when storing large strings
* in memory), or can be used as a mechanism for deep copying objects.
*
*/
public class SerializedObject implements Serializable {
/** Stores the serialized object */
private byte [] m_Serialized;
/** True if the object has been compressed during storage */
private boolean m_Compressed;
/**
* Serializes the supplied object into a byte array without compression.
*
* @param obj the Object to serialize.
* @throws IOException
* @exception Exception if the object is not Serializable.
*/
public SerializedObject(Object obj) throws IOException {
this(obj, false);
}
/**
* Serializes the supplied object into a byte array.
*
* @param obj the Object to serialize.
* @param compress true if the object should be stored compressed.
* @throws IOException
* @exception Exception if the object is not Serializable.
*/
public SerializedObject(Object obj, boolean compress) throws IOException {
//System.err.print("."); System.err.flush();
m_Compressed = compress;
m_Serialized = toByteArray(obj, m_Compressed);
}
/**
* Serializes the supplied object to a byte array.
*
* @param obj the Object to serialize
* @param compress true if the object should be compressed.
* @return the byte array containing the serialized object.
* @throws IOException
* @exception Exception if the object is not Serializable.
*/
protected static byte [] toByteArray(Object obj, boolean compress) throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
OutputStream os = bo;
if (compress)
os = new GZIPOutputStream(os);
os = new BufferedOutputStream(os);
ObjectOutputStream oo = new ObjectOutputStream(os);
oo.writeObject(obj);
oo.close();
return bo.toByteArray();
}
/**
* Gets the object stored in this SerializedObject. The object returned
* will be a deep copy of the original stored object.
*
* @return the deserialized Object.
*/
public Object getObject() {
try {
InputStream is = new ByteArrayInputStream(m_Serialized);
if (m_Compressed) {
is = new GZIPInputStream(is);
}
is = new BufferedInputStream(is);
ObjectInputStream oi = new ObjectInputStream(is);
Object result = oi.readObject();
oi.close();
return result;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* Compares this object with another for equality.
*
* @param other the other Object.
* @return true if the objects are equal.
*/
public final boolean equals(Object other) {
// Check class type
if ((other == null) || !(other.getClass().equals(this.getClass()))) {
return false;
}
// Check serialized length
byte [] os = ((SerializedObject)other).m_Serialized;
if (os.length != m_Serialized.length) {
return false;
}
// Check serialized contents
for (int i = 0; i < m_Serialized.length; i++) {
if (m_Serialized[i] != os[i]) {
return false;
}
}
return true;
}
/**
* Returns a hashcode for this object.
*
* @return the hashcode for this object.
*/
public final int hashCode() {
return m_Serialized.length;
}
/**
* Returns a text representation of the state of this object.
*
* @return a String representing this object.
*/
public String toString() {
return (m_Compressed ? "Compressed object: " : "Uncompressed object: ")
+ m_Serialized.length + " bytes";
}
}

View File

@@ -0,0 +1,137 @@
package eva2.tools;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* This class stores an object serialized in memory. It allows compression,
* to be used to conserve memory (for example, when storing large strings
* in memory), or can be used as a mechanism for deep copying objects.
*
* @author Holger Ulmer, Felix Streichert, Hannes Planatscher
*/
public class SerializedObject implements Serializable {
/** Stores the serialized object */
private byte [] m_Serialized;
/** True if the object has been compressed during storage */
private boolean m_Compressed;
/**
* Serializes the supplied object into a byte array without compression.
*
* @param obj the Object to serialize.
* @throws IOException
* @exception Exception if the object is not Serializable.
*/
public SerializedObject(Object obj) throws IOException {
this(obj, false);
}
/**
* Serializes the supplied object into a byte array.
*
* @param obj the Object to serialize.
* @param compress true if the object should be stored compressed.
* @throws IOException
* @exception Exception if the object is not Serializable.
*/
public SerializedObject(Object obj, boolean compress) throws IOException {
m_Compressed = compress;
m_Serialized = toByteArray(obj, m_Compressed);
}
/**
* Serializes the supplied object to a byte array.
*
* @param obj the Object to serialize
* @param compress true if the object should be compressed.
* @return the byte array containing the serialized object.
* @throws IOException
* @exception Exception if the object is not Serializable.
*/
protected static byte [] toByteArray(Object obj, boolean compress) throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
OutputStream os = bo;
if (compress)
os = new GZIPOutputStream(os);
os = new BufferedOutputStream(os);
ObjectOutputStream oo = new ObjectOutputStream(os);
oo.writeObject(obj);
oo.close();
return bo.toByteArray();
}
/**
* Gets the object stored in this SerializedObject. The object returned
* will be a deep copy of the original stored object.
*
* @return the deserialized Object.
*/
public Object getObject() {
try {
InputStream is = new ByteArrayInputStream(m_Serialized);
if (m_Compressed) {
is = new GZIPInputStream(is);
}
is = new BufferedInputStream(is);
ObjectInputStream oi = new ObjectInputStream(is);
Object result = oi.readObject();
oi.close();
return result;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* Compares this object with another for equality.
*
* @param other the other Object.
* @return true if the objects are equal.
*/
public final boolean equals(Object other) {
// Check class type
if ((other == null) || !(other.getClass().equals(this.getClass()))) {
return false;
}
// Check serialized length
byte [] os = ((SerializedObject)other).m_Serialized;
if (os.length != m_Serialized.length) {
return false;
}
// Check serialized contents
for (int i = 0; i < m_Serialized.length; i++) {
if (m_Serialized[i] != os[i]) {
return false;
}
}
return true;
}
/**
* Returns a hashcode for this object.
*
* @return the hashcode for this object.
*/
public final int hashCode() {
return m_Serialized.length;
}
/**
* Returns a text representation of the state of this object.
*
* @return a String representing this object.
*/
public String toString() {
return (m_Compressed ? "Compressed object: " : "Uncompressed object: ")
+ m_Serialized.length + " bytes";
}
}

View File

@@ -1,17 +1,5 @@
package eva2.tools; package eva2.tools;
/**
* Title: EvA2
* Description:
* Copyright: Copyright (c) 2003
* Company: University of Tuebingen, Computer Architecture
* @author Holger Ulmer, Felix Streichert, Hannes Planatscher
* @version: $Revision: 319 $
* $Date: 2007-12-05 11:29:32 +0100 (Wed, 05 Dec 2007) $
* $Author: mkron $
*/
/*==========================================================================*
* IMPORTS
*==========================================================================*/
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@@ -23,14 +11,12 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import eva2.gui.SerializedObject;
import eva2.server.go.problems.PSymbolicRegression;
import eva2.server.modules.GOParameters;
/*==========================================================================*
* CLASS DECLARATION
*==========================================================================*/
/** /**
* This class defines utility routines that use Java serialization. * This class defines utility routines that use Java serialization. Any
* serializable object can be stored to a file, loaded, and cloned (returning
* a deep copy).
*
* @author Holger Ulmer, Felix Streichert, Hannes Planatscher, Marcel Kronfeld
**/ **/
public class Serializer { public class Serializer {
/** /**
@@ -50,7 +36,7 @@ public class Serializer {
try { try {
Object objToStore = o; Object objToStore = o;
if (serializeInMem) objToStore = new SerializedObject((Object)o); if (serializeInMem) objToStore = new SerializedObject((Object)o);
// System.out.println("Writing " + o.getClass()); // System.out.println("Writing " + o.getClass());
out.writeObject(objToStore); out.writeObject(objToStore);
} catch (java.io.NotSerializableException e) { } catch (java.io.NotSerializableException e) {
System.err.println("Error: Object " + o.getClass() + " is not serializable - run settings cannot be stored."); System.err.println("Error: Object " + o.getClass() + " is not serializable - run settings cannot be stored.");
@@ -60,15 +46,15 @@ public class Serializer {
out.close(); out.close();
file.close(); file.close();
} }
// try { // try {
// FileOutputStream OutStream = new FileOutputStream("ESPara.ser"); // FileOutputStream OutStream = new FileOutputStream("ESPara.ser");
// ObjectOutputStream OutObjectStream = new ObjectOutputStream (OutStream); // ObjectOutputStream OutObjectStream = new ObjectOutputStream (OutStream);
// OutObjectStream.writeObject(this); // OutObjectStream.writeObject(this);
// OutObjectStream.flush(); // OutObjectStream.flush();
// OutStream.close(); // OutStream.close();
// } catch (Exception e) { // } catch (Exception e) {
// System.out.println ("ERROR ESPara.ser"+e.getMessage()); // System.out.println ("ERROR ESPara.ser"+e.getMessage());
// } // }
/** /**
* Deserialize the contents of File f and return the resulting object. * Deserialize the contents of File f and return the resulting object.
@@ -85,16 +71,20 @@ public class Serializer {
} }
/** /**
* Returns a copy of the object, or null if the object cannot * Use object serialization to make a "deep clone" of the object o.
* be serialized. * This method serializes o and all of its member objects, and then
*/ * deserializes that graph of objects, which means that everything is
public static Object deepClone(Object orig) { * copied. This differs from the clone() method of an object which is
* usually implemented to produce a "shallow" clone that copies references
* to other objects, instead of copying all referenced objects.
**/
public static Object deepClone(Object o) {
Object obj = null; Object obj = null;
try { try {
// Write the object out to a byte array // Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos); ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(orig); out.writeObject(o);
out.flush(); out.flush();
out.close(); out.close();
@@ -113,52 +103,14 @@ public class Serializer {
return obj; return obj;
} }
/**
* Use object serialization to make a "deep clone" of the object o.
* This method serializes o and all objects it refers to, and then
* deserializes that graph of objects, which means that everything is
* copied. This differs from the clone() method of an object which is
* usually implemented to produce a "shallow" clone that copies references
* to other objects, instead of copying all referenced objects.
**/
// static public Object deepclone(final Serializable o) throws IOException, ClassNotFoundException {
// // Create a connected pair of "piped" streams.
// // We'll write bytes to one, and them from the other one.
// final PipedOutputStream pipeout = new PipedOutputStream();
// PipedInputStream pipein = new PipedInputStream(pipeout);
// // Now define an independent thread to serialize the object and write
// // its bytes to the PipedOutputStream
// Thread writer = new Thread() {
// public void run() {
// ObjectOutputStream out = null;
// try {
// out = new ObjectOutputStream(pipeout);
// out.writeObject(o); }
// catch(IOException e) {
// System.out.println("ERROR in Serialization1"+ e.getMessage());
// }
// finally {
// try { out.close(); } catch (Exception e) {
// System.out.println("ERROR in Serialization2"+ e.getMessage());
// }
// }
// }
// };
// writer.start();
// // Meanwhile, in this thread, read and deserialize from the piped
// // input stream. The resulting object is a deep clone of the original.
// ObjectInputStream in = new ObjectInputStream(pipein);
// return in.readObject();
// }
/** /**
* This is a simple serializable data structure that we use below for * This is a simple serializable data structure that we use below for
* testing the methods above * testing the methods above
**/ **/
public static class DataStructure implements Serializable { static class ExampleDataStruct implements Serializable {
String message; String message;
int[] data; int[] data;
DataStructure other; ExampleDataStruct other;
public String toString() { public String toString() {
String s = message; String s = message;
for(int i = 0; i < data.length; i++) for(int i = 0; i < data.length; i++)
@@ -170,10 +122,10 @@ public class Serializer {
public static void main(String[] args) throws IOException, ClassNotFoundException { public static void main(String[] args) throws IOException, ClassNotFoundException {
// Create a simple object graph // Create a simple object graph
DataStructure ds = new DataStructure(); ExampleDataStruct ds = new ExampleDataStruct();
ds.message = "hello world"; ds.message = "hello world";
ds.data = new int[] { 1, 2, 3, 4 }; ds.data = new int[] { 1, 2, 3, 4 };
ds.other = new DataStructure(); ds.other = new ExampleDataStruct();
ds.other.message = "nested structure"; ds.other.message = "nested structure";
ds.other.data = new int[] { 9, 8, 7 }; ds.other.data = new int[] { 9, 8, 7 };
// Display the original object graph // Display the original object graph
@@ -183,11 +135,11 @@ public class Serializer {
System.out.println("Storing to a file..."); System.out.println("Storing to a file...");
Serializer.store(ds, f, true); Serializer.store(ds, f, true);
// Read it back from the file, and display it again // Read it back from the file, and display it again
ds = (DataStructure) Serializer.load(f); ds = (ExampleDataStruct) Serializer.load(f);
System.out.println("Read from the file: " + ds); System.out.println("Read from the file: " + ds);
// Create a deep clone and display that. After making the copy // Create a deep clone and display that. After making the copy
// modify the original to prove that the clone is "deep". // modify the original to prove that the clone is "deep".
DataStructure ds2 = (DataStructure) Serializer.deepClone(ds); ExampleDataStruct ds2 = (ExampleDataStruct) Serializer.deepClone(ds);
ds.other.message = null; ds.other.data = null; // Change original ds.other.message = null; ds.other.data = null; // Change original
System.out.println("Deep clone: " + ds2); System.out.println("Deep clone: " + ds2);
} }