7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails

Rémi Forax forax at univ-mlv.fr
Wed Aug 17 17:30:01 UTC 2011


On 08/17/2011 06:41 PM, Jing LV wrote:
> Hi Alan,
>
> Sorry for late reply, I somehow miss this mail. The patch seems 
> perfect. Just a curious question about the fix in BufferedWriter:
>
> -            try {
> +            try (Writer w = out) {
>                  flushBuffer();
>              } finally {
> -                out.close();
>                  out = null;
>                  cb = null;
>              }
>
>    I see this is a java7 new feature (try-catch-resource), but it use 
> a new variable.

Yes, you have to declare a new variable, you can't use an expression.

>    I am not very familiar with the new feature yet, so is this better 
> than put out.close() in finally block?

see 
http://download.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html
the difference is the way the exception that is thrown by out.close() if 
fushBuffer()
also throws an exception is managed (see supressed exception)

>    And if we'd like to fix java6 as well, we may use finally? Not sure 
> if the complied binary would be the same?

It will not be the same but jdk6 has no suppressed exception.

Rémi




More information about the core-libs-dev mailing list