From 91fedda0b3f954180788d0507cb14ef08726302a Mon Sep 17 00:00:00 2001 From: sgehwolf Date: Tue, 8 Oct 2019 15:30:46 +0200 Subject: [PATCH] 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/share/classes/java/nio/file/Files.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/share/classes/java/nio/file/Files.java b/src/share/classes/java/nio/file/Files.java index 012118c60..d6934f47b 100644 --- a/src/share/classes/java/nio/file/Files.java +++ b/src/share/classes/java/nio/file/Files.java @@ -3348,8 +3348,8 @@ public final class Files { // 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(); -- GitLab