"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:
+4
-4
@@ -624,9 +624,10 @@ public class DataResourceWorker implements org.apache.ofbiz.widget.content.Data
|
||||
|
||||
public static String renderDataResourceAsText(LocalDispatcher dispatcher, 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(dispatcher, delegator, dataResourceId, writer, templateContext, locale, targetMimeTypeId, cache, null);
|
||||
return writer.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static String renderDataResourceAsText(LocalDispatcher dispatcher, String dataResourceId, Appendable out,
|
||||
@@ -884,14 +885,13 @@ public class DataResourceWorker implements org.apache.ofbiz.widget.content.Data
|
||||
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 = "";
|
||||
|
||||
+2
-1
@@ -1354,8 +1354,9 @@ public class CommunicationEventServices {
|
||||
URL imageUrl;
|
||||
try {
|
||||
imageUrl = FlexibleLocation.resolveLocation("component://common-theme/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) {
|
||||
|
||||
+1
-2
@@ -578,9 +578,8 @@ public class ICalConverter {
|
||||
calendar = new Calendar();
|
||||
} else {
|
||||
if (Debug.verboseOn()) 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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+2
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -536,8 +536,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()) {
|
||||
// substitute the freemarker variables...
|
||||
ScreenStringRenderer foScreenStringRenderer = null;
|
||||
if(MimeConstants.MIME_PLAIN_TEXT.equals(attachmentType)){
|
||||
@@ -550,7 +549,6 @@ public class EmailServices {
|
||||
screensAtt.render(xslfoAttachScreenLocation);
|
||||
|
||||
// create the output stream for the generation
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
if (MimeConstants.MIME_PLAIN_TEXT.equals(attachmentType)) {
|
||||
baos.write(writer.toString().getBytes("UTF-8"));
|
||||
@@ -561,10 +559,6 @@ public class EmailServices {
|
||||
ApacheFopWorker.transform(src, null, fop);
|
||||
}
|
||||
|
||||
// and generate the attachment
|
||||
baos.flush();
|
||||
baos.close();
|
||||
|
||||
// store in the list of maps for sendmail....
|
||||
bodyParts.add(UtilMisc.<String, Object>toMap("content", baos.toByteArray(), "type", attachmentType, "filename", attachmentName));
|
||||
|
||||
|
||||
@@ -188,9 +188,10 @@ public class EntitySaxReader extends DefaultHandler {
|
||||
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 {
|
||||
@@ -199,17 +200,9 @@ public class EntitySaxReader extends DefaultHandler {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-9
@@ -105,10 +105,9 @@ public class MimeMessageWrapper implements java.io.Serializable {
|
||||
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);
|
||||
}
|
||||
@@ -393,8 +392,7 @@ public class MimeMessageWrapper implements java.io.Serializable {
|
||||
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);
|
||||
@@ -510,18 +508,16 @@ public class MimeMessageWrapper implements java.io.Serializable {
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -103,8 +103,7 @@ public class SOAPEventHandler implements EventHandler {
|
||||
}
|
||||
|
||||
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 EventHandler {
|
||||
}
|
||||
|
||||
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>");
|
||||
|
||||
@@ -138,10 +138,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.
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user