Bundle: fix memory leak at init/reset
authorNiki Roo <niki@nikiroo.be>
Sat, 23 May 2020 13:29:17 +0000 (15:29 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 23 May 2020 13:29:17 +0000 (15:29 +0200)
src/be/nikiroo/utils/resources/Bundle.java

index 84efceafabfacac5d1edf887dc49f3542bd76725..fe3ac1a3be4eb2c66580f855ea9df1ee206085dc 100644 (file)
@@ -5,6 +5,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
@@ -1168,13 +1169,21 @@ public class Bundle<E extends Enum<E>> {
 
                boolean found = false;
                if (!resetToDefault && dir != null) {
-                       // Look into Bundles.getDirectory() for .properties files
                        try {
+                               // Look into Bundles.getDirectory() for .properties files
                                File file = getPropertyFile(dir, name.name(), locale);
                                if (file != null) {
-                                       Reader reader = new InputStreamReader(new FileInputStream(
-                                                       file), "UTF-8");
-                                       resetMap(new PropertyResourceBundle(reader));
+                                       InputStream in = new FileInputStream(file);
+                                       try {
+                                               Reader reader = new InputStreamReader(in, "UTF-8");
+                                               try {
+                                                       resetMap(new PropertyResourceBundle(reader));
+                                               } finally {
+                                                       reader.close();
+                                               }
+                                       } finally {
+                                               in.close();
+                                       }
                                        found = true;
                                }
                        } catch (IOException e) {