Dhanraaj Patil
Dhanraaj Patil

Reputation: 41

Is it necessary to close the StringReader in this case?

Document doc = DocumentBuilderFactory.newInstance().
               newDocumentBuilder().
               parse(new InputSource(new StringReader(xml)));

Upvotes: 2

Views: 550

Answers (1)

LppEdd
LppEdd

Reputation: 21144

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.

Upvotes: 1

Related Questions