1
2
3
4
5 package org.gscg.common.interactionlayer;
6
7 import java.io.ByteArrayOutputStream;
8 import java.io.ObjectOutputStream;
9
10 /***
11 * http://www.churchillobjects.com/c/13029.html
12 * <p>
13 * See for project information <a href="http://www.simulation.tudelft.nl">
14 * www.simulation.tudelft.nl </a>
15 * <p>
16 *
17 * @author http://www.churchillobjects.com/c/13029.html
18 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:58 $
19 * @since 1.0.0
20 */
21 public final class ObjectMeter
22 {
23 /***
24 * constructs a new ObjectMeter
25 */
26 private ObjectMeter()
27 {
28
29 }
30
31 /***
32 * Determines the size of an object in bytes when it is serialized. This
33 * should not be used for anything other than optimization testing since it
34 * can be memory and processor intensive.
35 *
36 * @param object the object to get the size from
37 * @return returns an integer reflecting the object size in bytes
38 */
39 public static int getObjectSize(final Object object)
40 {
41 if (object == null)
42 {
43 System.err.println("Object is null. Cannot measure.");
44 return -1;
45 }
46 try
47 {
48 ByteArrayOutputStream baos = new ByteArrayOutputStream();
49 ObjectOutputStream oos = new ObjectOutputStream(baos);
50 oos.writeObject(object);
51 byte[] bytes = baos.toByteArray();
52 oos.close();
53 baos.close();
54 return bytes.length;
55 } catch (Exception e)
56 {
57 e.printStackTrace();
58 }
59 return -1;
60 }
61 }