Przeglądaj źródła

* Avoid FD leaking in binary importer/exporter

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10768 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
sha..RD 12 lat temu
rodzic
commit
f94bc8660c

+ 8 - 4
engine/src/core-plugins/com/jme3/export/binary/BinaryExporter.java

@@ -327,14 +327,18 @@ public class BinaryExporter implements JmeExporter {
 
     public boolean save(Savable object, File f) throws IOException {
         File parentDirectory = f.getParentFile();
-        if(parentDirectory != null && !parentDirectory.exists()) {
+        if (parentDirectory != null && !parentDirectory.exists()) {
             parentDirectory.mkdirs();
         }
 
         FileOutputStream fos = new FileOutputStream(f);
-        boolean rVal = save(object, fos);
-        fos.close();
-        return rVal;
+        try {
+            return save(object, fos);
+        } finally {
+            if (fos != null) {
+                fos.close();
+            }
+        }
     }
 
     public BinaryOutputCapsule getCapsule(Savable object) {

+ 7 - 3
engine/src/core-plugins/com/jme3/export/binary/BinaryImporter.java

@@ -267,9 +267,13 @@ public final class BinaryImporter implements JmeImporter {
 
     public Savable load(File f, ReadListener listener) throws IOException {
         FileInputStream fis = new FileInputStream(f);
-        Savable rVal = load(fis, listener);
-        fis.close();
-        return rVal;
+        try {
+            return load(fis, listener);
+        } finally {
+            if (fis != null) {
+                fis.close();
+            }
+        }
     }
 
     public Savable load(byte[] data) throws IOException {