[JENKINS-39363,CID-1205051] - Prevent resource leak in hudson.XmlFile#readRaw() in the case of encoding issues. (#2518)

[JENKINS-39363,CID-1205051] - Prevent resource leak in hudson.XmlFile#readRaw() in the case of encoding issues.
This commit is contained in:
Oleg Nenashev authored and GitHub committed 2016-10-29 15:28:39 +04:00
1 parent 1d16ce024c
commit fe2b697e14
1 file changed
+12 -1
+12 -1
View File
@@ -51,6 +51,7 @@ import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -196,9 +197,19 @@ public final class XmlFile {
* Opens a {@link Reader} that loads XML.
* This method uses {@link #sniffEncoding() the right encoding},
* not just the system default encoding.
* @throws IOException Encoding issues
* @return Reader for the file. should be close externally once read.
*/
public Reader readRaw() throws IOException {
return new InputStreamReader(new FileInputStream(file),sniffEncoding());
FileInputStream fileInputStream = new FileInputStream(file);
try {
return new InputStreamReader(fileInputStream, sniffEncoding());
} catch(IOException ex) {
// Exception may happen if we fail to find encoding or if this encoding is unsupported.
// In such case we close the underlying stream and rethrow.
Util.closeAndLogFailures(fileInputStream, LOGGER, "FileInputStream", file.toString());
throw ex;
}
}
/**