From ab8b77a6d36d265233ee60f491510e231f54d2e7 Mon Sep 17 00:00:00 2001 From: ibaker Date: Thu, 7 Mar 2024 07:41:43 -0800 Subject: [PATCH] Use try-with-resources for input and output streams in `TestUtil` Many of these streams were previously never closed. PiperOrigin-RevId: 613580471 --- .../androidx/media3/test/utils/TestUtil.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/libraries/test_utils/src/main/java/androidx/media3/test/utils/TestUtil.java b/libraries/test_utils/src/main/java/androidx/media3/test/utils/TestUtil.java index b335d8ecd3..079438016e 100644 --- a/libraries/test_utils/src/main/java/androidx/media3/test/utils/TestUtil.java +++ b/libraries/test_utils/src/main/java/androidx/media3/test/utils/TestUtil.java @@ -191,23 +191,26 @@ public class TestUtil { /** Writes test data with the specified length to the file and returns it. */ public static File createTestFile(File file, long length) throws IOException { - FileOutputStream output = new FileOutputStream(file); - for (long i = 0; i < length; i++) { - output.write((int) i); + try (FileOutputStream output = new FileOutputStream(file)) { + for (long i = 0; i < length; i++) { + output.write((int) i); + } } - output.close(); return file; } /** Returns the bytes of an asset file. */ public static byte[] getByteArray(Context context, String fileName) throws IOException { - return Util.toByteArray(getInputStream(context, fileName)); + try (InputStream inputStream = getInputStream(context, fileName)) { + return Util.toByteArray(inputStream); + } } /** Returns the bytes of a file using its file path. */ public static byte[] getByteArrayFromFilePath(String filePath) throws IOException { - InputStream inputStream = new FileInputStream(filePath); - return Util.toByteArray(inputStream); + try (InputStream inputStream = new FileInputStream(filePath)) { + return Util.toByteArray(inputStream); + } } /** Returns an {@link InputStream} for reading from an asset file. */