Fixed the open streams which were not being closed
authorPriyanshuAgarwal <pagarwal@amdocs.com>
Thu, 2 Nov 2017 11:37:19 +0000 (17:07 +0530)
committerMichael Lando <ml636r@att.com>
Thu, 30 Nov 2017 07:58:06 +0000 (07:58 +0000)
Updated files in JTosca Library

Change-Id: Ib6b55ef4e367ee9a4f0761ae436bfcb9944f3a41
Issue-ID: SDC-249
Signed-off-by: priyanshu <pagarwal@amdocs.com>
src/main/java/org/openecomp/sdc/toscaparser/api/ImportsLoader.java
src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java
src/main/java/org/openecomp/sdc/toscaparser/api/prereq/CSAR.java

index 6794f9a..1fac3f1 100644 (file)
@@ -246,9 +246,8 @@ public class ImportsLoader {
         }
 
         if(UrlUtils.validateUrl(fileName)) {
-               try {
-                   al[0] = fileName;
-                       InputStream input = new URL(fileName).openStream();
+                       try (InputStream input = new URL(fileName).openStream();) {
+                               al[0] = fileName;
                                Yaml yaml = new Yaml();
                                al[1] = yaml.load(input);
                    return al;
@@ -354,9 +353,9 @@ public class ImportsLoader {
                al[0] = al[1] = null;
                return al;
             }
-            try {
+            try (InputStream input = new FileInputStream(new File(importTemplate));) {
                    al[0] = importTemplate;
-                               InputStream input = new FileInputStream(new File(importTemplate));
+
                                Yaml yaml = new Yaml();
                                al[1] = yaml.load(input);
                    return al;
@@ -417,9 +416,8 @@ public class ImportsLoader {
                }
         }
         if(UrlUtils.validateUrl(fullUrl)) {
-               try {
-                   al[0] = fullUrl;
-                       InputStream input = new URL(fullUrl).openStream();
+                       try (InputStream input = new URL(fullUrl).openStream();) {
+                               al[0] = fullUrl;
                                Yaml yaml = new Yaml();
                                al[1] = yaml.load(input);
                    return al;
index 4c6ba3a..d4506e1 100644 (file)
@@ -144,10 +144,9 @@ public class ToscaTemplate extends Object {
                        path = _getPath(_path);
                        // load the YAML template
                        if (path != null && !path.isEmpty()) {
-                               try {
+                               try (InputStream input = new FileInputStream(new File(path));){
                                        //System.out.println("Loading YAML file " + path);
                                        log.debug("ToscaTemplate Loading YAMEL file {}", path);
-                                       InputStream input = new FileInputStream(new File(path));
                                        Yaml yaml = new Yaml();
                                        Object data = yaml.load(input);
                                        this.tpl = (LinkedHashMap<String,Object>) data;
index ef29b53..b40eded 100644 (file)
@@ -273,8 +273,7 @@ public class CSAR {
        public LinkedHashMap<String,Object> getMainTemplateYaml() throws JToscaException {
        String mainTemplate = tempDir + File.separator + getMainTemplate();
        if(mainTemplate != null) {
-                       try {
-                       InputStream input = new FileInputStream(new File(mainTemplate));
+                       try (InputStream input = new FileInputStream(new File(mainTemplate));){
                                Yaml yaml = new Yaml();
                                Object data = yaml.load(input);
                        if(!(data instanceof LinkedHashMap)) {
@@ -459,34 +458,35 @@ public class CSAR {
         if (!destDir.exists()) {
             destDir.mkdir();
         }
-        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
-        ZipEntry entry = zipIn.getNextEntry();
-        // iterates over entries in the zip file
-        while (entry != null) {
-               // create all directories needed for nested items
-               String[] parts = entry.getName().split("/");
-               String s = destDirectory + File.separator ;
-               for(int i=0; i< parts.length-1; i++) {
-                       s += parts[i];
-                       File idir = new File(s);
-                       if(!idir.exists()) {
-                               idir.mkdir();
-                       }
-                       s += File.separator;
-               }
-            String filePath = destDirectory + File.separator + entry.getName();
-            if (!entry.isDirectory()) {
-                // if the entry is a file, extracts it
-                extractFile(zipIn, filePath);
-            } else {
-                // if the entry is a directory, make the directory
-                File dir = new File(filePath);
-                dir.mkdir();
-            }
-            zipIn.closeEntry();
-            entry = zipIn.getNextEntry();
-        }
-        zipIn.close();
+
+        try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));){
+                       ZipEntry entry = zipIn.getNextEntry();
+                       // iterates over entries in the zip file
+                       while (entry != null) {
+                               // create all directories needed for nested items
+                               String[] parts = entry.getName().split("/");
+                               String s = destDirectory + File.separator ;
+                               for(int i=0; i< parts.length-1; i++) {
+                                       s += parts[i];
+                                       File idir = new File(s);
+                                       if(!idir.exists()) {
+                                               idir.mkdir();
+                                       }
+                                       s += File.separator;
+                               }
+                               String filePath = destDirectory + File.separator + entry.getName();
+                               if (!entry.isDirectory()) {
+                                       // if the entry is a file, extracts it
+                                       extractFile(zipIn, filePath);
+                               } else {
+                                       // if the entry is a directory, make the directory
+                                       File dir = new File(filePath);
+                                       dir.mkdir();
+                               }
+                               zipIn.closeEntry();
+                               entry = zipIn.getNextEntry();
+                       }
+               }
     }
     
     /**
@@ -499,14 +499,14 @@ public class CSAR {
     
     private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
         //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
-       FileOutputStream fos = new FileOutputStream(filePath);
-       BufferedOutputStream bos = new BufferedOutputStream(fos);
-        byte[] bytesIn = new byte[BUFFER_SIZE];
-        int read = 0;
-        while ((read = zipIn.read(bytesIn)) != -1) {
-            bos.write(bytesIn, 0, read);
-        }
-        bos.close();
+               try (FileOutputStream fos = new FileOutputStream(filePath);
+                        BufferedOutputStream bos = new BufferedOutputStream(fos);){
+                       byte[] bytesIn = new byte[BUFFER_SIZE];
+                       int read = 0;
+                       while ((read = zipIn.read(bytesIn)) != -1) {
+                               bos.write(bytesIn, 0, read);
+                       }
+               }
     }
 
 }