aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjitendra007 <jitendra.sharma1@huawei.com>2020-07-14 18:17:37 +0530
committerjitendra007 <jitendra.sharma1@huawei.com>2020-08-06 11:37:06 +0530
commitd8a6f9355c2e557f3b7b8ebc5d6e2f9637a472c2 (patch)
tree372a6d80f745ffe0e2124a30d29b46373e363d30
parentfff1c111395bd9c2715cf4adf3ae676a9fa0f20e (diff)
"java.nio.Files#delete" should be preferred
Issue-ID: VNFSDK-608 Signed-off-by: jitendra007 <jitendra.sharma1@huawei.com> Change-Id: I9379950525bf9fc2a950614827e33059993af8a2
-rw-r--r--vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java406
-rw-r--r--vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java15
-rw-r--r--vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/filemanage/http/ToolUtil.java37
-rw-r--r--vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/filemanage/FileManageTest.java10
4 files changed, 258 insertions, 210 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java
index 7f5ad88e..5697ddf7 100644
--- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java
+++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java
@@ -26,216 +26,228 @@ import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import java.io.FileWriter;
import java.io.FileReader;
+import java.nio.file.Files;
+import java.nio.file.Paths;
public final class FileUtil {
- public static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
-
- private static final int BUFFER_SIZE = 2 * 1024 * 1024;
-
- private static final int MAX_PACKAGE_SIZE = 50 * 1024 * 1024;
- private static Gson gson = new Gson();
-
- private FileUtil() {
- //Empty constructor
- }
-
- /**
- * create dir.
- *
- * @param dir
- * dir to create
- * @return boolean
- */
- public static boolean createDirectory(String dir) {
- File folder = new File(dir);
- if (!folder.exists() && !folder.mkdirs()) {
- return false;
- } else {
- return true;
- }
- }
-
- /**
- * delete file.
- *
- * @param file
- * the file to delete
- * @return boolean
- */
- public static boolean deleteFile(File file) {
- String hintInfo = file.isDirectory() ? "dir " : "file ";
- boolean isFileDeleted = file.delete();
- boolean isFileExist = file.exists();
- if (!isFileExist) {
- if (isFileDeleted) {
- logger.info("delete {} {}" ,hintInfo, file.getAbsolutePath());
- } else {
- isFileDeleted = true;
- logger.info("file not exist. no need delete {} {}" ,hintInfo , file.getAbsolutePath());
- }
- } else {
- logger.info("fail to delete {} {} " , hintInfo , file.getAbsolutePath());
- }
- return isFileDeleted;
- }
-
- /**
- * unzip zip file.
- *
- * @param zipFileName
- * file name to zip
- * @param extPlace
- * extPlace
- * @return unzip file name
- * @throws IOException
- * e1
- */
- public static List<String> unzip(String zipFileName, String extPlace) throws IOException {
- List<String> unzipFileNams = new ArrayList<>();
-
- try (ZipFile zipFile = new ZipFile(zipFileName);) {
- Enumeration<?> fileEn = zipFile.entries();
- byte[] buffer = new byte[BUFFER_SIZE];
-
- while (fileEn.hasMoreElements()) {
- 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));) {
- int length = 0;
- while ((length = input.read(buffer)) != -1) {
- bos.write(buffer, 0, length);
- }
- unzipFileNams.add(file.getAbsolutePath());
- }
- }
- }
- return unzipFileNams;
- }
-
- public static boolean checkFileExists(String filePath) {
- File file = new File(filePath);
- return file.exists();
- }
-
- public static boolean deleteFile(String filePath) {
- File file = new File(filePath);
- return deleteFile(file);
- }
-
- public static boolean writeJsonDatatoFile(String fileAbsPath, Object obj) {
- logger.info("Write JsonData to file : {} " , fileAbsPath);
-
- boolean bResult = false;
- if (checkFileExists(fileAbsPath)) {
- deleteFile(fileAbsPath);
- }
-
- try(FileWriter writer = new FileWriter(new File(fileAbsPath))) {
- gson.toJson(obj, writer);
- bResult = true;
- } catch (Exception e) { //NOSONAR
- logger.info("Exception: writeJsonDatatoFile-->" + fileAbsPath, e);
- }
- return bResult;
- }
-
- public static <T> Object readJsonDatafFromFile(String fileAbsPath, Class<T> clazz) {
- if (!checkFileExists(fileAbsPath)) {
- logger.info("read JsonData from file , file not found : {}" ,fileAbsPath);
- return null;
- }
-
- logger.info("read JsonData from file : {}" , fileAbsPath);
-
- T obj = null;
- /*
+ public static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
+
+ private static final int BUFFER_SIZE = 2 * 1024 * 1024;
+
+ private static final int MAX_PACKAGE_SIZE = 50 * 1024 * 1024;
+ private static Gson gson = new Gson();
+
+ private FileUtil() {
+ //Empty constructor
+ }
+
+ /**
+ * create dir.
+ *
+ * @param dir
+ * dir to create
+ * @return boolean
+ */
+ public static boolean createDirectory(String dir) {
+ File folder = new File(dir);
+ if (!folder.exists() && !folder.mkdirs()) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * delete file.
+ *
+ * @param file
+ * the file to delete
+ * @return boolean
+ */
+ public static boolean deleteFile(File file) {
+ String hintInfo = file.isDirectory() ? "dir " : "file ";
+ String fileAbsPath = "";
+ boolean isFileDeleted=false;
+ try {
+ if (file.exists()){
+ Files.delete(Paths.get(file.getPath()));
+ fileAbsPath = file.getAbsolutePath();
+ logger.info("delete {} {}" ,hintInfo, fileAbsPath);
+ }
+ else{
+ logger.info("file not exist. no need delete {} {}" ,hintInfo , fileAbsPath);
+ }
+ isFileDeleted=true;
+
+ } catch (IOException e) {
+ logger.error("fail to delete {} {} ", hintInfo, fileAbsPath, e);
+ }
+ return isFileDeleted;
+ }
+
+ /**
+ * unzip zip file.
+ *
+ * @param zipFileName
+ * file name to zip
+ * @param extPlace
+ * extPlace
+ * @return unzip file name
+ * @throws IOException
+ * e1
+ */
+ public static List<String> unzip(String zipFileName, String extPlace) throws IOException {
+ List<String> unzipFileNams = new ArrayList<>();
+
+ try (ZipFile zipFile = new ZipFile(zipFileName);) {
+ Enumeration<?> fileEn = zipFile.entries();
+ byte[] buffer = new byte[BUFFER_SIZE];
+
+ while (fileEn.hasMoreElements()) {
+ 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));) {
+ int length = 0;
+ while ((length = input.read(buffer)) != -1) {
+ bos.write(buffer, 0, length);
+ }
+ unzipFileNams.add(file.getAbsolutePath());
+ }
+ }
+ }
+ return unzipFileNams;
+ }
+
+ public static boolean checkFileExists(String filePath) {
+ File file = new File(filePath);
+ return file.exists();
+ }
+
+ public static boolean deleteFile(String filePath) {
+ File file = new File(filePath);
+ return deleteFile(file);
+ }
+
+ public static boolean writeJsonDatatoFile(String fileAbsPath, Object obj) {
+ logger.info("Write JsonData to file : {} " , fileAbsPath);
+
+ boolean bResult = false;
+ if (checkFileExists(fileAbsPath)) {
+ deleteFile(fileAbsPath);
+ }
+
+ try(FileWriter writer = new FileWriter(new File(fileAbsPath))) {
+ gson.toJson(obj, writer);
+ bResult = true;
+ } catch (Exception e) { //NOSONAR
+ logger.info("Exception: writeJsonDatatoFile-->" + fileAbsPath, e);
+ }
+ return bResult;
+ }
+
+ public static <T> Object readJsonDatafFromFile(String fileAbsPath, Class<T> clazz) {
+ if (!checkFileExists(fileAbsPath)) {
+ logger.info("read JsonData from file , file not found : {}" ,fileAbsPath);
+ return null;
+ }
+
+ logger.info("read JsonData from file : {}" , fileAbsPath);
+
+ T obj = null;
+ /*
Gson will ignore the unknown fields and simply match the fields that it's able to.
ref: https://www.baeldung.com/gson-deserialization-guide
By default, Gson just ignores extra JSON elements that do not have matching Java fields.
ref: https://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html
*/
- try(JsonReader jsonReader = new JsonReader(new FileReader(fileAbsPath))) {
- obj = gson.fromJson(jsonReader, clazz);
- } catch (Exception e1) { //NOSONAR
- logger.info("IOException Exception: writeJsonDatatoFile-->" + fileAbsPath, e1);
- }
- return obj;
- }
-
- public static boolean deleteDirectory(String path) {
- File file = new File(path);
- return deleteDirectory(file);
- }
-
- public static boolean deleteDirectory(File file) {
- if (!file.exists()) {
- return true;
- }
- if (file.isDirectory()) {
- for (File f : file.listFiles()) {
- deleteDirectory(f);
- }
- }
- return file.delete();
- }
-
- public static boolean validateStream(FileInputStream ifs) {
-
- if (null == ifs) {
- logger.error("File stream is null");
- return false;
- }
-
- try {
- if (!ifs.getFD().valid()) {
- logger.error("File descriptor is not valid");
- return false;
- }
- } catch (IOException e) {
- logger.error("Exception while getting File descriptor", e);
- }
-
- return true;
- }
-
- public static boolean validatePath(String path) {
- if (!new File(path).isDirectory()) {
- logger.error("File is not a directory");
- return false;
- }
- return true;
- }
-
- public static boolean validateFile(File fileData) {
- if (null == fileData) {
- logger.error("File data is null");
- return false;
- }
-
- if (MAX_PACKAGE_SIZE < fileData.length()) {
- logger.error("File size is greater than 50 MB {}", fileData.length());
- return false;
- }
-
- return true;
- }
+ try(JsonReader jsonReader = new JsonReader(new FileReader(fileAbsPath))) {
+ obj = gson.fromJson(jsonReader, clazz);
+ } catch (Exception e1) { //NOSONAR
+ logger.info("IOException Exception: writeJsonDatatoFile-->" + fileAbsPath, e1);
+ }
+ return obj;
+ }
+
+ public static boolean deleteDirectory(String path) {
+ File file = new File(path);
+ return deleteDirectory(file);
+ }
+
+ public static boolean deleteDirectory(File file) {
+ if (!file.exists()) {
+ return true;
+ }
+ if (file.isDirectory()) {
+ for (File f : file.listFiles()) {
+ deleteDirectory(f);
+ }
+ }
+ boolean isFileDeleted=false;
+ String fileAbsPath = file.getAbsolutePath();
+ try {
+ Files.delete(Paths.get(file.getPath()));
+ isFileDeleted=true;
+ } catch (IOException e) {
+ logger.error("fail to delete {} {} ", fileAbsPath, e);
+ }
+ return isFileDeleted;
+ }
+
+ public static boolean validateStream(FileInputStream ifs) {
+
+ if (null == ifs) {
+ logger.error("File stream is null");
+ return false;
+ }
+
+ try {
+ if (!ifs.getFD().valid()) {
+ logger.error("File descriptor is not valid");
+ return false;
+ }
+ } catch (IOException e) {
+ logger.error("Exception while getting File descriptor", e);
+ }
+
+ return true;
+ }
+
+ public static boolean validatePath(String path) {
+ if (!new File(path).isDirectory()) {
+ logger.error("File is not a directory");
+ return false;
+ }
+ return true;
+ }
+
+ public static boolean validateFile(File fileData) {
+ if (null == fileData) {
+ logger.error("File data is null");
+ return false;
+ }
+
+ if (MAX_PACKAGE_SIZE < fileData.length()) {
+ logger.error("File size is greater than 50 MB {}", fileData.length());
+ return false;
+ }
+
+ return true;
+ }
}
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java
index 13a53c33..16be15c3 100644
--- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java
+++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java
@@ -24,12 +24,11 @@ import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.UUID;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import com.google.gson.Gson;
-
+import java.nio.file.Files;
+import java.nio.file.Paths;
/**
* common utility class.
@@ -125,7 +124,15 @@ public class ToolUtil {
}
File file = new File(tmpDir.getAbsolutePath() + File.separator + fileName);
if(file.exists()) {
- return file.delete();
+ boolean isFileDeleted=false;
+ String fileAbsPath = file.getAbsolutePath();
+ try {
+ Files.delete(Paths.get(file.getPath()));
+ isFileDeleted=true;
+ } catch (IOException e) {
+ LOG.error("fail to delete {} {} ", fileAbsPath, e);
+ }
+ return isFileDeleted;
}
return true;
}
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/filemanage/http/ToolUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/filemanage/http/ToolUtil.java
index c199a0e1..49d349d5 100644
--- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/filemanage/http/ToolUtil.java
+++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/filemanage/http/ToolUtil.java
@@ -21,11 +21,11 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-
import org.onap.vnfsdk.marketplace.common.HttpServerPathConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
+import java.nio.file.Files;
+import java.nio.file.Paths;
public class ToolUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ToolUtil.class);
@@ -74,8 +74,11 @@ public class ToolUtil {
{
File destDir = new File(destDirName);
if (destDir.exists() && overlay) {
- if (!new File(destDirName).delete()) {
- LOGGER.error("failed to delete file in createDestDir()");
+ String fileAbsPath = destDir.getAbsolutePath();
+ try {
+ Files.delete(Paths.get(destDirName));
+ } catch (IOException e) {
+ LOGGER.error("fail to delete {} {} " , fileAbsPath, e);
}
} else if (destDir.exists() && !overlay) {
return false;
@@ -156,8 +159,12 @@ public class ToolUtil {
File destFile = new File(destFileName);
if (destFile.exists() && overlay) {
- if(!new File(destFileName).delete())
- LOGGER.error("failed to delete file in copyFile()");
+ String fileAbsPath = destFile.getAbsolutePath();
+ try {
+ Files.delete(Paths.get(destFileName));
+ } catch (IOException e) {
+ LOGGER.error("fail to delete {} {} ", fileAbsPath, e);
+ }
} else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
return false;
}
@@ -177,8 +184,12 @@ public class ToolUtil {
}
File dir = new File(useDestDirName);
if (dir.exists()) {
- if(!dir.delete())
- LOGGER.error("failed to delete file in createDir()");
+ String fileAbsPath = dir.getAbsolutePath();
+ try {
+ Files.delete(Paths.get(useDestDirName));
+ } catch (IOException e) {
+ LOGGER.error("fail to delete {} {} ", fileAbsPath, e);
+ }
}
return dir.mkdirs();
@@ -203,7 +214,15 @@ public class ToolUtil {
}
}
}
- return dir.delete();
+ boolean isFileDeleted=false;
+ String fileAbsPath = dir.getAbsolutePath();
+ try {
+ Files.delete(Paths.get(dir.getPath()));
+ isFileDeleted=true;
+ } catch (IOException e) {
+ LOGGER.error("fail to delete {} {} ", fileAbsPath, e);
+ }
+ return isFileDeleted;
}
public static String getAppDeployPath()
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/filemanage/FileManageTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/filemanage/FileManageTest.java
index 116e549d..050a8d4a 100644
--- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/filemanage/FileManageTest.java
+++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/filemanage/FileManageTest.java
@@ -123,4 +123,14 @@ public class FileManageTest {
assertEquals(false,ManagerImpl.upload(srcPath, dstPath) );
}
+ @Test
+ public void testCreateDir(){
+ String dstPath = "./dstPathForTest1";
+ File dst = new File(dstPath);
+ dst.mkdir();
+ assertTrue(ToolUtil.createDir(dstPath));
+ if (dst.exists())
+ dst.delete();
+ }
+
}