Why use BufferedReader in this case?

Tony Eichelberger picture Tony Eichelberger · Jun 2, 2009 · Viewed 7k times · Source

What is the difference between using a BufferedReader around the StringReader in the following code vs using the StringReader only? By loading up the DOM in line 2 of both examples, it seems like the BufferedReader is not necessary?

    InputSource is = new InputSource(new StringReader(html));
    Document dom = XMLResource.load(is).getDocument();

VS

    InputSource is = new InputSource(new BufferedReader(new StringReader(html)));
    Document dom = XMLResource.load(is).getDocument();

Answer

Jon Skeet picture Jon Skeet · Jun 2, 2009

In this particular case, I see no benefit. In general there are two benefits:

  • The oh-so-handy readLine() method is only defined in BufferedReader rather than Reader (irrelevant here)
  • BufferedReader reduces IO where individual calls to the underlying reader are potentially expensive (i.e. fewer chunky calls are faster than lots of little ones) - again, irrelevant for StringReader

Cut and paste fail?