X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FDataLoader.java;h=395f68314cc7caf131295d899bb179167033c052;hb=5895a95876fe63e76e726ef7fc4c97efe2ebd5b1;hp=ef115c9d60a9b647147fe3dff06c9bf5d95a6283;hpb=f1fb834c62f9d9a73edeeda3fed060e0dede8cef;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/DataLoader.java b/src/be/nikiroo/fanfix/DataLoader.java index ef115c9..395f683 100644 --- a/src/be/nikiroo/fanfix/DataLoader.java +++ b/src/be/nikiroo/fanfix/DataLoader.java @@ -1,5 +1,6 @@ package be.nikiroo.fanfix; +import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -99,8 +100,8 @@ public class DataLoader { // MUST NOT return null try { InputStream in = cache.load(originalUrl, false, stable); - Instance.trace("Cache " + (in != null ? "hit" : "miss") + ": " - + url); + Instance.getTraceHandler().trace( + "Cache " + (in != null ? "hit" : "miss") + ": " + url); if (in == null) { try { @@ -228,14 +229,53 @@ public class DataLoader { public void saveAsImage(URL url, File target) throws IOException { InputStream in = open(url, null, true); try { - ImageIO.write(ImageUtils.fromStream(in), Instance.getConfig() - .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(), - target); + saveAsImage(ImageUtils.fromStream(in), target); } finally { in.close(); } } + /** + * Save the given resource as an image on disk using the default image + * format for content. + * + * @param image + * the resource + * @param target + * the target file + * + * @throws IOException + * in case of I/O error + */ + public void saveAsImage(BufferedImage image, File target) + throws IOException { + try { + String format = Instance.getConfig() + .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(); + + boolean ok = false; + try { + ok = ImageIO.write(image, format, target); + } catch (IOException e) { + ok = false; + } + + // Some formats are not reliable + // Second change: PNG + if (!ok && !format.equals("png")) { + ok = ImageIO.write(image, "png", target); + } + + if (!ok) { + throw new IOException( + "Cannot find a writer for this image and format: " + + format); + } + } catch (IOException e) { + throw new IOException("Cannot write image to " + target, e); + } + } + /** * Manually add this item to the cache. * @@ -266,6 +306,18 @@ public class DataLoader { return cache.load(uniqueID, true, true); } + /** + * Remove the given resource from the cache. + * + * @param uniqueID + * a unique ID used to locate the cached resource + * + * @return TRUE if it was removed + */ + public boolean removeFromCache(String uniqueID) { + return cache.remove(uniqueID); + } + /** * Clean the cache (delete the cached items). *