You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dl...@apache.org on 2001/12/29 01:20:23 UTC

cvs commit: jakarta-commons-sandbox/util/src/java/org/apache/commons/util ObjectUtils.java

dlr         01/12/28 16:20:23

  Modified:    util/src/java/org/apache/commons/util ObjectUtils.java
  Log:
  Integrated patch by Janek Bogucki <ja...@yahoo.co.uk> which makes
  minor speed improvements by removing the use of a redundant RAM
  buffer, but--more importantly--reduces the amount and complexity of
  the code.  I made minor tweaks to Janek's patch and cleaned up the
  rest of file:
  
  I have a suggestion for improving the performance of
  ObjectUtils.deserialize( byte[] objectData ). I did
  some tests which showed improvement for the
  deserialisation of a serialised Strings with the new
  version.
  
  Here are the figures with elapsed times in millis:
  
  Time to deserialize "Hello World!" 1000*1000 times:
  
      original      new
         21408    12186
         21259    12134
  
  Time to deserialize a String 10*000 chars long
  100*1000 times:
  
      original      new
         69611    66650
         69412    67809
  
  Time to deserialize a String 100*000 chars long
  10*1000 times:
  
      original      new
        122917   122837
        123742   122635
  
  The change to get this improvement is to omit the
  BufferedInputStream object as buffering won't be of
  any benefit when the data is already in RAM.
  
  (I also dropped the bin.close () invocation as this is
  unneccesary since in.close () invokes bin.close ()
  automatically.)
  
  Revision  Changes    Path
  1.4       +18 -28    jakarta-commons-sandbox/util/src/java/org/apache/commons/util/ObjectUtils.java
  
  Index: ObjectUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/ObjectUtils.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -u -r1.3 -r1.4
  --- ObjectUtils.java	9 Aug 2001 08:19:17 -0000	1.3
  +++ ObjectUtils.java	29 Dec 2001 00:20:22 -0000	1.4
  @@ -61,10 +61,12 @@
   import java.io.ObjectInputStream;
   
   /**
  - * This is where common Object manipulation routines should go.
  + * Common <code>Object</code> manipulation routines.
    *
    * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
  - * @version $Id: ObjectUtils.java,v 1.3 2001/08/09 08:19:17 knielsen Exp $
  + * @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
  + * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  + * @version $Id: ObjectUtils.java,v 1.4 2001/12/29 00:20:22 dlr Exp $
    */
   public class ObjectUtils
   {
  @@ -75,17 +77,9 @@
        * @param dflt The default value to return.
        * @return The object o if it is not null, dflt otherwise.
        */
  -    public static Object isNull(Object o,
  -                                Object dflt)
  +    public static Object isNull(Object o, Object dflt)
       {
  -        if (o == null)
  -        {
  -            return dflt;
  -        }
  -        else
  -        {
  -            return o;
  -        }
  +        return (o != null ? o : dflt);
       }
   
       /**
  @@ -94,37 +88,34 @@
        * @param objectData The serialized object.
        * @return The deserialized object, or <code>null</code> on failure.
        */
  -    public static Object deserialize( byte[] objectData )
  +    public static Object deserialize(byte[] objectData)
       {
           Object object = null;
           if (objectData != null)
           {
  -            // These streams are closed in finally.
               ObjectInputStream in = null;
  -            ByteArrayInputStream bin =
  -                new ByteArrayInputStream(objectData);
  -            BufferedInputStream bufin =
  -                new BufferedInputStream(bin);
               try
               {
  -                in = new ObjectInputStream(bufin);
  +                InputStream bin = new ByteArrayInputStream(objectData);
  +                in = new ObjectInputStream(bin);
   
                   // If objectData has not been initialized, an
                   // exception will occur.
                   object = in.readObject();
               }
  -            catch (Exception e)
  +            catch (Exception returnNull)
               {
               }
               finally
               {
                   try
                   {
  -                    if (in != null) in.close();
  -                    if (bufin != null) bufin.close();
  -                    if (bin != null) bin.close();
  +                    if (in != null)
  +                    {
  +                        in.close();
  +                    }
                   }
  -                catch(IOException e)
  +                catch (IOException ignored)
                   {
                   }
               }
  @@ -133,15 +124,14 @@
       }
   
       /**
  -     * Compares two objects for equality where either one or both objects
  -     * may be null.
  +     * Compares two objects for equality, where either one or both
  +     * objects may be <code>null</code>.
        *
        * @param o1 The first object.
        * @param o2 The second object.
        * @return True if the values of both objects are the same.
        */
  -    public static boolean equals( Object o1,
  -                                  Object o2 )
  +    public static boolean equals(Object o1, Object o2)
       {
           if (o1 == null)
           {
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>