RFR: JDK-8223333: Use try-with-resources where feasible

Andy Herrick andy.herrick at oracle.com
Thu Jun 6 15:44:16 UTC 2019


good catch - update 7 files to use Files.newBufferedWriter().

revised webrev: http://cr.openjdk.java.net/~herrick/8223333/webrev.02 
<http://cr.openjdk.java.net/%7Eherrick/8223333/webrev.02>

/Andy

On 6/6/2019 2:54 AM, Remi Forax wrote:
> Hi Andy,
>
> A code like this is not safe
>    try (Writer w = new BufferedWriter(new FileWriter( ...
>
> because new BufferedWriter may throw an OutOfMemoryError, in that case the file descriptor used by the FileWriter is not freed.
>
> There are two ways to fix that, either you need to expand the try-with-resources to use two lines
>    try (FileWriter fileWriter = new FileWriter( ...);
>         Writer w = new BufferedWriter(fileWriter)) { ...
>
> or
>    you use Files.newBufferedWriter(Path) which abstract the creation of a FileWriter and a BufferedWriter into one method,
>    but you have to use a java.nio.file.Path instead a java.nio.File.
>
> regards,
> Rémi
>
> ----- Mail original -----
>> De: "Andy Herrick" <andy.herrick at oracle.com>
>> À: "core-libs-dev" <core-libs-dev at openjdk.java.net>
>> Envoyé: Jeudi 6 Juin 2019 01:54:05
>> Objet: RFR: JDK-8223333: Use try-with-resources where feasible
>> Please review the jpackage fix for bug [1] at [2].
>>
>> This is a fix for the JDK-8200758-branch branch of the open sandbox
>> repository (jpackage).
>>
>> [1] https://bugs.openjdk.java.net/browse/JDK-8223333
>> [2] http://cr.openjdk.java.net/~herrick/8223333/
>>
>> /Andy



More information about the core-libs-dev mailing list