Use try-with-resources.
This commit is contained in:
parent
8bcf0c6498
commit
95333d37c8
8 changed files with 84 additions and 137 deletions
|
|
@ -147,10 +147,10 @@ public class DownloadInitializer extends Thread {
|
|||
if (!mMission.running || Thread.interrupted()) return;
|
||||
}
|
||||
|
||||
SharpStream fs = mMission.storage.getStream();
|
||||
fs.setLength(mMission.offsets[mMission.current] + mMission.length);
|
||||
fs.seek(mMission.offsets[mMission.current]);
|
||||
fs.close();
|
||||
try (SharpStream fs = mMission.storage.getStream()) {
|
||||
fs.setLength(mMission.offsets[mMission.current] + mMission.length);
|
||||
fs.seek(mMission.offsets[mMission.current]);
|
||||
}
|
||||
|
||||
if (!mMission.running || Thread.interrupted()) return;
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -80,24 +80,15 @@ public class Utility {
|
|||
@SuppressWarnings("unchecked")
|
||||
public static <T> T readFromFile(File file) {
|
||||
T object;
|
||||
ObjectInputStream objectInputStream = null;
|
||||
|
||||
try {
|
||||
objectInputStream = new ObjectInputStream(new FileInputStream(file));
|
||||
try (ObjectInputStream objectInputStream =
|
||||
new ObjectInputStream(new FileInputStream(file))) {
|
||||
object = (T) objectInputStream.readObject();
|
||||
} catch (Exception e) {
|
||||
Log.e("Utility", "Failed to deserialize the object", e);
|
||||
object = null;
|
||||
}
|
||||
|
||||
if (objectInputStream != null) {
|
||||
try {
|
||||
objectInputStream.close();
|
||||
} catch (Exception e) {
|
||||
//nothing to do
|
||||
}
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue