title: Correct idiom for managing multiple chained resources in try-with-resources block?
body: <p>The Java 7 <em>try-with-resources</em> syntax (also known as ARM block (<em>Automatic Resource Management</em>)) is nice, short and straightforward when using only one <code>AutoCloseable</code> resource. However, I am not sure what is the correct idiom when I need to declare multiple resources that are dependent on each other, for example a <code>FileWriter</code> and a <code>BufferedWriter</code> that wraps it. Of course, this question concerns any case when some <code>AutoCloseable</code> resources are wrapped, not only these two specific classes.</p>
  
  <p>I came up with the three following alternatives:</p>
  
  <h2>1)</h2>
  
  <p>The naive idiom I have seen is to declare only the top-level wrapper in the ARM-managed variable:</p>
  
  <pre><code>static void printToFile1(String text, File file) {
      try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
          bw.write(text);
      } catch (IOException ex) {
          // handle ex
      }
  }
  </code></pre>
  
  <p>This is nice and short, but it is broken. Because the underlying <code>FileWriter</code> is not declared in a variable, it will never be closed directly in the generated <code>finally</code> block. It will be closed only through the <code>close</code> method of the wrapping <code>BufferedWriter</code>. The problem is, that if an exception is thrown from the <code>bw</code>'s constructor, its <code>close</code> will not be called and therefore the underlying <code>FileWriter</code> <strong>will not be closed</strong>.</p>
  
  <h2>2)</h2>
  
  <pre><code>static void printToFile2(String text, File file) {
      try (FileWriter fw = new FileWriter(file);
              BufferedWriter bw = new BufferedWriter(fw)) {
          bw.write(text);
      } catch (IOException ex) {
          // handle ex
      }
  }
  </code></pre>
  
  <p>Here, both the underlying and the wrapping resource are declared in the ARM-managed variables, so both of them will certainly be closed, but the underlying <code>fw.close()</code> <strong>will be called twice</strong>: not only directly, but also through the wrapping <code>bw.close()</code>.</p>
  
  <p>This should not be a problem for these two specific classes that both implement <code>Closeable</code> (which is a subtype of <code>AutoCloseable</code>), whose contract states that multiple calls to <code>close</code> are permitted:</p>
  
  <blockquote>
    <p>Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.</p>
  </blockquote>
  
  <p>However, in a general case, I can have resources that implement only <code>AutoCloseable</code> (and not <code>Closeable</code>), which doesn't guarantee that <code>close</code> can be called multiple times:</p>
  
  <blockquote>
    <p>Note that unlike the close method of java.io.Closeable, this close method is not required to be idempotent. In other words, calling this close method more than once may have some visible side effect, unlike Closeable.close which is required to have no effect if called more than once. However, implementers of this interface are strongly encouraged to make their close methods idempotent.</p>
  </blockquote>
  
  <h2>3)</h2>
  
  <pre><code>static void printToFile3(String text, File file) {
      try (FileWriter fw = new FileWriter(file)) {
          BufferedWriter bw = new BufferedWriter(fw);
          bw.write(text);
      } catch (IOException ex) {
          // handle ex
      }
  }
  </code></pre>
  
  <p>This version should be theoretically correct, because only the <code>fw</code> represents a real resource that needs to be cleaned up. The <code>bw</code> doesn't itself hold any resource, it only delegates to the <code>fw</code>, so it should be sufficient to only close the underlying <code>fw</code>.</p>
  
  <p>On the other hand, the syntax is a bit irregular and also, Eclipse issues a warning, which I believe is a false alarm, but it is still a warning that one has to deal with:</p>
  
  <blockquote>
    <p>Resource leak: 'bw' is never closed</p>
  </blockquote>
  
  <hr>
  
  <p>So, which approach to go for? Or have I missed some other idiom that is <em>the correct</em> one?</p>