diff options
author | Kailun Qin <kailun.qin@intel.com> | 2018-09-13 21:44:09 +0800 |
---|---|---|
committer | Kailun Qin <kailun.qin@intel.com> | 2018-09-13 22:27:59 +0800 |
commit | 13852811184e5b4552e67023a66e8bbfe34fb373 (patch) | |
tree | 8aab7d6426ee5faff79ca1e79f3938573b052ea5 /vnf-sdk-function-test/src/main | |
parent | 062018330db6955dd215c3696f17e2736ea0316a (diff) |
Sonar: code clean up for functest
Use "try-with-resources" statement instead of "try".
Change-Id: I0e9f9551a62fcbdb8d04cbd371c01d6814446c77
Issue-ID: VNFSDK-320
Signed-off-by: Kailun Qin <kailun.qin@intel.com>
Diffstat (limited to 'vnf-sdk-function-test/src/main')
-rw-r--r-- | vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java | 86 | ||||
-rw-r--r-- | vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java | 9 |
2 files changed, 20 insertions, 75 deletions
diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java index 2b13022..d10a7b9 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java @@ -91,30 +91,25 @@ public final class FileUtil { * @throws IOException e1 */ public static List<String> unzip(String zipFileName, String extPlace) throws IOException { - ZipFile zipFile = null; + ArrayList<String> unzipFileNams = new ArrayList<String>(); - try { - zipFile = new ZipFile(zipFileName); + try (ZipFile zipFile = new ZipFile(zipFileName)) { Enumeration<?> fileEn = zipFile.entries(); byte[] buffer = new byte[BUFFER_SIZE]; while (fileEn.hasMoreElements()) { - InputStream input = null; - BufferedOutputStream bos = null; - try { - ZipEntry entry = (ZipEntry) fileEn.nextElement(); - if (entry.isDirectory()) { - continue; - } - - input = zipFile.getInputStream(entry); - File file = new File(extPlace, entry.getName()); - if (!file.getParentFile().exists()) { - createDirectory(file.getParentFile().getAbsolutePath()); - } + ZipEntry entry = (ZipEntry) fileEn.nextElement(); + if (entry.isDirectory()) { + continue; + } + File file = new File(extPlace, entry.getName()); + if (!file.getParentFile().exists()) { + createDirectory(file.getParentFile().getAbsolutePath()); + } + try (InputStream input = zipFile.getInputStream(entry); + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) { - bos = new BufferedOutputStream(new FileOutputStream(file)); while (true) { int length = input.read(buffer); if (length == -1) { @@ -123,13 +118,14 @@ public final class FileUtil { bos.write(buffer, 0, length); } unzipFileNams.add(file.getAbsolutePath()); - } finally { - closeOutputStream(bos); - closeInputStream(input); + } catch (Exception ex) { + LOG.error("close InputStream error!: " + ex); + LOG.error("close OutputStream error!: " + ex); } } - } finally { - closeZipFile(zipFile); + } catch (Exception ex) { + LOG.error("close ZipFile error!: " + ex); + throw new IOException(ex); } return unzipFileNams; } @@ -144,52 +140,6 @@ public final class FileUtil { }); } - /** - * close InputStream. - * - * @param inputStream the inputstream to close - */ - private static void closeInputStream(InputStream inputStream) { - try { - if (inputStream != null) { - inputStream.close(); - } - } catch (Exception ex) { - LOG.error("close InputStream error!: " + ex); - } - } - - /** - * close OutputStream. - * - * @param outputStream the output stream to close - */ - private static void closeOutputStream(OutputStream outputStream) { - try { - if (outputStream != null) { - outputStream.close(); - } - } catch (Exception ex) { - LOG.error("close OutputStream error!: " + ex); - } - } - - /** - * close zipFile. - * - * @param zipFile the zipFile to close - */ - private static void closeZipFile(ZipFile zipFile) { - try { - ZipFile tempZipFile = zipFile; - if (tempZipFile != null) { - tempZipFile.close(); - } - } catch (IOException ioe) { - LOG.error("close ZipFile error!: " + ioe); - } - } - public static Boolean checkFileExist(String filePath) { File file = new File(filePath); return file.exists(); diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java index c46f7b5..8553110 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java @@ -76,20 +76,15 @@ public class ScriptManager { actualFile = st.nextToken(); } File file = new File(tmpDir + File.separator + actualFile); - OutputStream os = null; - try { + try (OutputStream os = new FileOutputStream(file, true)) { int read = 0; byte[] bytes = new byte[1024]; - os = new FileOutputStream(file, true); + while ((read = uploadedInputStream.read(bytes)) != -1) { os.write(bytes, 0, read); } os.flush(); return file.getAbsolutePath(); - } finally { - if (os != null) { - os.close(); - } } } |