RFR: 8374819: Some I/O streams left unclosed

Alexander Matveev almatvee at openjdk.org
Fri Jan 9 02:26:25 UTC 2026


On Fri, 9 Jan 2026 02:06:29 GMT, Alexey Semenyuk <asemenyuk at openjdk.org> wrote:

>> What I mean that our code expects specific set of exception and
>> `final Document doc = XmlUtils.initDocumentBuilder().parse(new ByteArrayInputStream(Files.readAllBytes(appImageFilePath)));` will throw different set of exception than `final Document doc = XmlUtils.initDocumentBuilder().parse(appImageFilePath.toFile());`. Such difference needs to be documented or code adjusted. Is it possible that `parse()` will change exception being thrown in case if file does not exist? If possible then our code will be broken. If we depend on  `java.nio.file.NoSuchFileException` vs `java.io.FileNotFoundException`, then lets throw exception we need by checking if file exist first.
>
>> Is it possible that `parse()` will change exception being thrown in case if file does not exist?
> 
> I assume you mean [javax.xml.parsers.DocumentBuilder#parse(java.io.File)](https://docs.oracle.com/en/java/javase/25/docs/api/java.xml/javax/xml/parsers/DocumentBuilder.html#parse(java.io.File))
> 
> As we have figured out the default JDK implementation (Xerces XML parser) throws `java.io.FileNotFoundException` if a file doesn't exist and throws `org.xml.sax.SAXParseException` if the file is a directory. Another DOM XML parser (a different version of Xerces?) may behave differently.
> 
> The use of `javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream)` eliminates differences in how XML parsers handle file system I/O errors. jpackage implementation code delegates filesystem I/O to `java.nio.file.Files`, XML parser deals with the byte stream in memory. We don't depend on how XML parser reports filesystem I/O errors because it reads data from memory, not from the filesystem. 
> 
> In the test code it doesn't matter how specific XML parser handles I/O errors, so we can use `javax.xml.parsers.DocumentBuilder#parse(java.io.File)`.
> 
>> If we depend on `java.nio.file.NoSuchFileException` vs `java.io.FileNotFoundException`, then lets throw exception we need by checking if file exist first.
> 
> We don't depend on `java.io.FileNotFoundException`. We depend on `java.nio.file.NoSuchFileException` that `Files.readAllBytes(Path)` may throw before any `DocumentBuilder#parse(...)` is called.

// Provide java.io.InputStream instead of java.io.File to eliminates differences in how XML parsers
// handle file system I/O errors.  jpackage implementation code depends on how java.nio.file.Files
// handles I/O errors.
final Document doc = XmlUtils.initDocumentBuilder().parse(
                    new ByteArrayInputStream(Files.readAllBytes(appImageFilePath)));

Thanks for explanation. We need to document it to be clear. Simple comment will be enough like above.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/29007#discussion_r2674578361


More information about the core-libs-dev mailing list