Ensure resource are closed
Test: unit tests
Bug: 62864604
Change-Id: Iaba692b222fb1c74679b98759da095aa3e042587
diff --git a/src/com/android/media/tests/AudioLoopbackTest.java b/src/com/android/media/tests/AudioLoopbackTest.java
index 1d9bab5..e005408 100644
--- a/src/com/android/media/tests/AudioLoopbackTest.java
+++ b/src/com/android/media/tests/AudioLoopbackTest.java
@@ -413,8 +413,9 @@
} while (!data.hasLogFile(LogFileType.RESULT) && !data.isTimedOut());
// Grab logcat for iteration
- final InputStreamSource lc = getDevice().getLogcatSince(deviceTestStartTime);
- saveLogcatForIteration(data, lc, data.getIteration());
+ try (final InputStreamSource lc = getDevice().getLogcatSince(deviceTestStartTime)) {
+ saveLogcatForIteration(data, lc, data.getIteration());
+ }
// Check if test timed out. If so, don't fail the test, but return to upper logic.
// We accept certain number of individual test timeouts.
@@ -694,11 +695,9 @@
CLog.e("Logfile not found for LogFileType=" + key.name());
} else {
File logFile = new File(logFilename);
- InputStreamSource iss = new FileInputStreamSource(logFile);
- listener.testLog(prefix, logDataType, iss);
-
- // cleanup
- iss.cancel();
+ try (InputStreamSource iss = new FileInputStreamSource(logFile)) {
+ listener.testLog(prefix, logDataType, iss);
+ }
}
}
@@ -716,7 +715,7 @@
// Copy logcat data into temp file
Files.copy(logcat.createInputStream(), temp.toPath(), REPLACE_EXISTING);
- logcat.cancel();
+ logcat.close();
} catch (final IOException e) {
CLog.i("Error when saving logcat for iteration=" + iteration);
CLog.e(e);
@@ -727,11 +726,10 @@
throws DeviceNotAvailableException, IOException {
final File csvTmpFile = File.createTempFile("audio_test_data", "csv");
mLoopbackTestHelper.writeAllResultsToCSVFile(csvTmpFile, getDevice());
- InputStreamSource iss = new FileInputStreamSource(csvTmpFile);
- listener.testLog("audio_test_data", LogDataType.JACOCO_CSV, iss);
-
+ try (InputStreamSource iss = new FileInputStreamSource(csvTmpFile)) {
+ listener.testLog("audio_test_data", LogDataType.JACOCO_CSV, iss);
+ }
// cleanup
- iss.cancel();
csvTmpFile.delete();
}