FIX MAJOR SONAR ISSUES IN SVNFM 17/57017/1
authorKrishnajinka <kris.jinka@samsung.com>
Fri, 20 Jul 2018 07:54:19 +0000 (16:54 +0900)
committerKrishnajinka <kris.jinka@samsung.com>
Fri, 20 Jul 2018 07:55:35 +0000 (16:55 +0900)
optimise the use of try blocks and also use
the try with resources blocks for efficiency

Issue-ID: VFC-967
Change-Id: I1b2a22f1341a76879b1573c2d4caf71e7a75c34a
Signed-off-by: Krishnajinka <kris.jinka@samsung.com>
huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/DownloadCsarManager.java
huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestHttpContentExchange.java
huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestfulConfigure.java
huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/vnf/ScaleManager.java

index b09c50d..4474c39 100644 (file)
@@ -172,10 +172,9 @@ public class DownloadCsarManager {
      */
     public static int unzipCSAR(String fileName, String filePath) {
         final int BUFFER = 2048;
-        int status = 0;
-        ZipFile zipFile = null;
-        try {
-            zipFile = new ZipFile(fileName);
+        int status = Constant.UNZIP_SUCCESS;
+
+        try(ZipFile zipFile = new ZipFile(fileName);) {
             Enumeration emu = zipFile.entries();
             while(emu.hasMoreElements()) {
                 ZipEntry entry = (ZipEntry)emu.nextElement();
@@ -192,33 +191,19 @@ public class DownloadCsarManager {
                 if(parent != null && (!parent.exists())) {
                     parent.mkdirs();
                 }
-                try(FileOutputStream fos = new FileOutputStream(file)){
-                    try(BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER)){
-
+                try(FileOutputStream fos = new FileOutputStream(file);
+                    BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);){
                     int count;
                     byte data[] = new byte[BUFFER];
                     while((count = bis.read(data, 0, BUFFER)) != -1) {
                         bos.write(data, 0, count);
                     }
                     bos.flush();
-                    }
                 }
             }
-
-            status = Constant.UNZIP_SUCCESS;
-
         } catch(Exception e) {
             status = Constant.UNZIP_FAIL;
             LOG.error("Exception: " + e);
-        } finally {
-            if(zipFile != null) {
-                try {
-                    zipFile.close();
-                } catch(IOException e) {
-                    LOG.error("IOException: " + e);
-                    ;
-                }
-            }
         }
         return status;
     }
index 26d2ad5..64017c7 100644 (file)
@@ -81,40 +81,14 @@ public class RestHttpContentExchange extends ContentExchange {
         if(data == null) {
             return "";
         }
-        ByteArrayInputStream input = null;
-        GZIPInputStream gzis = null;
-        InputStreamReader reader = null;
         final StringBuilder out = new StringBuilder();
-        try {
-            input = new ByteArrayInputStream(data);
-            gzis = new GZIPInputStream(input);
-            reader = new InputStreamReader(gzis, Charset.forName(RestfulClientConst.ENCODING));
+        try (ByteArrayInputStream input = new ByteArrayInputStream(data);
+             GZIPInputStream gzis = new GZIPInputStream(input);
+             InputStreamReader reader = new InputStreamReader(gzis, Charset.forName(RestfulClientConst.ENCODING));) {
             final char[] buff = new char[1024];
             for(int n; (n = reader.read(buff)) != -1;) {
                 out.append(new String(buff, 0, n));
             }
-        } finally {
-            if(reader != null) {
-                try {
-                    reader.close();
-                } catch(final IOException e) {
-                    LOGGER.error("decompress Gzip reader exception:", e);
-                }
-            }
-            if(gzis != null) {
-                try {
-                    gzis.close();
-                } catch(final IOException e) {
-                    LOGGER.error("decompress Gzip exception:", e);
-                }
-            }
-            if(input != null) {
-                try {
-                    input.close();
-                } catch(final IOException e) {
-                    LOGGER.error("decompress Gzip input exception:", e);
-                }
-            }
         }
         return out.toString();
 
index 8b71941..83bf9b5 100644 (file)
@@ -134,11 +134,10 @@ public class RestfulConfigure {
             LOG.error(filePath + "isn't exist.");
             return null;
         }
-        BufferedReader reader = null;
+
         final StringBuilder jsonstr = new StringBuilder();
         JSONObject jo = null;
-        try {
-            reader = new BufferedReader(new FileReader(file));
+        try (BufferedReader reader = new BufferedReader(new FileReader(file));) {
             final ReaderHelper rHelpper = new ReaderHelper(reader);
             String tempString = null;
             while((tempString = rHelpper.getLine()) != null) {
@@ -147,14 +146,6 @@ public class RestfulConfigure {
             jo = JSONObject.fromObject(jsonstr.toString());
         } catch(final IOException e) {
             LOG.error("load file exception:" + e);
-        } finally {
-            if(reader != null) {
-                try {
-                    reader.close();
-                } catch(final IOException e) {
-                    LOG.error("close error.", e);
-                }
-            }
         }
         return jo;
     }
index 7b0a4db..e92da07 100644 (file)
@@ -99,7 +99,6 @@ public abstract class ScaleManager {
 
     private static void writeVmIdsToFile(String vnfId, JSONArray vms) {
         String filePath = getVmIdsFilePath(vnfId);
-        FileOutputStream outStream = null;
         try {
             File destFile = FileUtils.getFile(filePath);
 
@@ -112,28 +111,23 @@ public abstract class ScaleManager {
             if(!destFile.exists()) {
                 destFile.createNewFile();
             }
-            outStream = new FileOutputStream(destFile);
-            outStream.write(vms.toString().getBytes());
+            try(FileOutputStream outStream = new FileOutputStream(destFile)) {
+                outStream.write(vms.toString().getBytes());
+            }
         } catch(IOException e) {
             LOG.error("function=writeVmIdsToFile, msg=write vms to file ioexception, e : {}", e);
-        } finally {
-            IOUtils.closeQuietly(outStream);
         }
     }
 
     private static JSONArray readVmIdsFile(String vnfId) {
         String filePath = getVmIdsFilePath(vnfId);
-        InputStream ins = null;
-        BufferedInputStream bins = null;
-        String fileContent = "";
-        try {
-            ins = FileUtils.openInputStream(FileUtils.getFile(filePath));
-            bins = new BufferedInputStream(ins);
 
+        String fileContent = "";
+        try(InputStream ins = FileUtils.openInputStream(FileUtils.getFile(filePath));
+            BufferedInputStream bins = new BufferedInputStream(ins);) {
             byte[] contentByte = new byte[ins.available()];
             int num = bins.read(contentByte);
 
-
             if(num > 0) {
                 fileContent = new String(contentByte);
             }
@@ -145,9 +139,6 @@ public abstract class ScaleManager {
             LOG.error("function=readVmIdsFile, msg=read vms from file IOException, filePath : {}", filePath);
         } catch(JSONException e) {
             LOG.error("function=readVmIdsFile, msg=read vms from file JSONException, fileContent : {}", fileContent);
-        } finally {
-            IOUtils.closeQuietly(bins);
-            IOUtils.closeQuietly(ins);
         }
         return new JSONArray();
     }