From cc517a0c7c56543426888a5d1f0620b9587e2642 Mon Sep 17 00:00:00 2001 From: ajay priyadarshi Date: Mon, 12 Mar 2018 17:15:29 +0530 Subject: sonar fix: R&E handling in UnCompressUtil try-with resource & logging issues fixed file name: UnCompressUtil.java Change-Id: I6e04cded3f9405b7a9e735063d9023cca8c775df Issue-ID: VFC-802 Signed-off-by: ajay priyadarshi --- .../jujuvnfmadapter/common/UnCompressUtil.java | 88 ++++++++-------------- 1 file changed, 31 insertions(+), 57 deletions(-) (limited to 'juju/juju-vnfmadapter/Juju-vnfmadapterService/service') diff --git a/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/UnCompressUtil.java b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/UnCompressUtil.java index f696be0..308adb2 100644 --- a/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/UnCompressUtil.java +++ b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/UnCompressUtil.java @@ -51,9 +51,9 @@ public class UnCompressUtil { private static Logger log = LoggerFactory.getLogger(UnCompressUtil.class); - public static Map archiveMap = new HashMap(); + private static Map archiveMap = new HashMap(); - public static Map compressorMap = new HashMap(); + private static Map compressorMap = new HashMap(); static { // archive type @@ -89,14 +89,11 @@ public class UnCompressUtil { * @since NFVO 0.5 */ public static boolean unCompressGzip(String zipfileName, String outputDirectory, List fileNames) { - FileInputStream fis = null; - ArchiveInputStream in = null; - BufferedInputStream bis = null; - try { - fis = new FileInputStream(zipfileName); + try ( + FileInputStream fis = new FileInputStream(zipfileName); GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(fis)); - in = new ArchiveStreamFactory().createArchiveInputStream("tar", gis); - bis = new BufferedInputStream(in); + ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("tar", gis); + BufferedInputStream bis = new BufferedInputStream(in)){ TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry(); while(entry != null) { String name = entry.getName(); @@ -108,35 +105,26 @@ public class UnCompressUtil { } if(name.endsWith("/")) { FileUtils.mkDirs(fileName); - } else { - File file = getRealFileName(outputDirectory, name); - if(null != fileNames) { - fileNames.add(file.getName()); - } - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); - int b = -1; - while((b = bis.read()) != -1) { - bos.write(b); - } - log.debug("ungzip to:" + file.getCanonicalPath()); - bos.flush(); - bos.close(); - } + } else { + File file = getRealFileName(outputDirectory, name); + if(null != fileNames) { + fileNames.add(file.getName()); + } + try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))){ + int b = -1; + while((b = bis.read()) != -1) { + bos.write(b); + } + log.debug("ungzip to:" + file.getCanonicalPath()); + } + } entry = (TarArchiveEntry)in.getNextEntry(); } return true; } catch(Exception e) { log.error("UnCompressGZip faield:", e); return false; - } finally { - try { - if(null != bis) { - bis.close(); - } - } catch(IOException e) { - log.error("UnCompressGZip faield:", e); - } - } + } } public static void unCompressAllZip(String sourcePath, String targetPath) throws FileNotFoundException { @@ -156,10 +144,9 @@ public class UnCompressUtil { * @since NFVO 0.5 */ public static boolean unCompressZip(String sourceFile, String targetPath,List fileNames) { - try { - BufferedOutputStream dest = null; + try ( FileInputStream fis = new FileInputStream(sourceFile); - ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); + ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))){ ZipEntry entry = null; while((entry = zis.getNextEntry()) != null) { String name = entry.getName(); @@ -176,14 +163,13 @@ public class UnCompressUtil { byte data[] = new byte[2048]; File file = getRealFileName(targetPath, name); fileNames.add(file.getName()); - dest = new BufferedOutputStream(new FileOutputStream(file)); + + try(BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(file))){ while((count = zis.read(data, 0, 2048)) != -1) { dest.write(data, 0, count); } - log.debug("unzip to:" + file.getCanonicalPath()); - dest.flush(); - dest.close(); + log.debug("unzip to:" + file.getCanonicalPath());} } } zis.close(); @@ -225,13 +211,11 @@ public class UnCompressUtil { * @since NFVO 0.5 */ public static boolean unCompressTarXZ(String zipfileName, String outputDirectory, List fileNames) { - ArchiveInputStream in = null; - BufferedInputStream bis = null; - try { + try( XZCompressorInputStream xzis = new XZCompressorInputStream(new BufferedInputStream(new FileInputStream(zipfileName))); - in = new ArchiveStreamFactory().createArchiveInputStream("tar", xzis); - bis = new BufferedInputStream(in); + ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("tar", xzis); + BufferedInputStream bis = new BufferedInputStream(in)){ TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry(); while(entry != null) { String name = entry.getName(); @@ -248,29 +232,19 @@ public class UnCompressUtil { if(null != fileNames) { fileNames.add(file.getName()); } - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); + try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))){ int b = -1; while((b = bis.read()) != -1) { bos.write(b); } - log.debug("ungzip to:" + file.getCanonicalPath()); - bos.flush(); - bos.close(); + log.debug("ungzip to:" + file.getCanonicalPath());} } entry = (TarArchiveEntry)in.getNextEntry(); } return true; } catch(Exception e) { log.error("unCompressTarXZ faield:", e); - } finally { - try { - if(null != bis) { - bis.close(); - } - } catch(IOException e) { - log.error("unCompressTarXZ faield:", e); - } - } + } return false; } -- cgit 1.2.3-korg