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/trunk@1857099 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2019-04-08 09:17:55 +00:00
parent 8dfa0dedd9
commit f85a480f01
17 changed files with 36 additions and 100 deletions
@@ -283,40 +283,22 @@ public final class MiniLangUtil {
*/
public static void writeMiniLangDocument(URL xmlURL, Document document) {
URL styleSheetURL = null;
InputStream styleSheetInStream = null;
Transformer transformer = null;
try {
styleSheetURL = FlexibleLocation.resolveLocation("component://minilang/config/MiniLang.xslt");
styleSheetInStream = styleSheetURL.openStream();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
try (InputStream styleSheetInStream = styleSheetURL.openStream()) {
transformer = transformerFactory.newTransformer(new StreamSource(styleSheetInStream));
}
} catch (Exception e) {
Debug.logWarning(e, "Error reading minilang/config/MiniLang.xslt: ", module);
return;
} finally {
if (styleSheetInStream != null) {
try {
styleSheetInStream.close();
} catch (IOException e) {
Debug.logWarning(e, "Error closing minilang/config/MiniLang.xslt: ", module);
}
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(xmlURL.getFile());
try (FileOutputStream fos = new FileOutputStream(xmlURL.getFile())) {
UtilXml.transformDomDocument(transformer, document, fos);
Debug.logInfo("Saved Mini-language file " + xmlURL, module);
} catch (Exception e) {
Debug.logWarning(e, "Error writing mini-language file " + xmlURL + ": ", module);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
Debug.logWarning(e, "Error closing " + xmlURL + ": ", module);
}
}
}
}