From 47b45c6fadbd0b468386949d4452829fe4fcd17e Mon Sep 17 00:00:00 2001 From: Brent Worden Date: Sat, 13 Aug 2005 04:06:37 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/math/TestUtils.java | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/test/org/apache/commons/math/TestUtils.java b/src/test/org/apache/commons/math/TestUtils.java index 7f160cb03..2aa67175b 100644 --- a/src/test/org/apache/commons/math/TestUtils.java +++ b/src/test/org/apache/commons/math/TestUtils.java @@ -19,6 +19,7 @@ package org.apache.commons.math; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; @@ -74,25 +75,43 @@ public class TestUtils { 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;