Fixed the open streams which were not being closed
Updated files in JTosca Library
Change-Id: Ib6b55ef4e367ee9a4f0761ae436bfcb9944f3a41
Issue-ID: SDC-249
Signed-off-by: priyanshu <pagarwal@amdocs.com>
diff --git a/src/main/java/org/openecomp/sdc/toscaparser/api/ImportsLoader.java b/src/main/java/org/openecomp/sdc/toscaparser/api/ImportsLoader.java
index 6794f9a..1fac3f1 100644
--- a/src/main/java/org/openecomp/sdc/toscaparser/api/ImportsLoader.java
+++ b/src/main/java/org/openecomp/sdc/toscaparser/api/ImportsLoader.java
@@ -246,9 +246,8 @@
}
if(UrlUtils.validateUrl(fileName)) {
- try {
- al[0] = fileName;
- InputStream input = new URL(fileName).openStream();
+ try (InputStream input = new URL(fileName).openStream();) {
+ al[0] = fileName;
Yaml yaml = new Yaml();
al[1] = yaml.load(input);
return al;
@@ -354,9 +353,9 @@
al[0] = al[1] = null;
return al;
}
- try {
+ try (InputStream input = new FileInputStream(new File(importTemplate));) {
al[0] = importTemplate;
- InputStream input = new FileInputStream(new File(importTemplate));
+
Yaml yaml = new Yaml();
al[1] = yaml.load(input);
return al;
@@ -417,9 +416,8 @@
}
}
if(UrlUtils.validateUrl(fullUrl)) {
- try {
- al[0] = fullUrl;
- InputStream input = new URL(fullUrl).openStream();
+ try (InputStream input = new URL(fullUrl).openStream();) {
+ al[0] = fullUrl;
Yaml yaml = new Yaml();
al[1] = yaml.load(input);
return al;
diff --git a/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java b/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java
index 4c6ba3a..d4506e1 100644
--- a/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java
+++ b/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java
@@ -144,10 +144,9 @@
path = _getPath(_path);
// load the YAML template
if (path != null && !path.isEmpty()) {
- try {
+ try (InputStream input = new FileInputStream(new File(path));){
//System.out.println("Loading YAML file " + path);
log.debug("ToscaTemplate Loading YAMEL file {}", path);
- InputStream input = new FileInputStream(new File(path));
Yaml yaml = new Yaml();
Object data = yaml.load(input);
this.tpl = (LinkedHashMap<String,Object>) data;
diff --git a/src/main/java/org/openecomp/sdc/toscaparser/api/prereq/CSAR.java b/src/main/java/org/openecomp/sdc/toscaparser/api/prereq/CSAR.java
index ef29b53..b40eded 100644
--- a/src/main/java/org/openecomp/sdc/toscaparser/api/prereq/CSAR.java
+++ b/src/main/java/org/openecomp/sdc/toscaparser/api/prereq/CSAR.java
@@ -273,8 +273,7 @@
public LinkedHashMap<String,Object> getMainTemplateYaml() throws JToscaException {
String mainTemplate = tempDir + File.separator + getMainTemplate();
if(mainTemplate != null) {
- try {
- InputStream input = new FileInputStream(new File(mainTemplate));
+ try (InputStream input = new FileInputStream(new File(mainTemplate));){
Yaml yaml = new Yaml();
Object data = yaml.load(input);
if(!(data instanceof LinkedHashMap)) {
@@ -459,34 +458,35 @@
if (!destDir.exists()) {
destDir.mkdir();
}
- ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
- ZipEntry entry = zipIn.getNextEntry();
- // iterates over entries in the zip file
- while (entry != null) {
- // create all directories needed for nested items
- String[] parts = entry.getName().split("/");
- String s = destDirectory + File.separator ;
- for(int i=0; i< parts.length-1; i++) {
- s += parts[i];
- File idir = new File(s);
- if(!idir.exists()) {
- idir.mkdir();
- }
- s += File.separator;
- }
- String filePath = destDirectory + File.separator + entry.getName();
- if (!entry.isDirectory()) {
- // if the entry is a file, extracts it
- extractFile(zipIn, filePath);
- } else {
- // if the entry is a directory, make the directory
- File dir = new File(filePath);
- dir.mkdir();
- }
- zipIn.closeEntry();
- entry = zipIn.getNextEntry();
- }
- zipIn.close();
+
+ try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));){
+ ZipEntry entry = zipIn.getNextEntry();
+ // iterates over entries in the zip file
+ while (entry != null) {
+ // create all directories needed for nested items
+ String[] parts = entry.getName().split("/");
+ String s = destDirectory + File.separator ;
+ for(int i=0; i< parts.length-1; i++) {
+ s += parts[i];
+ File idir = new File(s);
+ if(!idir.exists()) {
+ idir.mkdir();
+ }
+ s += File.separator;
+ }
+ String filePath = destDirectory + File.separator + entry.getName();
+ if (!entry.isDirectory()) {
+ // if the entry is a file, extracts it
+ extractFile(zipIn, filePath);
+ } else {
+ // if the entry is a directory, make the directory
+ File dir = new File(filePath);
+ dir.mkdir();
+ }
+ zipIn.closeEntry();
+ entry = zipIn.getNextEntry();
+ }
+ }
}
/**
@@ -499,14 +499,14 @@
private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
- FileOutputStream fos = new FileOutputStream(filePath);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- byte[] bytesIn = new byte[BUFFER_SIZE];
- int read = 0;
- while ((read = zipIn.read(bytesIn)) != -1) {
- bos.write(bytesIn, 0, read);
- }
- bos.close();
+ try (FileOutputStream fos = new FileOutputStream(filePath);
+ BufferedOutputStream bos = new BufferedOutputStream(fos);){
+ byte[] bytesIn = new byte[BUFFER_SIZE];
+ int read = 0;
+ while ((read = zipIn.read(bytesIn)) != -1) {
+ bos.write(bytesIn, 0, read);
+ }
+ }
}
}