Is it necessary to close the StringReader in this case?

470 Views Asked by At
Document doc = DocumentBuilderFactory.newInstance().
               newDocumentBuilder().
               parse(new InputSource(new StringReader(xml)));
1

There are 1 best solutions below

0
LppEdd On

StringReader extends Reader, which implements Closeable.
However, just by looking at the source code, you see what it does is basically irrelevant

public void close() {
    str = null;
}

InputSource doesn't implement Closeable or AutoCloseable, that means it is still the Reader duty to close itself. Another implementation of Reader might require that, however, so close it as standard.

One that might really require closing is FileReader, which is also acceptable by InputSource.