changeset 13813:6f7954907722

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
author sgehwolf
date Tue, 08 Oct 2019 15:30:46 +0200
parents 814fc47280c9
children 02a480beae45
files src/share/classes/java/nio/file/Files.java
diffstat 1 files changed, 2 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/nio/file/Files.java	Tue Dec 31 12:49:35 2019 +0000
+++ b/src/share/classes/java/nio/file/Files.java	Tue Oct 08 15:30:46 2019 +0200
@@ -3348,8 +3348,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();