8232003: (fs) Files.write can leak file descriptor in the exception case

Be sure to close the leaked OutputStream in all cases

Reviewed-by: alanb, bpb, clanger
This commit is contained in:
Severin Gehwolf 2019-10-08 15:30:46 +02:00
parent 6fe51396a2
commit dcc9cc3fdd

View File

@ -3550,8 +3550,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();