Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8213734

SAXParser.parse(File, ..) does not close resources when Exception occurs.

    Details

    • Subcomponent:
    • Resolved In Build:
      b23
    • CPU:
      x86_64
    • OS:
      windows

      Backports

        Description

        A DESCRIPTION OF THE PROBLEM :
        By calling SAXParser.parse(File, DefaultHandler) the parser will parse the file, although if the DefaultHandler throws an exception in startElement (or some other place) the file will not be closed and there will be a resource leak. Since the file is opened by SAXParser it should be responsible for closing the file no matter what.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Create an valid XML file and name it test.xml, place it in the execution path for the code provided and run the code.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The file should be closed when the execution reaches the Thread.sleep line.
        ACTUAL -
        Go to the test.xml in the file browser and try chaning its name as an example, there will be an error that tells you the file is open in another process. Thus it has not been closed.

        ---------- BEGIN SOURCE ----------
        import org.xml.sax.Attributes;
        import org.xml.sax.SAXException;
        import org.xml.sax.helpers.DefaultHandler;

        import javax.xml.parsers.ParserConfigurationException;
        import javax.xml.parsers.SAXParser;
        import javax.xml.parsers.SAXParserFactory;
        import java.io.File;
        import java.io.IOException;

        public class SAXParserBug {

            public static void main(String... args) throws ParserConfigurationException, SAXException, IOException, InterruptedException {
                SAXParserFactory factory = SAXParserFactory.newDefaultInstance();
                SAXParser parser = factory.newSAXParser();
                try {
                    parser.parse(new File("test.xml"), new DefaultHandler() {
                        @Override
                        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                            throw new SAXException("Stop the parser.");
                        }
                    });
                } catch (SAXException e) {
                    // Do nothing
                }

                Thread.sleep(1000000); // Some process that keeps the application running.
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Instead of using parser.parse(File, ...) one can use

        try (FileInputStream fis = new FileInputStream(file)) {
          parser.parse(fis, ...);
        }

        FREQUENCY : always


          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  joehw Joe Wang
                  Reporter:
                  webbuggrp Webbug Group
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  3 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: