0

I am using nu.xom.* for my project, link found at http://www.xom.nu/. My question is about the following part of my code:

private void open() {
    builder = new Builder();
    try {
        document = builder.build(file);
    } catch (ParsingException | IOException ex) {
        Logger.getLogger(InvoiceData.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I have some unwanted file locks in my program and am practically checking all places where I open a file. Now it struck me here that builder.build(File file) does implement Closeable, so I myself am not sure here whether it closes the file properly or not.

Can anyone shed some light on this?

Regards.

skiwi
  • 66,971
  • 31
  • 131
  • 216

1 Answers1

0

Fortunately the XOM library is open source, so you can take a look at the source code of nu.xom.Builder.build(File):

public Document build(File in) 
  throws ParsingException, ValidityException, IOException {

    InputStream fin = new FileInputStream(in);
    // [...]
    String base = url.toString();
    try {
        Document doc = build(fin, base);
        return doc;
    }
    finally {   
        fin.close();
    }

}

So you pass a File instance to the build() method and inside this method a FileInputStream is opened and closed at the end.

There is a part after new FileInputStream(in) which is not enclosed by a try block. If this code throws an unchecked exception it is possible that the input stream isn't closed. But if you don't catch an exception then you can be sure, that the input stream is closed properly.

vanje
  • 10,180
  • 2
  • 31
  • 47