PR 36084: Temp. files were not being deleted because open file streams 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
This commit is contained in:
Brent Worden 2005-08-13 04:06:37 +00:00
parent 4eb8044ae7
commit 47b45c6fad
1 changed files with 27 additions and 8 deletions

View File

@ -19,6 +19,7 @@ package org.apache.commons.math;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
@ -74,25 +75,43 @@ public class TestUtils {
Object result = null; Object result = null;
File tmp = null; File tmp = null;
FileOutputStream fo = null;
FileInputStream fi = null;
try { try {
// serialize the Object // serialize the Object
tmp = File.createTempFile("test",".ser"); tmp = File.createTempFile("test",".ser");
FileOutputStream fo = new FileOutputStream(tmp); fo = new FileOutputStream(tmp);
ObjectOutputStream so = new ObjectOutputStream(fo); ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(o); so.writeObject(o);
so.flush(); so.flush();
fo.close();
// deserialize the Book // deserialize the Book
FileInputStream fi = new FileInputStream(tmp); fi = new FileInputStream(tmp);
ObjectInputStream si = new ObjectInputStream(fi); ObjectInputStream si = new ObjectInputStream(fi);
result = si.readObject(); result = si.readObject();
} catch (Exception ex) {
}catch (Exception e) {
e.printStackTrace(); } finally {
}finally{ if (fo != null) {
if(tmp != null) tmp.delete(); try {
fo.close();
} catch (IOException ex) {
}
}
if (fi != null) {
try {
fi.close();
} catch (IOException ex) {
}
}
}
if (tmp != null) {
tmp.delete();
} }
return result; return result;