user994165 user994165 - 6 days ago 5
Java Question

Closing StreamSource

I was getting a too many open files error on my web service and just wanted to make sure that there's nothing else that I have to close. I added a

close()
on
outWriter
(
StringWriter
), but the JavaDoc said that this has no effect.
getCachedExtractSoapBodyXslt()
gets the
javax.xml.transform.Transformer
object.

String payload;
StreamResult result=null;
StringWriter outWriter=null;
try {
// Programmatically extract the SOAP Body from message
outWriter = new StringWriter();
result = new StreamResult(outWriter);
Source src = new StreamSource(new java.io.StringReader(doc));
getCachedExtractSoapBodyXslt().transform(src, result);
StringBuffer sb = outWriter.getBuffer();
payload = sb.toString();
} catch (TransformerException e) {
throw new ComponentException("Unable to extract SOAP Body");
}
finally {
LOG.debug("Closing XSLT transformer output stream");
try {
outWriter.close();
}
catch (IOException e) {
LOG.error("Failed to close stream for SOAP message");
}
}

Answer

I had to move this to it's own variable and to close it

new java.io.StringReader(doc)