1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * SerializedObject.java |
---|
19 | * Copyright (C) 2001 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.core; |
---|
24 | |
---|
25 | import weka.core.scripting.Jython; |
---|
26 | import weka.core.scripting.JythonSerializableObject; |
---|
27 | |
---|
28 | import java.io.BufferedInputStream; |
---|
29 | import java.io.BufferedOutputStream; |
---|
30 | import java.io.ByteArrayInputStream; |
---|
31 | import java.io.ByteArrayOutputStream; |
---|
32 | import java.io.ObjectInputStream; |
---|
33 | import java.io.ObjectOutputStream; |
---|
34 | import java.io.OutputStream; |
---|
35 | import java.io.Serializable; |
---|
36 | import java.util.zip.GZIPInputStream; |
---|
37 | import java.util.zip.GZIPOutputStream; |
---|
38 | |
---|
39 | /** |
---|
40 | * Class for storing an object in serialized form in memory. It can be used |
---|
41 | * to make deep copies of objects, and also allows compression to conserve |
---|
42 | * memory. <p> |
---|
43 | * |
---|
44 | * @author Richard Kirkby (rbk1@cs.waikato.ac.nz) |
---|
45 | * @version $Revision: 5018 $ |
---|
46 | */ |
---|
47 | public class SerializedObject |
---|
48 | implements Serializable, RevisionHandler { |
---|
49 | |
---|
50 | /** for serialization */ |
---|
51 | private static final long serialVersionUID = 6635502953928860434L; |
---|
52 | |
---|
53 | /** The array storing the object. */ |
---|
54 | private byte[] m_storedObjectArray; |
---|
55 | |
---|
56 | /** Whether or not the object is compressed. */ |
---|
57 | private boolean m_isCompressed; |
---|
58 | |
---|
59 | /** Whether it is a Jython object or not */ |
---|
60 | private boolean m_isJython; |
---|
61 | |
---|
62 | /** |
---|
63 | * Creates a new serialized object (without compression). |
---|
64 | * |
---|
65 | * @param toStore the object to store |
---|
66 | * @exception Exception if the object couldn't be serialized |
---|
67 | */ |
---|
68 | public SerializedObject(Object toStore) throws Exception { |
---|
69 | |
---|
70 | this(toStore, false); |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Creates a new serialized object. |
---|
75 | * |
---|
76 | * @param toStore the object to store |
---|
77 | * @param compress whether or not to use compression |
---|
78 | * @exception Exception if the object couldn't be serialized |
---|
79 | */ |
---|
80 | public SerializedObject(Object toStore, boolean compress) throws Exception { |
---|
81 | |
---|
82 | ByteArrayOutputStream ostream = new ByteArrayOutputStream(); |
---|
83 | OutputStream os = ostream; |
---|
84 | ObjectOutputStream p; |
---|
85 | if (!compress) |
---|
86 | p = new ObjectOutputStream(new BufferedOutputStream(os)); |
---|
87 | else |
---|
88 | p = new ObjectOutputStream(new BufferedOutputStream(new GZIPOutputStream(os))); |
---|
89 | p.writeObject(toStore); |
---|
90 | p.flush(); |
---|
91 | p.close(); // used to be ostream.close() ! |
---|
92 | m_storedObjectArray = ostream.toByteArray(); |
---|
93 | |
---|
94 | m_isCompressed = compress; |
---|
95 | m_isJython = (toStore instanceof JythonSerializableObject); |
---|
96 | } |
---|
97 | |
---|
98 | /* |
---|
99 | * Checks to see whether this object is equal to another. |
---|
100 | * |
---|
101 | * @param compareTo the object to compare to |
---|
102 | * @return whether or not the objects are equal |
---|
103 | */ |
---|
104 | public final boolean equals(Object compareTo) { |
---|
105 | |
---|
106 | if (compareTo == null) return false; |
---|
107 | if (!compareTo.getClass().equals(this.getClass())) return false; |
---|
108 | byte[] compareArray = ((SerializedObject)compareTo).m_storedObjectArray; |
---|
109 | if (compareArray.length != m_storedObjectArray.length) return false; |
---|
110 | for (int i=0; i<compareArray.length; i++) { |
---|
111 | if (compareArray[i] != m_storedObjectArray[i]) return false; |
---|
112 | } |
---|
113 | return true; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Returns a hashcode for this object. |
---|
118 | * |
---|
119 | * @return the hashcode |
---|
120 | */ |
---|
121 | public int hashCode() { |
---|
122 | |
---|
123 | return m_storedObjectArray.length; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Returns a serialized object. Uses org.python.util.PythonObjectInputStream |
---|
128 | * for Jython objects (read |
---|
129 | * <a href="http://aspn.activestate.com/ASPN/Mail/Message/Jython-users/1001401">here</a> |
---|
130 | * for more details). |
---|
131 | * |
---|
132 | * @return the restored object |
---|
133 | * @exception Exception if the object couldn't be restored |
---|
134 | */ |
---|
135 | public Object getObject() { |
---|
136 | |
---|
137 | try { |
---|
138 | ByteArrayInputStream istream = new ByteArrayInputStream(m_storedObjectArray); |
---|
139 | ObjectInputStream p; |
---|
140 | Object toReturn = null; |
---|
141 | if (m_isJython) { |
---|
142 | if (!m_isCompressed) |
---|
143 | toReturn = Jython.deserialize(new BufferedInputStream(istream)); |
---|
144 | else |
---|
145 | toReturn = Jython.deserialize(new BufferedInputStream(new GZIPInputStream(istream))); |
---|
146 | } |
---|
147 | else { |
---|
148 | if (!m_isCompressed) |
---|
149 | p = new ObjectInputStream(new BufferedInputStream(istream)); |
---|
150 | else |
---|
151 | p = new ObjectInputStream(new BufferedInputStream(new GZIPInputStream(istream))); |
---|
152 | toReturn = p.readObject(); |
---|
153 | } |
---|
154 | istream.close(); |
---|
155 | return toReturn; |
---|
156 | } catch (Exception e) { |
---|
157 | e.printStackTrace(); |
---|
158 | return null; |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Returns the revision string. |
---|
164 | * |
---|
165 | * @return the revision |
---|
166 | */ |
---|
167 | public String getRevision() { |
---|
168 | return RevisionUtils.extract("$Revision: 5018 $"); |
---|
169 | } |
---|
170 | } |
---|