commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r611492 - /commons/proper/math/trunk/src/test/org/apache/commons/math/TestUtils.java
Date Sat, 12 Jan 2008 21:52:17 GMT
Author: luc
Date: Sat Jan 12 13:52:16 2008
New Revision: 611492

URL: http://svn.apache.org/viewvc?rev=611492&view=rev
Log:
replaced serialization to temporary file by serialization to byte array
and simplified the error handling to remove a findbugs warning

Modified:
    commons/proper/math/trunk/src/test/org/apache/commons/math/TestUtils.java

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/TestUtils.java?rev=611492&r1=611491&r2=611492&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/TestUtils.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/TestUtils.java Sat Jan 12 13:52:16
2008
@@ -17,9 +17,8 @@
 
 package org.apache.commons.math;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
@@ -100,55 +99,28 @@
     }
     
     /**
-     * Serializes an object to a temp file and then recovers the object from the file.
+     * Serializes an object to a bytes array and then recovers the object from the bytes
array.
      * Returns the deserialized object.
      * 
      * @param o  object to serialize and recover
-     * @return  the recovered, deseriailized object
+     * @return  the recovered, deserialized object
      */
-    public static Object serializeAndRecover(Object o){
-        
-        Object result = null;
-        File tmp = null;
-        FileOutputStream fo = null;
-        FileInputStream fi = null;
-        
+    public static Object serializeAndRecover(Object o) {
         try {
             // serialize the Object
-            tmp = File.createTempFile("test",".ser");
-            fo = new FileOutputStream(tmp);
-            ObjectOutputStream so = new ObjectOutputStream(fo);
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            ObjectOutputStream so = new ObjectOutputStream(bos);
             so.writeObject(o);
-            so.flush();
-            fo.close();
 
             // deserialize the Object
-            fi = new FileInputStream(tmp);
-            ObjectInputStream si = new ObjectInputStream(fi);  
-            result = si.readObject();
-        } catch (Exception ex) {
-            
-        } finally {
-            if (fo != null) {
-                try {
-                    fo.close();
-                } catch (IOException ex) {
-                }
-            }
-
-            if (fi != null) {
-                try {
-                    fi.close();
-                } catch (IOException ex) {
-                }
-            }
+            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
+            ObjectInputStream si = new ObjectInputStream(bis);
+            return si.readObject();
+        } catch (IOException ioe) {
+            return null;
+        } catch (ClassNotFoundException cnfe) {
+            return null;
         }
-           
-        if (tmp != null) {
-            tmp.delete();
-        }
-        
-        return result;
     }
     
     /**



Mime
View raw message