Improved: Writer closed but not in case of exception

(OFBIZ-9388)

This is an improvement only because no cases were reported. But obviously in 
case of unlucky exception after the Writer creation and before it's closed the 
Writer remains in memory.

The solution is to use try-with-ressources when possible, even when finally was
used (no problems then).

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1797742 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2017-06-06 07:34:28 +00:00
parent 3708ada432
commit 4e529f7055
4 changed files with 30 additions and 37 deletions
@@ -299,22 +299,23 @@ public final class UtilIO {
* @param out where to write the converted bytes to
* @param charset the charset to use to convert the raw bytes
* @param value the value to write
* @throws IOException
*/
public static void writeString(OutputStream out, Charset charset, String value) throws IOException {
Writer writer = new OutputStreamWriter(out, charset);
String nl = System.getProperty("line.separator");
int r = 0;
while (r < value.length()) {
int i = value.indexOf("\n", r);
if (i == -1) {
break;
try (Writer writer = new OutputStreamWriter(out, charset)) {
String nl = System.getProperty("line.separator");
int r = 0;
while (r < value.length()) {
int i = value.indexOf("\n", r);
if (i == -1) {
break;
}
writer.write(value.substring(r, i));
writer.write(nl);
r = i + 1;
}
writer.write(value.substring(r, i));
writer.write(nl);
r = i + 1;
writer.write(value.substring(r));
}
writer.write(value.substring(r));
writer.close();
}
public static Charset getUtf8() {
@@ -18,6 +18,9 @@
*/
package org.apache.ofbiz.base.util;
import static org.apache.ofbiz.base.util.UtilGenerics.checkList;
import static org.apache.ofbiz.base.util.UtilGenerics.checkMap;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
@@ -29,9 +32,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.apache.ofbiz.base.util.UtilGenerics.checkList;
import static org.apache.ofbiz.base.util.UtilGenerics.checkMap;
/**
* File Utilities
*
@@ -157,16 +157,16 @@ public final class UtilPlist {
* @throws UnsupportedEncodingException
*/
public static void writePlistFile(Map<String, Object> eoModelMap, String eomodeldFullPath, String filename, boolean useXml) throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter plistWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(eomodeldFullPath, filename)), "UTF-8")));
if (useXml) {
plistWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
plistWriter.println("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
plistWriter.println("<plist version=\"1.0\">");
writePlistPropertyMapXml(eoModelMap, 0, plistWriter);
plistWriter.println("</plist>");
} else {
writePlistPropertyMap(eoModelMap, 0, plistWriter, false);
try (PrintWriter plistWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(eomodeldFullPath, filename)), "UTF-8")))) {
if (useXml) {
plistWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
plistWriter.println("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
plistWriter.println("<plist version=\"1.0\">");
writePlistPropertyMapXml(eoModelMap, 0, plistWriter);
plistWriter.println("</plist>");
} else {
writePlistPropertyMap(eoModelMap, 0, plistWriter, false);
}
}
plistWriter.close();
}
}
}