Skip to content

Commit 2b33f5d

Browse files
fix: fix BufferedReader resource leak in FileIOUtils.readAsUTFStringLines (#18470)
1 parent a649188 commit 2b33f5d

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

hudi-io/src/main/java/org/apache/hudi/io/util/FileIOUtils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.nio.charset.StandardCharsets;
4444
import java.nio.file.Files;
4545
import java.nio.file.Path;
46-
import java.util.ArrayList;
4746
import java.util.Arrays;
4847
import java.util.Collections;
4948
import java.util.Comparator;
@@ -94,11 +93,13 @@ public static String readAsUTFString(InputStream input, int length) throws IOExc
9493
* @return String lines in a list.
9594
*/
9695
public static List<String> readAsUTFStringLines(InputStream input) {
97-
List<String> lines = new ArrayList<>();
98-
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
99-
lines = bufferedReader.lines().collect(Collectors.toList());
100-
closeQuietly(bufferedReader);
101-
return lines;
96+
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
97+
return bufferedReader.lines().collect(Collectors.toList());
98+
} catch (IOException e) {
99+
// Note: stream errors from lines().collect(...) surface as UncheckedIOException and bypass this block.
100+
// This catch handles IOException from close() during try-with-resources teardown.
101+
throw new RuntimeException(e);
102+
}
102103
}
103104

104105
public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {

0 commit comments

Comments
 (0)