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

XMLEventReader must not close underlying input source while parsing

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      XMLEventReader closes underlying input source just during process of parsing XML. This is unexpected and leads to undesirable effects.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run a program compiled from the source code specified below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Console output reads:

      The UncheckedIOException is expected here.
      ACTUAL -
      Console output reads:

      We don't expect UncheckedIOException to show up here.
      There must be no way for xmlEventReader.hasNext() to be true at this point.

      ---------- BEGIN SOURCE ----------
      package ru.gazprombank.omniglossary;

      import javax.xml.XMLConstants;
      import javax.xml.stream.XMLInputFactory;
      import javax.xml.stream.XMLStreamException;
      import java.io.IOException;
      import java.io.StringReader;
      import java.io.UncheckedIOException;

      public class XMLEventReaderBugReport {

          public static void main(String[] args) throws XMLStreamException {
              var xmlInputFactory = XMLInputFactory.newFactory();
              var input = new StringReader("<a/>") {
                  private boolean closed = false;
                  @Override
                  public void close() {
                      if (closed) {
                          return;
                      }
                      this.closed = true;
                      if (System.currentTimeMillis() > 0) {
                          throw new UncheckedIOException(new IOException("close failed"));
                      }
                  }
              };
              var xmlEventReader = xmlInputFactory.createXMLEventReader(input);
              while (xmlEventReader.hasNext()) {
                  try {
                      xmlEventReader.nextEvent();
                  } catch (UncheckedIOException e) {
                      System.err.printf("We don't expect UncheckedIOException to show up here.%n");
                      break;
                  }
              }
              if (xmlEventReader.hasNext()) {
                  System.err.printf("There must be no way for xmlEventReader.hasNext() to be true at this point.%n");
              }
              try {
                  input.close();
              } catch (UncheckedIOException e) {
                  System.err.printf("The UncheckedIOException is expected here.%n");
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Wrap input stream into custom classes to control time of calling the method close() and to catch its exceptions.

      FREQUENCY : always


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

              Created:
              Updated: