You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2008/08/20 23:33:52 UTC

svn commit: r687473 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilObject.java

Author: adrianc
Date: Wed Aug 20 14:33:52 2008
New Revision: 687473

URL: http://svn.apache.org/viewvc?rev=687473&view=rev
Log:
Small fix for UtilObject. The getByteCount(Object obj) method was returning unusual results for non-serializable objects. I also added an explanatory note in the JavaDoc.

An interesting article on getting the size of Java objects - http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html?page=1.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilObject.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilObject.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilObject.java?rev=687473&r1=687472&r2=687473&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilObject.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilObject.java Wed Aug 20 14:33:52 2008
@@ -91,14 +91,25 @@
         return data;
     }
 
+    /** Returns the size of a serializable object. Non-serializable objects
+     * will output an error message and return zero.<p>It is important to note
+     * that the returned value is length of the byte stream after the object has
+     * been serialized. The returned value does not represent the amount of memory
+     * the object uses. There is no accurate way to determine the size of an
+     * object in memory.</p>
+     * @param obj
+     * @return
+     */
     public static long getByteCount(Object obj) {
         OutputStreamByteCount osbc = null;
         ObjectOutputStream oos = null;
+        long objSize = 0;
         try {
             osbc = new OutputStreamByteCount();
             oos = new ObjectOutputStream(osbc);
             oos.writeObject(obj);
-        } catch (IOException e) {
+            objSize = osbc.getByteCount();
+        } catch (Exception e) {
             Debug.logError(e, module);
         } finally {
             try {
@@ -113,11 +124,7 @@
                 Debug.logError(e, module);
             }
         }
-        if (osbc != null) {
-            return osbc.getByteCount();
-        } else {
-            return 0;
-        }
+        return objSize;
     }
 
     /** Deserialize a byte array back to an object */