[jdom-interest] Re: SAXBuilder.build( File )

Elias Ross eross at m-Qube.com
Fri Nov 19 12:53:02 PST 2004


A fix sounds pretty simple to me.

Have SAXBuilder.build(File) do this:

build(File f) {
  InputStream is = FileInputStream(f);
  try {
    return build(is);
  } finally {
    is.close();
  }
}

build(URL url) {
  InputStream = ...;
  try {
    return build(is);
  } finally {
    is.close();
  }
}

On Fri, 2004-11-19 at 12:14, Jason Hunter wrote:
> Dave Jarvis wrote:
> 
> > Hi, guys.
> > 
> > Sorry for not using the servlet archive and mailing list; it's big, 
> > unwieldy, tough to search, and severely limited in many respects. (I did 
> > search for about 15 minutes for this bug.)
> > 
> > There is a bug in JDOM 1.0 with SAXBuilder.build( File ) (and likely 
> > build( URL )): the input stream is never closed. Since the file 
> > descriptor is not closed, this can eventually lead into a "too many open 
> > files" error (I have not tested this scenario; just a guess).
> > 
> > On Windows platforms, however, if a file is parsed using build( File ), 
> > then that File object cannot subsequently be renamed because Windows 
> > locks open files.
> > 
> > Windows test code:
> > 
> >     File file = new File( "file.xml" );
> >     SAXBuilder builder = getSAXBuilder();
> >     Document document = builder.build( file );
> >     boolean result = file.renameTo( new File( "renamed.xml" ) );
> > 
> >     if( !result ) System.out.println( "Rename Failed" );
> > 
> > Under Unix, the file is renamed, as expected.
> > 
> > The fix is trivial:
> > 
> >     File file = new File( "file.xml" );
> >     FileInputStream in = new FileInputStream( file );
> >     SAXBuilder builder = getSAXBuilder();
> >     Document document = builder.build( in );
> >     in.close();
> >     boolean result = file.renameTo( new File( "renamed.xml" ) );
> > 
> >     if( !result ) System.out.println( "Rename Failed" );
> > 
> > I caught a suggestion by Brett that the build( File ) be removed, back 
> > in 2000. Because both File and URL have no way of getting any 
> > java.io.InputStream(Reader) that may be related to the object in 
> > question, I would recommend removing them.
> > 
> > Sincerely,
> > Dave Jarvis
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
> 



More information about the jdom-interest mailing list