diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/images/ImageCache.java b/Mage.Client/src/main/java/org/mage/plugins/card/images/ImageCache.java index a369f7d2ce9..5937360b8b4 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/images/ImageCache.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/images/ImageCache.java @@ -7,6 +7,7 @@ import com.mortennobel.imagescaling.ResampleOp; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.image.BufferedImage; +import java.io.EOFException; import java.io.File; import java.io.IOException; import java.util.Map; @@ -92,16 +93,20 @@ public class ImageCache { if (path == null) { return null; } - TFile file = new TFile(path); - if (!file.exists()) { - log.debug("File does not exist: " + file.toString()); + TFile file = getTFile(path); + if (file == null) { return null; } if (thumbnail && path.endsWith(".jpg")) { String thumbnailPath = buildThumbnailPath(path); - TFile thumbnailFile = new TFile(thumbnailPath); - if (thumbnailFile.exists()) { + TFile thumbnailFile = null; + try { + thumbnailFile = new TFile(thumbnailPath); + } catch (Exception ex) { + } + + if (thumbnailFile != null && thumbnailFile.exists()) { log.debug("loading thumbnail for " + key + ", path="+thumbnailPath); return loadImage(thumbnailFile); } else { @@ -135,15 +140,21 @@ public class ImageCache { CardDownloadData info = new CardDownloadData("Morph", "KTK", 0, false, 0, "KTK"); info.setToken(true); String path = CardImageUtils.generateTokenImagePath(info); - TFile file = new TFile(path); - return loadImage(file); + if (path == null) { + return null; + } + TFile file = getTFile(path); + return loadImage(file); } public static BufferedImage getManifestImage() { CardDownloadData info = new CardDownloadData("Manifest", "FRF", 0, false, 0, "FRF"); info.setToken(true); String path = CardImageUtils.generateTokenImagePath(info); - TFile file = new TFile(path); + if (path == null) { + return null; + } + TFile file = getTFile(path); return loadImage(file); } @@ -196,29 +207,13 @@ public class ImageCache { return getImage(key); } -// /** -// * Returns the Image corresponding to the Path -// */ -// private static BufferedImage getImageByPath(String path) { -// if (path == null) { -// return null; -// } -// TFile file = new TFile(path); -// if (!file.exists()) { -// log.warn("File does not exist: " + file.toString()); -// return null; -// } -// return getWizardsCard(loadImage(file)); -// -// } - /** * Returns the Image corresponding to the key */ private static BufferedImage getImage(String key) { try { BufferedImage image = imageCache.get(key); - return image; + return image; } catch (NullPointerException ex) { // unfortunately NullOutputException, thrown when apply() returns // null, is not public @@ -266,15 +261,18 @@ public class ImageCache { * @return {@link BufferedImage} */ public static BufferedImage loadImage(TFile file) { + if (file == null) { + return null; + } BufferedImage image = null; if (!file.exists()) { log.debug("File does not exist: " + file.toString()); return null; } try { - TFileInputStream inputStream = new TFileInputStream(file); - image = ImageIO.read(inputStream); - inputStream.close(); + try (TFileInputStream inputStream = new TFileInputStream(file)) { + image = ImageIO.read(inputStream); + } } catch (Exception e) { log.error(e, e); } @@ -284,17 +282,13 @@ public class ImageCache { public static BufferedImage makeThumbnail(BufferedImage original, String path) { BufferedImage image = getResizedImage(original, Constants.THUMBNAIL_SIZE_FULL); - TFile imageFile = new TFile(path); + TFile imageFile = getTFile(path); + if (imageFile == null) { + return null; + } try { - TFileOutputStream outputStream = null; - try { - //log.debug("thumbnail path:"+path); - outputStream = new TFileOutputStream(imageFile); + try (TFileOutputStream outputStream = new TFileOutputStream(imageFile)) { ImageIO.write(image, "jpg", outputStream); - } finally { - if (outputStream != null) { - outputStream.close(); - } } } catch (IOException e) { log.error(e,e); @@ -382,4 +376,14 @@ public class ImageCache { return getFullSizeImage(original, scale); } + + public static TFile getTFile(String path) { + try { + TFile file = new TFile(path); + return file; + } catch (NullPointerException ex) { + log.warn("Imagefile does not exist: " + path); + } + return null; + } } diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java b/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java index 3114d3192f7..0192af02f5b 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java @@ -4,13 +4,14 @@ import java.util.HashMap; import mage.client.constants.Constants; import mage.client.dialog.PreferencesDialog; import net.java.truevfs.access.TFile; +import org.apache.log4j.Logger; import org.mage.plugins.card.images.CardDownloadData; import org.mage.plugins.card.properties.SettingsManager; public class CardImageUtils { private static final HashMap pathCache = new HashMap<>(); - + private static final Logger log = Logger.getLogger(CardImageUtils.class); /** * @@ -35,7 +36,7 @@ public class CardImageUtils { return filePath; } } - + log.warn("Token image file not found: " + card.getTokenSetCode() + " - " + card.getName()); return null; }