Close the 'DirectoryStream' after its be used. 53/34453/1
authorYuanHu <yuan.hu1@zte.com.cn>
Wed, 7 Mar 2018 07:49:39 +0000 (15:49 +0800)
committerYuanHu <yuan.hu1@zte.com.cn>
Wed, 7 Mar 2018 07:49:39 +0000 (15:49 +0800)
Close the 'DirectoryStream' after its be used.

Issue-ID: SDC-1080

Change-Id: Ic907bc58df838b9ed0fe24b1f943f259b08c3a08
Signed-off-by: YuanHu <yuan.hu1@zte.com.cn>
sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/FileCommonUtils.java

index bdea66e..c4f455d 100644 (file)
@@ -301,10 +301,18 @@ public class FileCommonUtils {
     }
 
     List<Path> list = new ArrayList<>();
-    DirectoryStream<Path> ds = Files.newDirectoryStream(path);
-    for (Path p : ds) {
-      list.add(p);
+    DirectoryStream<Path> ds = null;
+    try {
+      ds = Files.newDirectoryStream(path);
+      for (Path p : ds) {
+        list.add(p);
+      }
+    } finally {
+      if (ds != null) {
+        ds.close();
+      }
     }
+
     return list;
   }
 
@@ -315,9 +323,16 @@ public class FileCommonUtils {
    */
   public static List<String> listFileName(Path path) throws IOException {
     List<String> list = new ArrayList<>();
-    DirectoryStream<Path> ds = Files.newDirectoryStream(path);
-    for (Path p : ds) {
-      list.add(p.getFileName().toString());
+    DirectoryStream<Path> ds = null;
+    try {
+      ds = Files.newDirectoryStream(path);
+      for (Path p : ds) {
+        list.add(p.getFileName().toString());
+      }
+    } finally {
+      if (ds != null) {
+        ds.close();
+      }
     }
 
     return list;