"Applied fix from trunk for revision: 1857099"

------------------------------------------------------------------------
r1857099 | jleroux | 2019-04-08 11:17:55 +0200 (lun. 08 avr. 2019) | 12 lignes

Improved: Using try-with-resources with File IO Objects.
(OFBIZ-9855)

Uses try with resources for File IO objects like ObjectInputStream and other 
objects. File classes have the AutoCloseable interface.

jleroux: this is only a part of the 2nd patch. The 1st patch will be committed
soon too...

All tests pass

Thanks: Pradhan Yash Sharma
------------------------------------------------------------------------


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/branches/release17.12@1857487 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2019-04-13 17:50:42 +00:00
parent bf73437f27
commit 69a46f08f4
17 changed files with 36 additions and 100 deletions
@@ -1187,18 +1187,12 @@ public final class UtilProperties implements Serializable {
super(defaults);
}
public ExtendedProperties(URL url, Locale locale) throws IOException, InvalidPropertiesFormatException {
InputStream in = null;
try {
in = new BufferedInputStream(url.openStream());
try (InputStream in = new BufferedInputStream(url.openStream())) {
if (url.getFile().endsWith(".xml")) {
xmlToProperties(in, locale, this);
} else {
load(in);
}
} finally {
if (in != null) {
in.close();
}
}
}
@Override
@@ -25,6 +25,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;
import java.text.DateFormat;
@@ -82,10 +82,11 @@ public final class XslTransform {
// compile the xsl template
Transformer transformer = tfactory.newTransformer(new StreamSource(template));
// and apply the xsl template to the source document and save in a result string
StringWriter sw = new StringWriter();
try (StringWriter sw = new StringWriter()) {
StreamResult sr = new StreamResult(sw);
transformer.transform(source, sr);
result = sw.toString();
} catch (IOException e) {}
} else {
Debug.logError("tfactory does not support SAX features!", module);
}
@@ -156,8 +157,9 @@ public final class XslTransform {
} else if (UtilValidate.isNotEmpty(inputUrl)) {
URL url = FlexibleLocation.resolveLocation(inputUrl);
URLConnection conn = URLConnector.openConnection(url);
InputStream in = conn.getInputStream();
try (InputStream in = conn.getInputStream()) {
source = new StreamSource(in);
}
}
return source;
}
@@ -75,7 +75,8 @@ public class UtilPropertiesTests extends GenericTestCaseBase {
"\">Key Value</value>\n" +
" </property>\n" +
"</resource>";
InputStream in = new ByteArrayInputStream(new String(xmlData.getBytes(), Charset.forName("UTF-8")).getBytes());
try (InputStream in = new ByteArrayInputStream(new String(xmlData.getBytes(), Charset.forName("UTF-8")).getBytes())) {
return UtilProperties.xmlToProperties(in, locale, null);
}
}
}