X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FDataLoader.java;h=4a42218c5f21058bd2fffe5384ea9585df2a2306;hb=ae78e5179d241ca5e2439048f960fe6d4a07c990;hp=ef115c9d60a9b647147fe3dff06c9bf5d95a6283;hpb=f1fb834c62f9d9a73edeeda3fed060e0dede8cef;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/DataLoader.java b/src/be/nikiroo/fanfix/DataLoader.java index ef115c9..4a42218 100644 --- a/src/be/nikiroo/fanfix/DataLoader.java +++ b/src/be/nikiroo/fanfix/DataLoader.java @@ -6,13 +6,15 @@ import java.io.InputStream; import java.net.URL; import java.util.Map; -import javax.imageio.ImageIO; - import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.supported.BasicSupport; import be.nikiroo.utils.Cache; +import be.nikiroo.utils.CacheMemory; import be.nikiroo.utils.Downloader; +import be.nikiroo.utils.IOUtils; +import be.nikiroo.utils.Image; import be.nikiroo.utils.ImageUtils; +import be.nikiroo.utils.TraceHandler; /** * This cache will manage Internet (and local) downloads, as well as put the @@ -24,8 +26,9 @@ import be.nikiroo.utils.ImageUtils; * @author niki */ public class DataLoader { - private Cache cache; private Downloader downloader; + private Cache downloadCache; + private Cache cache; /** * Create a new {@link DataLoader} object. @@ -48,8 +51,37 @@ public class DataLoader { */ public DataLoader(File dir, String UA, int hoursChanging, int hoursStable) throws IOException { - cache = new Cache(dir, hoursChanging, hoursStable); downloader = new Downloader(UA); + downloadCache = new Cache(dir, hoursChanging, hoursStable); + cache = downloadCache; + } + + /** + * Create a new {@link DataLoader} object without disk cache (will keep a + * memory cache for manual cache operations). + * + * @param UA + * the User-Agent to use to download the resources + */ + public DataLoader(String UA) { + downloader = new Downloader(UA); + downloadCache = null; + cache = new CacheMemory(); + } + + /** + * The traces handler for this {@link Cache}. + * + * @param tracer + * the new traces handler + */ + public void setTraceHandler(TraceHandler tracer) { + downloader.setTraceHandler(tracer); + cache.setTraceHandler(tracer); + if (downloadCache != null) { + downloadCache.setTraceHandler(tracer); + } + } /** @@ -98,17 +130,29 @@ public class DataLoader { URL originalUrl) throws IOException { // MUST NOT return null try { - InputStream in = cache.load(originalUrl, false, stable); - Instance.trace("Cache " + (in != null ? "hit" : "miss") + ": " - + url); + InputStream in = null; + + if (downloadCache != null) { + in = downloadCache.load(originalUrl, false, stable); + Instance.getTraceHandler().trace( + "Cache " + (in != null ? "hit" : "miss") + ": " + url); + } if (in == null) { try { in = openNoCache(url, support, null, null, null); - cache.save(in, originalUrl); - // ..But we want a resetable stream - in.close(); - in = cache.load(originalUrl, false, stable); + if (downloadCache != null) { + downloadCache.save(in, originalUrl); + // ..But we want a resetable stream + in.close(); + in = downloadCache.load(originalUrl, false, stable); + } else { + InputStream resetIn = IOUtils.forceResetableStream(in); + if (resetIn != in) { + in.close(); + in = resetIn; + } + } } catch (IOException e) { throw new IOException("Cannot save the url: " + (url == null ? "null" : url.toString()), e); @@ -192,7 +236,7 @@ public class DataLoader { */ public void refresh(URL url, BasicSupport support, boolean stable) throws IOException { - if (!cache.check(url, false, stable)) { + if (downloadCache != null && !downloadCache.check(url, false, stable)) { open(url, support, stable).close(); } } @@ -210,30 +254,53 @@ public class DataLoader { * */ public boolean check(URL url, boolean stable) { - return cache.check(url, false, stable); + return downloadCache != null && downloadCache.check(url, false, stable); } /** * Save the given resource as an image on disk using the default image - * format for content. + * format for content or cover -- will automatically add the extension, too. * - * @param url + * @param img * the resource * @param target - * the target file + * the target file without extension + * @param cover + * use the cover image format instead of the content image format * * @throws IOException * in case of I/O error */ - 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); - } finally { - in.close(); + public void saveAsImage(Image img, File target, boolean cover) + throws IOException { + String format; + if (cover) { + format = Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER) + .toLowerCase(); + } else { + format = Instance.getConfig() + .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(); } + saveAsImage(img, new File(target.toString() + "." + format), format); + } + + /** + * Save the given resource as an image on disk using the given image format + * for content, or with "png" format if it fails. + * + * @param img + * the resource + * @param target + * the target file + * @param format + * the file format ("png", "jpeg", "bmp"...) + * + * @throws IOException + * in case of I/O error + */ + public void saveAsImage(Image img, File target, String format) + throws IOException { + ImageUtils.getInstance().saveAsImage(img, target, format); } /** @@ -244,13 +311,12 @@ public class DataLoader { * @param uniqueID * a unique ID for this resource * - * @return the resulting {@link File} * * @throws IOException * in case of I/O error */ - public File addToCache(InputStream in, String uniqueID) throws IOException { - return cache.save(in, uniqueID); + public void addToCache(InputStream in, String uniqueID) throws IOException { + cache.save(in, uniqueID); } /** @@ -266,6 +332,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). *