Author: jleroux
Date: Tue Jun  6 07:34:28 2017
New Revision: 1797742

URL: http://svn.apache.org/viewvc?rev=1797742&view=rev
Log:
Improved: Writer closed but not in case of exception
(OFBIZ-)

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.

Modified:
    
ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalServices.java
    
ofbiz/ofbiz-framework/trunk/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalWorker.java
    
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilIO.java
    
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilPlist.java

Modified: 
ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalServices.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalServices.java?rev=1797742&r1=1797741&r2=1797742&view=diff
==============================================================================
--- 
ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalServices.java
 (original)
+++ 
ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalServices.java
 Tue Jun  6 07:34:28 2017
@@ -275,10 +275,8 @@ public class PayPalServices {
                 Debug.logError(e, module);
             }
 
-            try {
-                Writer writer = response.getWriter();
+            try (Writer writer = response.getWriter()) {
                 writer.write(responseMsg);
-                writer.close();
             } catch (IOException e) {
                 Debug.logError(e, module);
             }

Modified: 
ofbiz/ofbiz-framework/trunk/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalWorker.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalWorker.java?rev=1797742&r1=1797741&r2=1797742&view=diff
==============================================================================
--- 
ofbiz/ofbiz-framework/trunk/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalWorker.java
 (original)
+++ 
ofbiz/ofbiz-framework/trunk/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalWorker.java
 Tue Jun  6 07:34:28 2017
@@ -208,11 +208,8 @@ public final class ICalWorker {
                     Debug.logVerbose("[handlePropFindRequest] PROPFIND 
response:\r\n" + UtilXml.writeXmlDocument(responseDocument), module);
                 }
                 ResponseHelper.prepareResponse(response, 207, "Multi-Status");
-                Writer writer = response.getWriter();
-                try {
+                try (Writer writer = response.getWriter()) {
                     helper.writeResponse(response, writer);
-                } finally {
-                    writer.close();
                 }
                 return;
             }
@@ -319,11 +316,8 @@ public final class ICalWorker {
         }
         if (responseProps.statusMessage != null) {
             response.setContentLength(responseProps.statusMessage.length());
-            Writer writer = response.getWriter();
-            try {
+            try (Writer writer = response.getWriter()) {
                 writer.write(responseProps.statusMessage);
-            } finally {
-                writer.close();
             }
         }
     }

Modified: 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilIO.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilIO.java?rev=1797742&r1=1797741&r2=1797742&view=diff
==============================================================================
--- 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilIO.java
 (original)
+++ 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilIO.java
 Tue Jun  6 07:34:28 2017
@@ -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() {

Modified: 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilPlist.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilPlist.java?rev=1797742&r1=1797741&r2=1797742&view=diff
==============================================================================
--- 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilPlist.java
 (original)
+++ 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilPlist.java
 Tue Jun  6 07:34:28 2017
@@ -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();
     }
-}
+}
\ No newline at end of file


Reply via email to