8232003: (fs) Files.write can leak file descriptor in the exception case
authorsgehwolf
Tue, 08 Oct 2019 15:30:46 +0200
changeset 58515 8f849d3ec1e5
parent 58514 270fdb5898f2
child 58516 d376d86b0a01
8232003: (fs) Files.write can leak file descriptor in the exception case Summary: Be sure to close the leaked OutputStream in all cases Reviewed-by: alanb, bpb, clanger
src/java.base/share/classes/java/nio/file/Files.java
--- a/src/java.base/share/classes/java/nio/file/Files.java	Wed Oct 09 10:15:58 2019 +0000
+++ b/src/java.base/share/classes/java/nio/file/Files.java	Tue Oct 08 15:30:46 2019 +0200
@@ -3550,8 +3550,8 @@
         // ensure lines is not null before opening file
         Objects.requireNonNull(lines);
         CharsetEncoder encoder = cs.newEncoder();
-        OutputStream out = newOutputStream(path, options);
-        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
+        try (OutputStream out = newOutputStream(path, options);
+             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
             for (CharSequence line: lines) {
                 writer.append(line);
                 writer.newLine();