1
0
Fork 0
mirror of https://github.com/correl/mage.git synced 2025-04-06 09:13:45 -09:00

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
Mage.Client/src/main/java/org/mage/plugins/card
Mage.Updater/src/main/java/com/magefree/update/helpers

View file

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

View file

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

View file

@ -68,8 +68,9 @@ public final class FileHelper {
for (String filename : files) { for (String filename : files) {
File f = new File(filename); File f = new File(filename);
if (f.exists()) { if (f.exists()) {
f.delete(); if(f.delete()) {
System.out.println("File has been deleted: " + filename); System.out.println("File has been deleted: " + filename);
}
} else { } else {
System.out.println("ERROR. Couldn't find file to delete: " + filename); 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) { public static void downloadFile(String filename, HttpURLConnection urlConnection) {
System.out.println("Downloading " + filename); System.out.println("Downloading " + filename);
InputStream in = null; try (InputStream in = urlConnection.getInputStream() ; FileOutputStream out = new FileOutputStream(filename)){
FileOutputStream out = null;
try {
in = urlConnection.getInputStream();
File f = new File(filename); File f = new File(filename);
if (!f.exists() && f.getParentFile() != null) { if (!f.exists() && f.getParentFile() != null) {
f.getParentFile().mkdirs(); if(f.getParentFile().mkdirs()) {
System.out.println("Directories have been created: " + f.getParentFile().getPath()); System.out.println("Directories have been created: " + f.getParentFile().getPath());
}
} }
out = new FileOutputStream(filename);
byte[] buf = new byte[4 * 1024]; byte[] buf = new byte[4 * 1024];
int bytesRead; int bytesRead;
@ -105,19 +103,6 @@ public final class FileHelper {
System.out.println("File has been updated: " + filename); System.out.println("File has been updated: " + filename);
} catch (IOException e) { } catch (IOException e) {
System.out.println("i/o exception - " + e.getMessage()); 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());
}
} }
} }
} }