Skip to content

Commit

Permalink
PR 36084: Temp. files were not being deleted because open file stream…
Browse files Browse the repository at this point in the history
…s were not being closed properly.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@232415 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Brent Worden committed Aug 13, 2005
1 parent 4eb8044 commit 47b45c6
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/test/org/apache/commons/math/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

Expand Down Expand Up @@ -74,25 +75,43 @@ public static Object serializeAndRecover(Object o){
Object result = null;

File tmp = null;
FileOutputStream fo = null;
FileInputStream fi = null;

try {

// serialize the Object
tmp = File.createTempFile("test",".ser");
FileOutputStream fo = new FileOutputStream(tmp);
fo = new FileOutputStream(tmp);
ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(o);
so.flush();
fo.close();

// deserialize the Book
FileInputStream fi = new FileInputStream(tmp);
fi = new FileInputStream(tmp);
ObjectInputStream si = new ObjectInputStream(fi);
result = si.readObject();

}catch (Exception e) {
e.printStackTrace();
}finally{
if(tmp != null) tmp.delete();
} catch (Exception ex) {

} finally {
if (fo != null) {
try {
fo.close();
} catch (IOException ex) {
}
}

if (fi != null) {
try {
fi.close();
} catch (IOException ex) {
}
}
}


if (tmp != null) {
tmp.delete();
}

return result;
Expand Down

0 comments on commit 47b45c6

Please sign in to comment.