Use autoclosable try-withs

This commit is contained in:
Ingmar Goudt 2019-01-19 22:03:13 +01:00
parent 50f28a2bf7
commit e7c729f11c
3 changed files with 15 additions and 47 deletions

View file

@ -70,10 +70,7 @@ public class ScryfallSymbolsSource implements Iterable<DownloadJob> {
}
// gen symbols list
ArrayList<String> allMageSymbols = new ArrayList<>();
for (int i = 0; i < SYMBOLS_LIST.length; i++) {
allMageSymbols.add(SYMBOLS_LIST[i]);
}
List<String> allMageSymbols = Arrays.asList(SYMBOLS_LIST);
for (Integer i = SYMBOLS_NUMBER_START; i <= SYMBOLS_NUMBER_END; i++) {
allMageSymbols.add(String.valueOf(SYMBOLS_NUMBER_START + i));
}
@ -111,21 +108,17 @@ public class ScryfallSymbolsSource implements Iterable<DownloadJob> {
if (destFile.exists() && (destFile.length() > 0)) {
continue;
}
FileOutputStream stream = null;
try {
try(FileOutputStream stream = new FileOutputStream(destFile)) {
// base64 transform
String data64 = foundedData.get(searchCode);
Base64.Decoder dec = Base64.getDecoder();
byte[] fileData = dec.decode(data64);
stream = new FileOutputStream(destFile);
stream.write(fileData);
LOGGER.info("New svg symbol downloaded: " + needCode);
} catch (Exception e) {
LOGGER.error("Can't decode svg icon and save to file: " + destFile.getPath() + ", reason: " + e.getMessage());
} finally {
StreamUtils.closeQuietly(stream);
}
}
}
@ -166,7 +159,7 @@ public class ScryfallSymbolsSource implements Iterable<DownloadJob> {
org.jsoup.nodes.Document doc = CardImageUtils.downloadHtmlDocument(CSS_SOURCE_URL);
org.jsoup.select.Elements cssList = doc.select(CSS_SOURCE_SELECTOR);
if (cssList.size() == 1) {
this.cssUrl = cssList.first().attr("href").toString();
this.cssUrl = cssList.first().attr("href");
}
if (this.cssUrl.isEmpty()) {

View file

@ -26,8 +26,8 @@ import java.awt.event.ItemEvent;
import java.io.*;
import java.net.*;
import java.nio.file.AccessDeniedException;
import java.util.List;
import java.util.*;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@ -817,13 +817,9 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
// can save result
if (isDownloadOK & httpConn != null) {
// save data to temp
OutputStream out = null;
OutputStream tfileout = null;
InputStream in = null;
try {
in = new BufferedInputStream(httpConn.getInputStream());
tfileout = new TFileOutputStream(fileTempImage);
out = new BufferedOutputStream(tfileout);
try (InputStream in = new BufferedInputStream(httpConn.getInputStream());
OutputStream tfileout = new TFileOutputStream(fileTempImage);
OutputStream out = new BufferedOutputStream(tfileout)) {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
@ -849,13 +845,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
}
out.write(buf, 0, len);
}
} finally {
StreamUtils.closeQuietly(in);
StreamUtils.closeQuietly(out);
StreamUtils.closeQuietly(tfileout);
}
// TODO: add two faces card correction? (WTF)
// SAVE final data
if (fileTempImage.exists()) {
@ -912,7 +902,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
this.cardsDownloadQueue.removeAll(downloadedCards);
this.cardsMissing.removeAll(downloadedCards);
if (this.cardsDownloadQueue.size() == 0) {
if (this.cardsDownloadQueue.isEmpty()) {
// stop download
uiDialog.getProgressBar().setString("0 images remaining. Please close.");
} else {