DocumentBuilder.parse是否关闭InputStream

时间:2012-03-06 06:14:25

标签: java xml inputstream

对于类似以下的代码:

InputStream is = new FileInputstream("test.xml");
Document doc = DocumentBuilder.parser(is);

我的问题是我是否需要手动关闭流(调用is.close())。 DocumentBuilder是否为我关闭了InputStream?

5 个答案:

答案 0 :(得分:7)

使用以下测试代码查看输入流是否已关闭,您可以看到哪一行代码关闭了流。

public class DocumentBuilderTest {

 public static void main(String[] args) {
   try {
     InputStream is = new MyInputStream("project.xml");
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
     documentBuilder.parse(is);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }

 static class MyInputStream extends FileInputStream {   
   public MyInputStream(String filename) throws FileNotFoundException {
     super(filename);
   }

   @Override
   public void close() throws IOException {
     // here we log when the stream is close.
     System.out.println("file input stream closed.");
     Exception e = new Exception();
     e.printStackTrace();
     super.close();
   }

 }
}

传递给DocumentBuilder的输入流是否关闭取决于DOMParser实现。在我的环境中,文件输入流已关闭,请参阅下面的堆栈跟踪:

at DocumentBuilderTest$MyInputStream.close(DocumentBuilderTest.java:37)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.close(XMLEntityManager.java:3047)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.close(UTF8Reader.java:661)
at com.sun.xml.internal.stream.Entity$ScannedEntity.close(Entity.java:441)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.endEntity(XMLEntityManager.java:1406)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1763)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipSpaces(XMLEntityScanner.java:1543)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(XMLDocumentScannerImpl.java:1400)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
at DocumentBuilderTest.main(DocumentBuilderTest.java:22)

因此,您无法在此特定示例中手动关闭流。但是,当您确定不再使用该流时,最好关闭输入流。在您的情况下,一旦解析了文档,就不再需要输入流,因此可以安全地关闭流,我建议您这样做。

答案 1 :(得分:4)

我所做的是扩展FilterInputStream,并覆盖close()方法实现以防止关闭InputStream

public class HackInputStream
    extends FilterInputStream {

    public HackInputStream(InputStream in) {
        super(in);
    }

    @Override
    public void close() {
        // this does not close stream.
        // use hackedClose() instead.
    }

    public void hackedClose() 
            throws IOException {
        super.close();
    }


}

答案 2 :(得分:3)

通常的合同是获取资源的代码必须释放它。这是一种很好的做法,因为这意味着如果任何干预代码抛出异常,您就不会泄漏资源。

使用try-with-resources块:

try (InputStream in = new FileInputStream("foo")) {
  // process data
}

对于Java 7之前的版本:

InputStream in = new FileInputStream("foo");
try {
  // process data
} finally {
  in.close();
}

答案 3 :(得分:2)

文档没有提及它关闭流,我不希望它为你关闭流。

可以肯定的是,您可以在调用parse()之后阅读源代码,或者检查它是否在一个简单的示例中打开。

但简短的回答:是的,您需要在之后手动关闭它。

答案 4 :(得分:0)

如果您需要在DOM解析后关闭流(例如,为了能够继续读取它),请参阅此主题: Java create InputStream from ZipInputStream entry