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

Remi Forax forax at univ-mlv.fr
Thu Jun 6 06:54:29 UTC 2019


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