001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * SerializedObject.java
019: * Copyright (C) 2001 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.core;
024:
025: import java.io.BufferedInputStream;
026: import java.io.BufferedOutputStream;
027: import java.io.ByteArrayInputStream;
028: import java.io.ByteArrayOutputStream;
029: import java.io.ObjectInputStream;
030: import java.io.ObjectOutputStream;
031: import java.io.OutputStream;
032: import java.io.Serializable;
033: import java.util.zip.GZIPInputStream;
034: import java.util.zip.GZIPOutputStream;
035:
036: /**
037: * Class for storing an object in serialized form in memory. It can be used
038: * to make deep copies of objects, and also allows compression to conserve
039: * memory. <p>
040: *
041: * @author Richard Kirkby (rbk1@cs.waikato.ac.nz)
042: * @version $Revision: 1.10 $
043: */
044: public class SerializedObject implements Serializable {
045:
046: /** for serialization */
047: private static final long serialVersionUID = 6635502953928860434L;
048:
049: /** The array storing the object. */
050: private byte[] m_storedObjectArray;
051:
052: /** Whether or not the object is compressed. */
053: private boolean m_isCompressed;
054:
055: /**
056: * Creates a new serialized object (without compression).
057: *
058: * @param toStore the object to store
059: * @exception Exception if the object couldn't be serialized
060: */
061: public SerializedObject(Object toStore) throws Exception {
062:
063: this (toStore, false);
064: }
065:
066: /**
067: * Creates a new serialized object.
068: *
069: * @param toStore the object to store
070: * @param compress whether or not to use compression
071: * @exception Exception if the object couldn't be serialized
072: */
073: public SerializedObject(Object toStore, boolean compress)
074: throws Exception {
075:
076: ByteArrayOutputStream ostream = new ByteArrayOutputStream();
077: OutputStream os = ostream;
078: ObjectOutputStream p;
079: if (!compress)
080: p = new ObjectOutputStream(new BufferedOutputStream(os));
081: else
082: p = new ObjectOutputStream(new BufferedOutputStream(
083: new GZIPOutputStream(os)));
084: p.writeObject(toStore);
085: p.flush();
086: p.close(); // used to be ostream.close() !
087: m_storedObjectArray = ostream.toByteArray();
088:
089: m_isCompressed = compress;
090: }
091:
092: /*
093: * Checks to see whether this object is equal to another.
094: *
095: * @param compareTo the object to compare to
096: * @return whether or not the objects are equal
097: */
098: public final boolean equals(Object compareTo) {
099:
100: if (compareTo == null)
101: return false;
102: if (!compareTo.getClass().equals(this .getClass()))
103: return false;
104: byte[] compareArray = ((SerializedObject) compareTo).m_storedObjectArray;
105: if (compareArray.length != m_storedObjectArray.length)
106: return false;
107: for (int i = 0; i < compareArray.length; i++) {
108: if (compareArray[i] != m_storedObjectArray[i])
109: return false;
110: }
111: return true;
112: }
113:
114: /**
115: * Returns a hashcode for this object.
116: *
117: * @return the hashcode
118: */
119: public int hashCode() {
120:
121: return m_storedObjectArray.length;
122: }
123:
124: /**
125: * Returns a serialized object.
126: *
127: * @return the restored object
128: * @exception Exception if the object couldn't be restored
129: */
130: public Object getObject() {
131:
132: try {
133: ByteArrayInputStream istream = new ByteArrayInputStream(
134: m_storedObjectArray);
135: ObjectInputStream p;
136: if (!m_isCompressed)
137: p = new ObjectInputStream(new BufferedInputStream(
138: istream));
139: else
140: p = new ObjectInputStream(new BufferedInputStream(
141: new GZIPInputStream(istream)));
142: Object toReturn = p.readObject();
143: istream.close();
144: return toReturn;
145: } catch (Exception e) {
146: e.printStackTrace();
147: return null;
148: }
149: }
150: }
|