summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorajay priyadarshi <ajay.priyadarshi@ril.com>2018-03-14 10:31:04 +0530
committerajay priyadarshi <ajay.priyadarshi@ril.com>2018-03-14 10:31:04 +0530
commitf59840b648effaddf5ccb716aedbbbc7d2225923 (patch)
treed2a1807022bb99b3fdcb838219abf5b19c9b4638
parent9c1b55281f7c214aae403a62e59e2daeb96aa6a2 (diff)
sonar fix: Rsrc handling nokia/CommonUtil
try-with resource issues fixed file name: CommonUtil.java Change-Id: Ibb5da925f50376a5d8c4101ea76ee1378f7cb9a9 Issue-ID: VFC-811 Signed-off-by: ajay priyadarshi <ajay.priyadarshi@ril.com>
-rw-r--r--nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/util/CommonUtil.java51
1 files changed, 19 insertions, 32 deletions
diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/util/CommonUtil.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/util/CommonUtil.java
index 47bb55f1..9ae3c940 100644
--- a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/util/CommonUtil.java
+++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/common/util/CommonUtil.java
@@ -38,13 +38,10 @@ public final class CommonUtil {
}
public static String getJsonStrFromFilePath(String fileName) throws IOException {
- InputStream ins = null;
- BufferedInputStream bins = null;
String fileContent = "";
- try {
- ins = new FileInputStream(fileName);
- bins = new BufferedInputStream(ins);
+ try (InputStream ins = new FileInputStream(fileName);
+ BufferedInputStream bins = new BufferedInputStream(ins)){
byte[] contentByte = new byte[ins.available()];
int num = bins.read(contentByte);
@@ -54,14 +51,7 @@ public final class CommonUtil {
}
} catch(FileNotFoundException e) {
logger.error(fileName + " is not found!", e);
- } finally {
- if(ins != null) {
- ins.close();
- }
- if(bins != null) {
- bins.close();
- }
- }
+ }
return fileContent;
}
@@ -87,25 +77,22 @@ public final class CommonUtil {
}
public static byte[] getBytes(String filePath){
- byte[] buffer = null;
- try {
- File file = new File(filePath);
- FileInputStream fis = new FileInputStream(file);
- ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
- byte[] b = new byte[1000];
- int n;
- while ((n = fis.read(b)) != -1) {
- bos.write(b, 0, n);
- }
- fis.close();
- bos.close();
- buffer = bos.toByteArray();
- } catch (FileNotFoundException e) {
- logger.error("file " + filePath + " is not found.", e);
- } catch (IOException e) {
- logger.error("file " + filePath + " IOException.", e);
- }
- return buffer;
+ byte[] buffer = null;
+ File file = new File(filePath);
+ try(FileInputStream fis = new FileInputStream(file);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream(1000)){
+ byte[] b = new byte[1000];
+ int n;
+ while ((n = fis.read(b)) != -1) {
+ bos.write(b, 0, n);
+ }
+ buffer = bos.toByteArray();
+ } catch (FileNotFoundException e) {
+ logger.error("file " + filePath + " is not found.", e);
+ } catch (IOException e) {
+ logger.error("file " + filePath + " IOException.", e);
+ }
+ return buffer;
}
}