From 252b7cd8f1e6cbc947bf20cf2b033ccaa9904fdd Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Thu, 23 Aug 2018 14:59:28 +0900 Subject: Fix sonar issues Fix use try-with-resources issues Issue-ID: SDC-1672 Change-Id: I1c85377cbf18d3865af9b15fa512f845bea3cb6e Signed-off-by: Parshad Patel --- .../config/generation/GenerateEcompErrorsCsv.java | 16 +------ .../org/openecomp/sdc/common/util/ZipUtil.java | 53 ++++++---------------- 2 files changed, 14 insertions(+), 55 deletions(-) (limited to 'common-app-api') diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/config/generation/GenerateEcompErrorsCsv.java b/common-app-api/src/main/java/org/openecomp/sdc/common/config/generation/GenerateEcompErrorsCsv.java index 8ee8c6035d..f57695de99 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/config/generation/GenerateEcompErrorsCsv.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/config/generation/GenerateEcompErrorsCsv.java @@ -147,10 +147,7 @@ public class GenerateEcompErrorsCsv { String outputFile = targetFolder + "ecompErrorCodes" + dateFormatted + ".csv"; - FileWriter writer = null; - - try { - writer = new FileWriter(outputFile); + try(FileWriter writer = new FileWriter(outputFile)) { List errors = new ArrayList<>(); @@ -200,17 +197,6 @@ public class GenerateEcompErrorsCsv { } catch (Exception e) { log.info("generate Ecomp Errors Csv File failed - {}" , e); - - } finally { - if (writer != null) { - try { - writer.flush(); - writer.close(); - } catch (IOException e) { - log.info("close FileOutputStream failed - {}" , e); - } - - } } return result; diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/util/ZipUtil.java b/common-app-api/src/main/java/org/openecomp/sdc/common/util/ZipUtil.java index cac94a5820..2711a290cb 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/util/ZipUtil.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/util/ZipUtil.java @@ -64,8 +64,7 @@ public class ZipUtil { if (!ze.isDirectory()) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - try { + try(ByteArrayOutputStream os = new ByteArrayOutputStream()) { int len; while ((len = zis.read(buffer)) > 0) { os.write(buffer, 0, len); @@ -73,10 +72,6 @@ public class ZipUtil { fileNameToByteArray.put(fileName, os.toByteArray()); - } finally { - if (os != null) { - os.close(); - } } } ze = zis.getNextEntry(); @@ -87,7 +82,6 @@ public class ZipUtil { zis.close(); } catch (IOException ex) { - log.info("close Byte stream failed - {}" , ex); return null; } finally { @@ -96,7 +90,7 @@ public class ZipUtil { zis.closeEntry(); zis.close(); } catch (IOException e) { - // TODO: add log + log.info("Close ZipInputStream failed - {}" , e); } } @@ -127,50 +121,29 @@ public class ZipUtil { } public static byte[] zipBytes(byte[] input) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(baos); - ZipEntry entry = new ZipEntry("zip"); - entry.setSize(input.length); - zos.putNextEntry(entry); - zos.write(input); - zos.closeEntry(); - zos.close(); - return baos.toByteArray(); + try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ZipOutputStream zos = new ZipOutputStream(baos)){ + ZipEntry entry = new ZipEntry("zip"); + entry.setSize(input.length); + zos.putNextEntry(entry); + zos.write(input); + zos.closeEntry(); + return baos.toByteArray(); + } } public static byte[] unzip(byte[] zipped) { - ZipInputStream zipinputstream = null; - ByteArrayOutputStream outputStream = null; - try { + try(ZipInputStream zipinputstream = new ZipInputStream(new ByteArrayInputStream(zipped)); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { byte[] buf = new byte[1024]; - - zipinputstream = new ZipInputStream(new ByteArrayInputStream(zipped)); ZipEntry zipentry = zipinputstream.getNextEntry(); - outputStream = new ByteArrayOutputStream(); int n; while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { outputStream.write(buf, 0, n); } - return outputStream.toByteArray(); } catch (Exception e) { throw new IllegalStateException("Can't unzip input stream", e); - } finally { - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException e) { - log.debug("Failed to close output stream", e); - } - } - if (zipinputstream != null) { - try { - zipinputstream.closeEntry(); - zipinputstream.close(); - } catch (IOException e) { - log.debug("Failed to close zip input stream", e); - } - } } } -- cgit 1.2.3-korg