mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Use try-with-resources for input and output streams in TestUtil
Many of these streams were previously never closed. PiperOrigin-RevId: 613580471
This commit is contained in:
parent
0f72126c20
commit
ab8b77a6d3
@ -191,24 +191,27 @@ public class TestUtil {
|
|||||||
|
|
||||||
/** Writes test data with the specified length to the file and returns it. */
|
/** Writes test data with the specified length to the file and returns it. */
|
||||||
public static File createTestFile(File file, long length) throws IOException {
|
public static File createTestFile(File file, long length) throws IOException {
|
||||||
FileOutputStream output = new FileOutputStream(file);
|
try (FileOutputStream output = new FileOutputStream(file)) {
|
||||||
for (long i = 0; i < length; i++) {
|
for (long i = 0; i < length; i++) {
|
||||||
output.write((int) i);
|
output.write((int) i);
|
||||||
}
|
}
|
||||||
output.close();
|
}
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the bytes of an asset file. */
|
/** Returns the bytes of an asset file. */
|
||||||
public static byte[] getByteArray(Context context, String fileName) throws IOException {
|
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. */
|
/** Returns the bytes of a file using its file path. */
|
||||||
public static byte[] getByteArrayFromFilePath(String filePath) throws IOException {
|
public static byte[] getByteArrayFromFilePath(String filePath) throws IOException {
|
||||||
InputStream inputStream = new FileInputStream(filePath);
|
try (InputStream inputStream = new FileInputStream(filePath)) {
|
||||||
return Util.toByteArray(inputStream);
|
return Util.toByteArray(inputStream);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns an {@link InputStream} for reading from an asset file. */
|
/** Returns an {@link InputStream} for reading from an asset file. */
|
||||||
public static InputStream getInputStream(Context context, String fileName) throws IOException {
|
public static InputStream getInputStream(Context context, String fileName) throws IOException {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user