title: Why do I get a "potential resource leak" warning with a wrapping writer object?
body: <p>I have Eclipse issue the following warning:</p>
  <blockquote>
  <p><code>Potential resource leak: '&lt;unassigned Closeable value&gt;' may not be closed</code></p>
  </blockquote>
  <p>...for the expression <code>new BufferedWriter(...)</code> in the snippet below:</p>
  <pre><code>boolean useStdout = askUserWhetherToUseStdout();
  Writer writer = useStdout ? new OutputStreamWriter(System.out) : new BufferedWriter(new FileWriter(new File(askUserForFilename())));
  try {
      writer.write(&quot;Hello World!&quot;);
  }
  finally {
      writer.close();
  }
  </code></pre>
  <p>Is this a false positive? The way I have understood the whole <code>Writer</code> thing is that any writer will close its underlying writers. In my case, <code>writer</code> will encapsulate the new buffered writer (which in turn encapsulates a file writer), and so closing <code>writer</code> should not leak any resources associated with the buffered writer or its underlying writers?</p>
  <p>What am I missing here?</p>