mirror of
https://github.com/correl/mage.git
synced 2024-12-24 11:50:45 +00:00
Use autoclosable try-withs
This commit is contained in:
parent
50f28a2bf7
commit
e7c729f11c
3 changed files with 15 additions and 47 deletions
|
@ -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()) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue