From e7c729f11c29671c6a9b427f7d3ce674702faaf8 Mon Sep 17 00:00:00 2001 From: Ingmar Goudt Date: Sat, 19 Jan 2019 22:03:13 +0100 Subject: [PATCH] Use autoclosable try-withs --- .../dl/sources/ScryfallSymbolsSource.java | 13 ++------- .../card/images/DownloadPicturesService.java | 20 ++++--------- .../magefree/update/helpers/FileHelper.java | 29 +++++-------------- 3 files changed, 15 insertions(+), 47 deletions(-) diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallSymbolsSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallSymbolsSource.java index 483bb27e86..11388b07b9 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallSymbolsSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallSymbolsSource.java @@ -70,10 +70,7 @@ public class ScryfallSymbolsSource implements Iterable { } // gen symbols list - ArrayList allMageSymbols = new ArrayList<>(); - for (int i = 0; i < SYMBOLS_LIST.length; i++) { - allMageSymbols.add(SYMBOLS_LIST[i]); - } + List 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 { 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 { 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()) { diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPicturesService.java b/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPicturesService.java index ddec9bd3b4..44b6256902 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPicturesService.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPicturesService.java @@ -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 { diff --git a/Mage.Updater/src/main/java/com/magefree/update/helpers/FileHelper.java b/Mage.Updater/src/main/java/com/magefree/update/helpers/FileHelper.java index d23c0bdf4c..6ca8233d86 100644 --- a/Mage.Updater/src/main/java/com/magefree/update/helpers/FileHelper.java +++ b/Mage.Updater/src/main/java/com/magefree/update/helpers/FileHelper.java @@ -68,8 +68,9 @@ public final class FileHelper { for (String filename : files) { File f = new File(filename); if (f.exists()) { - f.delete(); - System.out.println("File has been deleted: " + filename); + if(f.delete()) { + System.out.println("File has been deleted: " + filename); + } } else { System.out.println("ERROR. Couldn't find file to delete: " + filename); } @@ -84,17 +85,14 @@ public final class FileHelper { */ public static void downloadFile(String filename, HttpURLConnection urlConnection) { System.out.println("Downloading " + filename); - InputStream in = null; - FileOutputStream out = null; - try { - in = urlConnection.getInputStream(); + try (InputStream in = urlConnection.getInputStream() ; FileOutputStream out = new FileOutputStream(filename)){ File f = new File(filename); if (!f.exists() && f.getParentFile() != null) { - f.getParentFile().mkdirs(); - System.out.println("Directories have been created: " + f.getParentFile().getPath()); + if(f.getParentFile().mkdirs()) { + System.out.println("Directories have been created: " + f.getParentFile().getPath()); + } } - out = new FileOutputStream(filename); byte[] buf = new byte[4 * 1024]; int bytesRead; @@ -105,19 +103,6 @@ public final class FileHelper { System.out.println("File has been updated: " + filename); } catch (IOException e) { System.out.println("i/o exception - " + e.getMessage()); - } finally { - closeQuietly(in); - closeQuietly(out); - } - } - - public static void closeQuietly(Closeable s) { - if(s != null) { - try { - s.close(); - } catch (Exception e) { - System.out.println("i/o exception - " + e.getMessage()); - } } } }