From d11e2c166b0e6fd0409f8b56a52cb13dbefba989 Mon Sep 17 00:00:00 2001 From: FC Stegerman Date: Sun, 16 Jul 2023 18:29:23 +0200 Subject: [PATCH] Utils.copyToTempFile(): use try for resource management --- app/src/main/java/protect/card_locker/Utils.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index b3ee9f9db..dd634489a 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -507,15 +507,14 @@ public class Utils { public static File copyToTempFile(Context context, InputStream input, String name) throws IOException { File file = createTempFile(context, name); - FileOutputStream out = new FileOutputStream(file); - byte[] buf = new byte[4096]; - int len; - while ((len = input.read(buf)) != -1) { - out.write(buf, 0, len); + try (input; FileOutputStream out = new FileOutputStream(file)) { + byte[] buf = new byte[4096]; + int len; + while ((len = input.read(buf)) != -1) { + out.write(buf, 0, len); + } + return file; } - out.close(); - input.close(); - return file; } public static String saveTempImage(Context context, Bitmap in, String name, Bitmap.CompressFormat format) {