From d048d5d6c17c1d06670fc6c9a21cc65beda4a86e Mon Sep 17 00:00:00 2001 From: "sha..RD" Date: Fri, 13 Sep 2013 00:35:03 +0000 Subject: [PATCH] * Avoid FD leaking in AbstractHeightMap save git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10769 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../jme3/terrain/heightmap/AbstractHeightMap.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/engine/src/terrain/com/jme3/terrain/heightmap/AbstractHeightMap.java b/engine/src/terrain/com/jme3/terrain/heightmap/AbstractHeightMap.java index 63692c61b..21c504528 100644 --- a/engine/src/terrain/com/jme3/terrain/heightmap/AbstractHeightMap.java +++ b/engine/src/terrain/com/jme3/terrain/heightmap/AbstractHeightMap.java @@ -253,9 +253,12 @@ public abstract class AbstractHeightMap implements HeightMap { throw new Exception("Filename must not be null"); } //open the streams and send the height data to the file. + FileOutputStream fos = null; + DataOutputStream dos = null; try { - FileOutputStream fos = new FileOutputStream(filename); - DataOutputStream dos = new DataOutputStream(fos); + fos = new FileOutputStream(filename); + dos = new DataOutputStream(fos); + for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { dos.write((int) heightData[j + (i * size)]); @@ -270,6 +273,13 @@ public abstract class AbstractHeightMap implements HeightMap { } catch (IOException e) { logger.log(Level.WARNING, "Error writing to file {0}", filename); return false; + } finally { + if (fos != null) { + fos.close(); + } + if (dos != null) { + dos.close(); + } } logger.log(Level.FINE, "Saved terrain to {0}", filename);