Browse Source

* Avoid FD leaking in AbstractHeightMap save

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10769 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
sha..RD 12 years ago
parent
commit
d048d5d6c1
1 changed files with 12 additions and 2 deletions
  1. 12 2
      engine/src/terrain/com/jme3/terrain/heightmap/AbstractHeightMap.java

+ 12 - 2
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);