source: trunk/gsdl/packages/kea/kea-3.0/weka/core/SerializedObject.java@ 8815

Last change on this file since 8815 was 8815, checked in by mdewsnip, 19 years ago

Kea 3.0, as downloaded from http://www.nzdl.org/kea but with CSTR_abstracts_test, CSTR_abstracts_train, Chinese_test, and Chinese_train directories removed.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
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) 2000 Webmind Inc.
20 *
21 */
22
23package weka.core;
24
25import java.io.BufferedInputStream;
26import java.io.BufferedOutputStream;
27import java.io.ByteArrayInputStream;
28import java.io.ByteArrayOutputStream;
29import java.io.InputStream;
30import java.io.ObjectInputStream;
31import java.io.ObjectOutputStream;
32import java.io.OutputStream;
33import java.io.Serializable;
34import java.util.zip.GZIPInputStream;
35import java.util.zip.GZIPOutputStream;
36
37/**
38 * This class stores an object serialized in memory. It allows compression,
39 * to be used to conserve memory (for example, when storing large strings
40 * in memory), or can be used as a mechanism for deep copying objects.
41 *
42 * @author Len Trigg ([email protected])
43 * @version $Revision: 8815 $
44 */
45public class SerializedObject implements Serializable {
46
47 /** Stores the serialized object */
48 private byte [] m_Serialized;
49
50 /** True if the object has been compressed during storage */
51 private boolean m_Compressed;
52
53 /**
54 * Serializes the supplied object into a byte array without compression.
55 *
56 * @param obj the Object to serialize.
57 * @exception Exception if the object is not Serializable.
58 */
59 public SerializedObject(Object obj) throws Exception {
60
61 this(obj, false);
62 }
63
64 /**
65 * Serializes the supplied object into a byte array.
66 *
67 * @param obj the Object to serialize.
68 * @param compress true if the object should be stored compressed.
69 * @exception Exception if the object is not Serializable.
70 */
71 public SerializedObject(Object obj, boolean compress) throws Exception {
72
73 //System.err.print("."); System.err.flush();
74 m_Compressed = compress;
75 m_Serialized = toByteArray(obj, m_Compressed);
76 }
77
78 /**
79 * Serializes the supplied object to a byte array.
80 *
81 * @param obj the Object to serialize
82 * @param compress true if the object should be compressed.
83 * @return the byte array containing the serialized object.
84 * @exception Exception if the object is not Serializable.
85 */
86 protected static byte [] toByteArray(Object obj, boolean compress)
87 throws Exception {
88
89 ByteArrayOutputStream bo = new ByteArrayOutputStream();
90 OutputStream os = bo;
91 if (compress) {
92 os = new GZIPOutputStream(os);
93 }
94 os = new BufferedOutputStream(os);
95 ObjectOutputStream oo = new ObjectOutputStream(os);
96 oo.writeObject(obj);
97 oo.close();
98 return bo.toByteArray();
99 }
100
101 /**
102 * Gets the object stored in this SerializedObject. The object returned
103 * will be a deep copy of the original stored object.
104 *
105 * @return the deserialized Object.
106 */
107 public Object getObject() {
108 try {
109 InputStream is = new ByteArrayInputStream(m_Serialized);
110 if (m_Compressed) {
111 is = new GZIPInputStream(is);
112 }
113 is = new BufferedInputStream(is);
114 ObjectInputStream oi = new ObjectInputStream(is);
115 Object result = oi.readObject();
116 oi.close();
117 return result;
118 } catch (Exception ex) {
119 ex.printStackTrace();
120 }
121 return null;
122 }
123
124 /**
125 * Compares this object with another for equality.
126 *
127 * @param other the other Object.
128 * @return true if the objects are equal.
129 */
130 public final boolean equals(Object other) {
131
132 // Check class type
133 if ((other == null) || !(other.getClass().equals(this.getClass()))) {
134 return false;
135 }
136 // Check serialized length
137 byte [] os = ((SerializedObject)other).m_Serialized;
138 if (os.length != m_Serialized.length) {
139 return false;
140 }
141 // Check serialized contents
142 for (int i = 0; i < m_Serialized.length; i++) {
143 if (m_Serialized[i] != os[i]) {
144 return false;
145 }
146 }
147 return true;
148 }
149
150 /**
151 * Returns a hashcode for this object.
152 *
153 * @return the hashcode for this object.
154 */
155 public final int hashCode() {
156
157 return m_Serialized.length;
158 }
159
160 /**
161 * Returns a text representation of the state of this object.
162 *
163 * @return a String representing this object.
164 */
165 public String toString() {
166
167 return (m_Compressed ? "Compressed object: " : "Uncompressed object: ")
168 + m_Serialized.length + " bytes";
169 }
170
171 /**
172 * Test routine, reads text from stdin and measures memory usage
173 */
174 public static void main2(String []args) {
175
176 try {
177 Runtime r = Runtime.getRuntime();
178 r.gc();
179 java.io.LineNumberReader lnr = new java.io.LineNumberReader(new java.io.InputStreamReader(System.in));
180 StringBuffer sb = new StringBuffer();
181 String line;
182 while ((line = lnr.readLine()) != null) {
183 sb.append(line).append('\n');
184 }
185 String text = sb.toString();
186 //System.err.println("TEXT:");
187 //System.err.println(text);
188 r.gc();
189
190 System.err.print("TOTAL: " + r.totalMemory());
191 System.err.println("\tFREE: " + r.freeMemory());
192 long used = r.totalMemory() - r.freeMemory();
193
194 // convert to a compressedobject
195 SerializedObject co = new SerializedObject(text, true);
196
197 System.err.print("TOTAL: " + r.totalMemory());
198 System.err.println("\tFREE: " + r.freeMemory());
199 r.gc();
200 long used1 = r.totalMemory() - r.freeMemory();
201 long csize = used1 - used;
202 System.err.println("CompressedSize = " + csize);
203
204 String newstr = (String)co.getObject();
205 r.gc();
206 System.err.print("TOTAL: " + r.totalMemory());
207 System.err.println("\tFREE: " + r.freeMemory());
208 long used2 = r.totalMemory() - r.freeMemory();
209 long usize = used2 - used1;
210 System.err.println("UncompressedSize = " + usize);
211
212 // A couple of references to the original objects
213 // so they don't get freed prematurely and muck up the
214 // measurements.
215 newstr = newstr.toLowerCase();
216 text = text.toLowerCase();
217 System.err.println("Byte array: " + co.toString());
218 System.err.println("Length of text: " + text.length());
219 } catch (Exception ex) {
220 ex.printStackTrace();
221 }
222 }
223
224
225 /**
226 * Test routine, reads an arff file from stdin and measures memory usage
227 * (the arff file should have long string attribute values)
228 */
229 public static void main(String []args) {
230
231 try {
232 Runtime r = Runtime.getRuntime();
233 r.gc();
234 System.err.print("TOTAL: " + r.totalMemory());
235 System.err.println("\tFREE: " + r.freeMemory());
236 long used = r.totalMemory() - r.freeMemory();
237 Instances inst = new Instances(new java.io.BufferedReader(new java.io.InputStreamReader(System.in)));
238 r.gc();
239 long used1 = r.totalMemory() - r.freeMemory();
240 long csize = used1 - used;
241 System.err.print("\nTOTAL: " + r.totalMemory());
242 System.err.println("\tFREE: " + r.freeMemory());
243 System.err.println("size = " + csize);
244 int blah = inst.numAttributes();
245 } catch (Exception ex) {
246 ex.printStackTrace();
247 }
248 }
249}
Note: See TracBrowser for help on using the repository browser.