Merge pull request #3718 from Isira-Seneviratne/Use_try-with_resources_in_Monitor

[Android] Use try-with resources in Monitor's installFile().
This commit is contained in:
Vitalii Koshura 2020-05-18 00:11:04 +02:00 committed by GitHub
commit a41c9400f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 13 deletions

View File

@ -720,23 +720,24 @@ public class Monitor extends Service {
// If file is executable, cpu architecture has to be evaluated
// and assets directory select accordingly
String source;
final String source;
if (executable)
source = getAssetsDirForCpuArchitecture() + file;
else
source = file;
try {
final File target;
if (!targetFile.isEmpty()) {
target = new File(boincWorkingDir + targetFile);
} else {
target = new File(boincWorkingDir + file);
}
try (InputStream asset = getApplicationContext().getAssets().open(source);
OutputStream targetData = new FileOutputStream(target)) {
if (Logging.ERROR)
Log.d(Logging.TAG, "installing: " + source);
File target;
if (!targetFile.isEmpty()) {
target = new File(boincWorkingDir + targetFile);
} else {
target = new File(boincWorkingDir + file);
}
// Check path and create it
File installDir = new File(boincWorkingDir);
if (!installDir.exists()) {
@ -769,13 +770,9 @@ public class Monitor extends Service {
}
// Copy file from the asset manager to clientPath
InputStream asset = getApplicationContext().getAssets().open(source);
OutputStream targetData = new FileOutputStream(target);
while ((count = asset.read(b)) != -1) {
targetData.write(b, 0, count);
}
asset.close();
targetData.close();
success = true; //copy succeeded without exception