Use try-with-resources.

This commit is contained in:
Isira Seneviratne 2020-11-06 06:16:13 +05:30
parent 8bcf0c6498
commit 95333d37c8
8 changed files with 84 additions and 137 deletions

View file

@ -108,7 +108,6 @@ public abstract class Postprocessing implements Serializable {
public void run(DownloadMission target) throws IOException {
this.mission = target;
CircularFileWriter out = null;
int result;
long finalLength = -1;
@ -151,30 +150,32 @@ public abstract class Postprocessing implements Serializable {
return -1;
};
out = new CircularFileWriter(mission.storage.getStream(), tempFile, checker);
out.onProgress = (long position) -> mission.done = position;
try (CircularFileWriter out = new CircularFileWriter(
mission.storage.getStream(), tempFile, checker)) {
out.onProgress = (long position) -> mission.done = position;
out.onWriteError = (err) -> {
mission.psState = 3;
mission.notifyError(ERROR_POSTPROCESSING_HOLD, err);
out.onWriteError = err -> {
mission.psState = 3;
mission.notifyError(ERROR_POSTPROCESSING_HOLD, err);
try {
synchronized (this) {
while (mission.psState == 3)
wait();
try {
synchronized (this) {
while (mission.psState == 3)
wait();
}
} catch (InterruptedException e) {
// nothing to do
Log.e(getClass().getSimpleName(), "got InterruptedException");
}
} catch (InterruptedException e) {
// nothing to do
Log.e(this.getClass().getSimpleName(), "got InterruptedException");
}
return mission.errCode == ERROR_NOTHING;
};
return mission.errCode == ERROR_NOTHING;
};
result = process(out, sources);
result = process(out, sources);
if (result == OK_RESULT)
finalLength = out.finalizeFile();
if (result == OK_RESULT)
finalLength = out.finalizeFile();
}
} else {
result = OK_RESULT;
}
@ -184,9 +185,6 @@ public abstract class Postprocessing implements Serializable {
source.close();
}
}
if (out != null) {
out.close();
}
if (tempFile != null) {
//noinspection ResultOfMethodCallIgnored
tempFile.delete();