#67 Fail to close InputSteam opened for parse

open
nobody
parsing (14)
5
2003-03-26
2003-03-26
No

I'm in the process of adding on to the OpenJNLP source
I downloaded in order to delete cached files. The
problem is that I open up a JNLPParser in order to get
the directory that contains the cache for a given URL.
I then wanted to delete all of the cached files, but
the JNLP file was in use. It turns out that this is
the offending block of code in
org.nanode.jnlp.JNLPParser.java

public synchronized void parse() throws
ParseException {
...
// attempt to parse XML contents from source
try {
handler = new JNLPContentHandler(cache,
getSourceURL());

XMLReader xmlReader =
FileCache.createXMLReader(null); // get default parser
xmlReader.setContentHandler(handler);
====> xmlReader.parse(new
InputSource(openInputStream()));
descriptor = handler.getDescriptor();
} catch (Exception e) {
e.printStackTrace();
throw new ParseException(e.getMessage(), -1);
}

openInputStream opens an InputStream, but never closes
it, so later in my program, I can't delete the JNLP
file because it's still open. Since only the parse
uses the InputStream (and doesn't store it), I changed
the code to read...

// attempt to parse XML contents from source
try {
InputSource parseInputSource;
handler = new JNLPContentHandler(cache,
getSourceURL());

XMLReader xmlReader =
FileCache.createXMLReader(null); // get default parser
xmlReader.setContentHandler(handler);
parseInputSource = new
InputSource(openInputStream());
xmlReader.parse(parseInputSource);
parseInputSource.getByteStream().close();
descriptor = handler.getDescriptor();
} catch (Exception e) {
e.printStackTrace();
throw new ParseException(e.getMessage(), -1);
}

I'm not totally positive that this is what needs to be
done, but I believe it's a good practise to close the
InputStream.

- Jason Leveille
jleveille@funk.com

Discussion


Log in to post a comment.