Author: jleroux
Date: Sat Apr 13 17:51:39 2019 New Revision: 1857488 URL: http://svn.apache.org/viewvc?rev=1857488&view=rev Log: "Applied fix from trunk framework 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 ------------------------------------------------------------------------ Modified: ofbiz/branches/release16.11/ (props changed) ofbiz/branches/release16.11/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java ofbiz/branches/release16.11/applications/party/src/main/java/org/apache/ofbiz/party/communication/CommunicationEventServices.java ofbiz/branches/release16.11/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalConverter.java ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/template/XslTransform.java ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/test/UtilPropertiesTests.java ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/FtpServices.java ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java ofbiz/branches/release16.11/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntitySaxReader.java ofbiz/branches/release16.11/framework/minilang/src/main/java/org/apache/ofbiz/minilang/MiniLangUtil.java ofbiz/branches/release16.11/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppUtil.java ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/SOAPEventHandler.java ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/view/ApacheFopWorker.java ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/ResponseHelper.java ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/WebDavUtil.java Propchange: ofbiz/branches/release16.11/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Sat Apr 13 17:51:39 2019 @@ -10,5 +10,5 @@ /ofbiz/branches/json-integration-refactoring:1634077-1635900 /ofbiz/branches/multitenant20100310:921280-927264 /ofbiz/branches/release13.07:1547657 -/ofbiz/ofbiz-framework/trunk:1783202,1783388,1784549,1784558,1784708,1785882,1785925,1786079,1786214,1786525,1787047,1787133,1787176,1787535,1787906-1787911,1787949,1789665,1789863,1789874,1790396,1790810,1791277,1791288,1791342,1791346,1791490,1791496,1791625,1791634,1791791,1791804,1792270,1792272,1792275,1792432,1792609,1792638,1793300,1794008,1794132,1796047,1796262,1797733,1798668,1798682,1798796,1798803,1798808,1799088,1799183,1799327,1799417,1799687,1799767,1799793,1799859,1800250,1800780,1800832,1800853,1801094,1801262-1801263,1801273-1801274,1801303,1801316,1801318-1801319,1801336,1801340,1801346,1801349-1801350,1801359,1801742,1802657,1802766,1803525,1804656,1804843,1804847,1804859,1805143,1805558,1805880,1806036,1806220,1806266,1806269,1806951,1807597,1807890,1808834,1809399,1809429,1809594,1809741,1810102,1811794,1812387,1813600,1813617,1813647,1813833,1814277,1814319,1814349,1814392,1814501,1814591,1814642,1814644,1814709,1814873,1814928,1814934,1815059,1816264,1816273, 1816289,1816291,1816297,1816369,1816373,1816461,1816635,1816795,1818101,1818269,1818273,1818402,1819122,1819136,1819144,1819811,1820823,1820949,1820966,1821012,1821036,1821613,1821965,1822310,1822377,1822383,1823876,1824314,1824316,1824732,1824803,1824847,1824855,1825192,1825233,1825262,1825444,1825450,1826374,1826592,1826671,1826805,1826938,1828255,1830936,1831234,1831608,1831831,1832577,1832662,1832756,1832944,1833211,1834181,1834191,1835235,1836144,1838032,1840189,1840199,1840828,1841657,1841662,1842372,1842921,1843225,1843893,1845418,1845420,1845466,1845544,1845552,1846214,1846594,1846632,1847398,1848263,1848336,1848398,1848444,1848449,1849191,1849193,1849275,1849528,1849540,1849567,1849693,1850015,1850023,1850530,1850685,1850914,1850918,1850948,1851200,1851247,1851319,1851805,1851998,1852587,1852818,1853070,1853691,1853745,1853750,1854306,1854457,1855078,1855287,1855371,1855403,1855488,1855492,1855497,1855501,1855898,1856405,1856455,1856459-1856460,1856484,1856598,1856617,18566 67,1857180,1857213,1857392 +/ofbiz/ofbiz-framework/trunk:1783202,1783388,1784549,1784558,1784708,1785882,1785925,1786079,1786214,1786525,1787047,1787133,1787176,1787535,1787906-1787911,1787949,1789665,1789863,1789874,1790396,1790810,1791277,1791288,1791342,1791346,1791490,1791496,1791625,1791634,1791791,1791804,1792270,1792272,1792275,1792432,1792609,1792638,1793300,1794008,1794132,1796047,1796262,1797733,1798668,1798682,1798796,1798803,1798808,1799088,1799183,1799327,1799417,1799687,1799767,1799793,1799859,1800250,1800780,1800832,1800853,1801094,1801262-1801263,1801273-1801274,1801303,1801316,1801318-1801319,1801336,1801340,1801346,1801349-1801350,1801359,1801742,1802657,1802766,1803525,1804656,1804843,1804847,1804859,1805143,1805558,1805880,1806036,1806220,1806266,1806269,1806951,1807597,1807890,1808834,1809399,1809429,1809594,1809741,1810102,1811794,1812387,1813600,1813617,1813647,1813833,1814277,1814319,1814349,1814392,1814501,1814591,1814642,1814644,1814709,1814873,1814928,1814934,1815059,1816264,1816273, 1816289,1816291,1816297,1816369,1816373,1816461,1816635,1816795,1818101,1818269,1818273,1818402,1819122,1819136,1819144,1819811,1820823,1820949,1820966,1821012,1821036,1821613,1821965,1822310,1822377,1822383,1823876,1824314,1824316,1824732,1824803,1824847,1824855,1825192,1825233,1825262,1825444,1825450,1826374,1826592,1826671,1826805,1826938,1828255,1830936,1831234,1831608,1831831,1832577,1832662,1832756,1832944,1833211,1834181,1834191,1835235,1836144,1838032,1840189,1840199,1840828,1841657,1841662,1842372,1842921,1843225,1843893,1845418,1845420,1845466,1845544,1845552,1846214,1846594,1846632,1847398,1848263,1848336,1848398,1848444,1848449,1849191,1849193,1849275,1849528,1849540,1849567,1849693,1850015,1850023,1850530,1850685,1850914,1850918,1850948,1851200,1851247,1851319,1851805,1851998,1852587,1852818,1853070,1853691,1853745,1853750,1854306,1854457,1855078,1855287,1855371,1855403,1855488,1855492,1855497,1855501,1855898,1856405,1856455,1856459-1856460,1856484,1856598,1856617,18566 67,1857099,1857180,1857213,1857392 /ofbiz/trunk:1770481,1770490,1770540,1771440,1771448,1771516,1771935,1772346,1772880,1774772,1775441,1779724,1780659,1781109,1781125,1781979,1782498,1782520 Modified: ofbiz/branches/release16.11/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java (original) +++ ofbiz/branches/release16.11/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java Sat Apr 13 17:51:39 2019 @@ -588,9 +588,10 @@ public class DataResourceWorker impleme public static String renderDataResourceAsText(Delegator delegator, String dataResourceId, Map<String, Object> templateContext, Locale locale, String targetMimeTypeId, boolean cache) throws GeneralException, IOException { - Writer writer = new StringWriter(); + try (Writer writer = new StringWriter()) { renderDataResourceAsText(delegator, dataResourceId, writer, templateContext, locale, targetMimeTypeId, cache, null); return writer.toString(); + } } public static String renderDataResourceAsText(Delegator delegator, String dataResourceId, Appendable out, @@ -828,14 +829,13 @@ public class DataResourceWorker impleme URL url = FlexibleLocation.resolveLocation(dataResource.getString("objectInfo")); if (url.getHost() != null) { // is absolute - InputStream in = url.openStream(); int c; - StringWriter sw = new StringWriter(); + try (InputStream in = url.openStream(); StringWriter sw = new StringWriter()) { while ((c = in.read()) != -1) { sw.write(c); } - sw.close(); text = sw.toString(); + } } else { String prefix = DataResourceWorker.buildRequestPrefix(delegator, locale, webSiteId, https); String sep = ""; Modified: ofbiz/branches/release16.11/applications/party/src/main/java/org/apache/ofbiz/party/communication/CommunicationEventServices.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/applications/party/src/main/java/org/apache/ofbiz/party/communication/CommunicationEventServices.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/applications/party/src/main/java/org/apache/ofbiz/party/communication/CommunicationEventServices.java (original) +++ ofbiz/branches/release16.11/applications/party/src/main/java/org/apache/ofbiz/party/communication/CommunicationEventServices.java Sat Apr 13 17:51:39 2019 @@ -1369,8 +1369,9 @@ public class CommunicationEventServices URL imageUrl; try { imageUrl = FlexibleLocation.resolveLocation("component://images/webapp/images/spacer.gif"); - InputStream imageStream = imageUrl.openStream(); + try (InputStream imageStream = imageUrl.openStream()) { UtilHttp.streamContentToBrowser(response, imageStream, 43, "image/gif", null); + } } catch (MalformedURLException e) { Debug.logError(e, module); } catch (IOException e) { Modified: ofbiz/branches/release16.11/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalConverter.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalConverter.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalConverter.java (original) +++ ofbiz/branches/release16.11/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalConverter.java Sat Apr 13 17:51:39 2019 @@ -580,9 +580,8 @@ public class ICalConverter { calendar = new Calendar(); } else { Debug.logVerbose("iCalendar Data found, using saved Calendar", module); - StringReader reader = new StringReader(iCalData); + try (StringReader reader = new StringReader(iCalData)) { CalendarBuilder builder = new CalendarBuilder(); - try { calendar = builder.build(reader); newCalendar = false; } catch (Exception e) { Modified: ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java (original) +++ ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java Sat Apr 13 17:51:39 2019 @@ -1153,18 +1153,12 @@ public final class UtilProperties implem 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 Modified: ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java (original) +++ ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java Sat Apr 13 17:51:39 2019 @@ -24,6 +24,7 @@ import java.io.File; import java.io.FileOutputStream; 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; Modified: ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/template/XslTransform.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/template/XslTransform.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/template/XslTransform.java (original) +++ ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/template/XslTransform.java Sat Apr 13 17:51:39 2019 @@ -84,10 +84,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); } @@ -158,8 +159,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; } Modified: ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/test/UtilPropertiesTests.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/test/UtilPropertiesTests.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/test/UtilPropertiesTests.java (original) +++ ofbiz/branches/release16.11/framework/base/src/main/java/org/apache/ofbiz/base/util/test/UtilPropertiesTests.java Sat Apr 13 17:51:39 2019 @@ -75,7 +75,8 @@ public class UtilPropertiesTests extends "\">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); + } } } Modified: ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/FtpServices.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/FtpServices.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/FtpServices.java (original) +++ ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/FtpServices.java Sat Apr 13 17:51:39 2019 @@ -120,11 +120,6 @@ public class FtpServices { } catch (Exception e) { Debug.logWarning(e, "[putFile] Problem with FTP disconnect: ", module); } - try { - localFile.close(); - } catch (Exception e) { - Debug.logWarning(e, "[putFile] Problem closing local file: ", module); - } } if (errorList.size() > 0) { Debug.logError("[putFile] The following error(s) (" + errorList.size() + ") occurred: " + errorList, module); @@ -188,11 +183,6 @@ public class FtpServices { } catch (Exception e) { Debug.logWarning(e, "[getFile] Problem with FTP disconnect: ", module); } - try { - localFile.close(); - } catch (Exception e) { - Debug.logWarning(e, "[getFile] Problem closing local file: ", module); - } } if (errorList.size() > 0) { Debug.logError("[getFile] The following error(s) (" + errorList.size() + ") occurred: " + errorList, module); Modified: ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java (original) +++ ofbiz/branches/release16.11/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java Sat Apr 13 17:51:39 2019 @@ -517,8 +517,7 @@ public class EmailServices { } isMultiPart = true; // start processing fo pdf attachment - try { - Writer writer = new StringWriter(); + try (Writer writer = new StringWriter(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { MapStack<String> screenContextAtt = MapStack.create(); // substitute the freemarker variables... ScreenStringRenderer foScreenStringRenderer = new MacroScreenRenderer(EntityUtilProperties.getPropertyValue("widget", "screenfop.name", dctx.getDelegator()), @@ -532,15 +531,10 @@ public class EmailServices { StreamSource src = new StreamSource(new StringReader(writer.toString())); // create the output stream for the generation - ByteArrayOutputStream baos = new ByteArrayOutputStream(); Fop fop = ApacheFopWorker.createFopInstance(baos, MimeConstants.MIME_PDF); ApacheFopWorker.transform(src, null, fop); - // and generate the PDF - baos.flush(); - baos.close(); - // store in the list of maps for sendmail.... bodyParts.add(UtilMisc.<String, Object> toMap("content", baos.toByteArray(), "type", "application/pdf", "filename", attachmentName)); Modified: ofbiz/branches/release16.11/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntitySaxReader.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntitySaxReader.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntitySaxReader.java (original) +++ ofbiz/branches/release16.11/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntitySaxReader.java Sat Apr 13 17:51:39 2019 @@ -181,9 +181,10 @@ public class EntitySaxReader extends Def Debug.logWarning("content was null, doing nothing", module); return 0; } - ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes("UTF-8")); + try (ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes("UTF-8"))) { return this.parse(bis, "Internal Content"); + } } public long parse(URL location) throws SAXException, java.io.IOException { @@ -192,17 +193,9 @@ public class EntitySaxReader extends Def return 0; } Debug.logImportant("Beginning import from URL: " + location.toExternalForm(), module); - InputStream is = null; long numberRead = 0; - try { - is = location.openStream(); + try (InputStream is = location.openStream()) { numberRead = this.parse(is, location.toString()); - } finally { - if (is != null) { - try { - is.close(); - } catch(Exception e) {} - } } return numberRead; } Modified: ofbiz/branches/release16.11/framework/minilang/src/main/java/org/apache/ofbiz/minilang/MiniLangUtil.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/minilang/src/main/java/org/apache/ofbiz/minilang/MiniLangUtil.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/minilang/src/main/java/org/apache/ofbiz/minilang/MiniLangUtil.java (original) +++ ofbiz/branches/release16.11/framework/minilang/src/main/java/org/apache/ofbiz/minilang/MiniLangUtil.java Sat Apr 13 17:51:39 2019 @@ -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); - } - } } } Modified: ofbiz/branches/release16.11/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java (original) +++ ofbiz/branches/release16.11/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java Sat Apr 13 17:51:39 2019 @@ -113,10 +113,9 @@ public class MimeMessageWrapper implemen if (message == null) { // deserialize the message if (serializedBytes != null) { - ByteArrayInputStream bais = new ByteArrayInputStream(serializedBytes); - try { + try (ByteArrayInputStream bais = new ByteArrayInputStream(serializedBytes)){ message = new MimeMessage(this.getSession(), bais); - } catch (MessagingException e) { + } catch (MessagingException | IOException e) { Debug.logError(e, module); throw new GeneralRuntimeException(e.getMessage(), e); } @@ -409,8 +408,7 @@ public class MimeMessageWrapper implemen public ByteBuffer getPartByteBuffer(String index) { BodyPart part = getPart(index); if (part != null) { - try { - InputStream stream = part.getInputStream(); + try (InputStream stream = part.getInputStream()) { return getByteBufferFromStream(stream); } catch (Exception e) { Debug.logError(e, module); @@ -533,18 +531,16 @@ public class MimeMessageWrapper implemen } protected ByteBuffer getByteBufferFromStream(InputStream stream) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; - try { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { for (int n; (n = stream.read(buffer)) != -1;) { baos.write(buffer, 0, n); } + return ByteBuffer.wrap(baos.toByteArray()); } catch (IOException e) { Debug.logError(e, module); return null; } - - return ByteBuffer.wrap(baos.toByteArray()); } static { Modified: ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppUtil.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppUtil.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppUtil.java (original) +++ ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppUtil.java Sat Apr 13 17:51:39 2019 @@ -249,7 +249,6 @@ public final class WebAppUtil { throw new IllegalArgumentException(webXmlFileLocation + " does not exist."); } boolean namespaceAware = true; - InputStream is = new FileInputStream(file); result = new WebXml(); LocalResolver lr = new LocalResolver(new DefaultHandler()); ErrorHandler handler = new LocalErrorHandler(webXmlFileLocation, lr); @@ -257,17 +256,10 @@ public final class WebAppUtil { digester.getParser(); digester.push(result); digester.setErrorHandler(handler); - try { + try (InputStream is = new FileInputStream(file)) { digester.parse(new InputSource(is)); } finally { digester.reset(); - if (is != null) { - try { - is.close(); - } catch (Throwable t) { - Debug.logError(t, "Exception thrown while parsing " + webXmlFileLocation + ": ", module); - } - } } result = webXmlCache.putIfAbsentAndGet(webXmlFileLocation, result); } Modified: ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/SOAPEventHandler.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/SOAPEventHandler.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/SOAPEventHandler.java (original) +++ ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/SOAPEventHandler.java Sat Apr 13 17:51:39 2019 @@ -103,8 +103,7 @@ public class SOAPEventHandler implements } if (wsdl != null) { - try { - OutputStream os = response.getOutputStream(); + try (OutputStream os = response.getOutputStream()) { response.setContentType("text/xml"); UtilXml.writeXmlDocument(os, wsdl); response.flushBuffer(); @@ -119,8 +118,7 @@ public class SOAPEventHandler implements } if (serviceName == null) { - try { - Writer writer = response.getWriter(); + try (Writer writer = response.getWriter()) { StringBuilder sb = new StringBuilder(); sb.append("<html><head><title>OFBiz SOAP/1.1 Services</title></head>"); sb.append("<body>No such service.").append("<p>Services:<ul>"); Modified: ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/view/ApacheFopWorker.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/view/ApacheFopWorker.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/view/ApacheFopWorker.java (original) +++ ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/view/ApacheFopWorker.java Sat Apr 13 17:51:39 2019 @@ -136,10 +136,10 @@ public final class ApacheFopWorker { public static void transform(File srcFile, File destFile, File stylesheetFile, String outputFormat) throws IOException, FOPException { StreamSource src = new StreamSource(srcFile); StreamSource stylesheet = stylesheetFile == null ? null : new StreamSource(stylesheetFile); - BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(destFile)); + try (BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(destFile))) { Fop fop = createFopInstance(dest, outputFormat); transform(src, stylesheet, fop); - dest.close(); + } } /** Transform an xsl-fo InputStream to the specified OutputStream format. Modified: ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/ResponseHelper.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/ResponseHelper.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/ResponseHelper.java (original) +++ ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/ResponseHelper.java Sat Apr 13 17:51:39 2019 @@ -98,13 +98,12 @@ public class ResponseHelper { } public void writeResponse(HttpServletResponse response, Writer writer) throws IOException { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - try { + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { UtilXml.writeXmlDocument(os, this.responseDocument, "UTF-8", true, true); + response.setContentLength(os.size()); + writer.write(os.toString("UTF-8")); } catch (Exception e) { throw new IOException(e.getMessage()); } - response.setContentLength(os.size()); - writer.write(os.toString("UTF-8")); } } Modified: ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/WebDavUtil.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/WebDavUtil.java?rev=1857488&r1=1857487&r2=1857488&view=diff ============================================================================== --- ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/WebDavUtil.java (original) +++ ofbiz/branches/release16.11/framework/webapp/src/main/java/org/apache/ofbiz/webapp/webdav/WebDavUtil.java Sat Apr 13 17:51:39 2019 @@ -55,14 +55,8 @@ public final class WebDavUtil { public static Document getDocumentFromRequest(HttpServletRequest request) throws IOException, SAXException, ParserConfigurationException { Document document = null; - InputStream is = null; - try { - is = request.getInputStream(); + try (InputStream is = request.getInputStream()) { document = UtilXml.readXmlDocument(is, false, "WebDAV request"); - } finally { - if (is != null) { - is.close(); - } } return document; } |
Free forum by Nabble | Edit this page |