RFR: 8374819: Some I/O streams left unclosed
Alexey Semenyuk
asemenyuk at openjdk.org
Thu Jan 8 20:20:02 UTC 2026
On Thu, 8 Jan 2026 19:39:54 GMT, Johannes Döbler <duke at openjdk.org> wrote:
>> - Replace `Files.newInputStream(path)` in chain method calls with either `new ByteArrayInputStream(Files.readAllBytes(path))` or `path.toFile()` if there is an alternative method taking a `File` instance instead of an `InputStream` and if appropriate.
>> - Add missing try-with-resources for `Class.getResourceAsStream()` calls.
>
> src/jdk.jpackage/share/classes/jdk/jpackage/internal/AppImageFile.java line 167:
>
>> 165: try {
>> 166: final Document doc = XmlUtils.initDocumentBuilder().parse(
>> 167: new ByteArrayInputStream(Files.readAllBytes(appImageFilePath)));
>
> would `.parse(appImageFilePath.toFile())` also work?
Yes, but `DocumentBuilder#parse(java.io.File)` will throw `SAXException` if the file doesn't exist, while `Files#readAllBytes(Path)` throws `IOException`. This change will break the corresponding unit test. I already tried it.
I switched to using `DocumentBuilder#parse(java.io.File)` in the test code because it doesn't matter what exceptions it throws.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/29007#discussion_r2673749253
More information about the core-libs-dev
mailing list