ensure closing resources after finishing/canceling download of pictures

This commit is contained in:
Marc Zwart 2018-03-20 15:37:25 +01:00
parent 1e18875725
commit f883d6b0a8

View file

@ -24,6 +24,7 @@ import mage.client.MageFrame;
import mage.client.dialog.PreferencesDialog; import mage.client.dialog.PreferencesDialog;
import mage.client.util.sets.ConstructedFormats; import mage.client.util.sets.ConstructedFormats;
import mage.remote.Connection; import mage.remote.Connection;
import mage.util.StreamUtils;
import net.java.truevfs.access.TFile; import net.java.truevfs.access.TFile;
import net.java.truevfs.access.TFileOutputStream; import net.java.truevfs.access.TFileOutputStream;
import net.java.truevfs.access.TVFS; import net.java.truevfs.access.TVFS;
@ -782,18 +783,16 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
if (responseCode == 200) { if (responseCode == 200) {
// download OK // download OK
// save data to temp // save data to temp
BufferedOutputStream out; BufferedOutputStream out = null;
try (BufferedInputStream in = new BufferedInputStream(httpConn.getInputStream())) { BufferedInputStream in = null;
try {
in = new BufferedInputStream(httpConn.getInputStream());
out = new BufferedOutputStream(new TFileOutputStream(fileTempImage)); out = new BufferedOutputStream(new TFileOutputStream(fileTempImage));
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len; int len;
while ((len = in.read(buf)) != -1) { while ((len = in.read(buf)) != -1) {
// user cancelled // user cancelled
if (cancel) { if (cancel) {
in.close();
out.flush();
out.close();
// stop download, save current state and exit // stop download, save current state and exit
TFile archive = destFile.getTopLevelArchive(); TFile archive = destFile.getTopLevelArchive();
///* not need to unmout/close - it's auto action ///* not need to unmout/close - it's auto action
@ -804,8 +803,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} catch (Exception e) { } catch (Exception e) {
logger.error("Can't close archive file: " + e.getMessage(), e); logger.error("Can't close archive file: " + e.getMessage(), e);
} }
}
}//*/
try { try {
TFile.rm(fileTempImage); TFile.rm(fileTempImage);
} catch (Exception e) { } catch (Exception e) {
@ -816,9 +814,11 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
out.write(buf, 0, len); out.write(buf, 0, len);
} }
} }
// TODO: remove to finnaly section? finally {
out.flush(); StreamUtils.closeQuietly(in);
out.close(); StreamUtils.closeQuietly(out);
}
// TODO: add two faces card correction? (WTF) // TODO: add two faces card correction? (WTF)
// SAVE final data // SAVE final data