You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Jeffrey Sinclair <je...@cooljeff.co.uk> on 2008/10/16 09:08:19 UTC

Xerces appears to close user managed InputStream

j-users,
 
I'm finding that Xerces is closing a user managed InputStream which I'm not
expecting.
 
Am I correct in thinking this is a bug and I should proceed with submitting
a JIRA?
 
The following JUnit test demonstrates this:
 
/**
  * Tests that a simple build does not closed the original (user managed)
stream.
  * 
  * @throws Exception
  *             if there were any unexpected errors that occur.
  */
public void testBuildWithErrorsDoesNotCloseStream() throws Exception {
  byte[] XML_BYTES = "<A>a</A>".getBytes();
  XMLReader reader = XMLReaderFactory.createXMLReader();
  reader.parse(new InputSource(new
NonClosableByteArrayInputStream(XML_BYTES)));
}
 

/**
  * Throws an Assertion failure should close be called.
  */
private static class NonClosableByteArrayInputStream extends
ByteArrayInputStream {
 
  public NonClosableByteArrayInputStream(byte[] buf_) {
    super(buf_);
  }
 
  public void close() throws IOException {
    fail("Not expecting close() to be called.");
  }
}
 
junit.framework.AssertionFailedError: Not expecting close() to be called.
 at junit.framework.Assert.fail(Assert.java:47)
 at XercesTest$NonClosableByteArrayInputStream.close(XercesTest.java:203)
 at 
org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.close(Unknown
Source)
 at org.apache.xerces.impl.io.UTF8Reader.close(Unknown Source)
 at org.apache.xerces.impl.XMLEntityManager.closeReaders(Unknown Source)
 at org.apache.xerces.parsers.XML11Configuration.cleanup(Unknown Source)
 at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
 at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
 at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
 at XercesTest.testBuildDoesNotCloseStream(XercesTest.java:178)

Regards,
 
Jeff Sinclair



---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Xerces appears to close user managed InputStream

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Jeff,

It's working as designed. The API doc [1] for the SAX InputSource says: "
... standard processing of both byte and character streams is to close them
on as part of end-of-parse cleanup, so applications should not attempt to
re-use such streams after they have been handed to a parser." If you want
to block the parser from closing the stream wrap your InputStream in a
filter [2] whose close() method does nothing.

Thanks.

[1] http://java.sun.com/javase/6/docs/api/org/xml/sax/InputSource.html
[2] http://java.sun.com/javase/6/docs/api/java/io/FilterInputStream.html

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Jeffrey Sinclair <je...@cooljeff.co.uk> wrote on 10/16/2008 03:08:19 AM:

> j-users,
>
> I'm finding that Xerces is closing a user managed InputStream which I'm
not
> expecting.
>
> Am I correct in thinking this is a bug and I should proceed with
submitting
> a JIRA?
>
> The following JUnit test demonstrates this:
>
> /**
>   * Tests that a simple build does not closed the original (user managed)
> stream.
>   *
>   * @throws Exception
>   *             if there were any unexpected errors that occur.
>   */
> public void testBuildWithErrorsDoesNotCloseStream() throws Exception {
>   byte[] XML_BYTES = "<A>a</A>".getBytes();
>   XMLReader reader = XMLReaderFactory.createXMLReader();
>   reader.parse(new InputSource(new
> NonClosableByteArrayInputStream(XML_BYTES)));
> }
>
>
> /**
>   * Throws an Assertion failure should close be called.
>   */
> private static class NonClosableByteArrayInputStream extends
> ByteArrayInputStream {
>
>   public NonClosableByteArrayInputStream(byte[] buf_) {
>     super(buf_);
>   }
>
>   public void close() throws IOException {
>     fail("Not expecting close() to be called.");
>   }
> }
>
> junit.framework.AssertionFailedError: Not expecting close() to be called.
>  at junit.framework.Assert.fail(Assert.java:47)
>  at XercesTest$NonClosableByteArrayInputStream.close(XercesTest.java:203)
>  at
>
org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.close(Unknown
> Source)
>  at org.apache.xerces.impl.io.UTF8Reader.close(Unknown Source)
>  at org.apache.xerces.impl.XMLEntityManager.closeReaders(Unknown Source)
>  at org.apache.xerces.parsers.XML11Configuration.cleanup(Unknown Source)
>  at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
>  at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
>  at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
>  at XercesTest.testBuildDoesNotCloseStream(XercesTest.java:178)
>
> Regards,
>
> Jeff Sinclair
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org