package be.nikiroo.fanfix; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Map; 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 * downloaded files into a cache. *
* As long the cached resource is not too old, it will use it instead of * retrieving the file again. * * @author niki */ public class DataLoader { private Downloader downloader; private Cache downloadCache; private Cache cache; /** * Create a new {@link DataLoader} object. * * @param dir * the directory to use as cache * @param UA * the User-Agent to use to download the resources * @param hoursChanging * the number of hours after which a cached file that is thought * to change ~often is considered too old (or -1 for * "never too old") * @param hoursStable * the number of hours after which a LARGE cached file that is * thought to change rarely is considered too old (or -1 for * "never too old") * * @throws IOException * in case of I/O error */ public DataLoader(File dir, String UA, int hoursChanging, int hoursStable) throws IOException { 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); } } /** * Open a resource (will load it from the cache if possible, or save it into * the cache after downloading if not). * * @param url * the resource to open * @param support * the support to use to download the resource * @param stable * TRUE for more stable resources, FALSE when they often change * * @return the opened resource, NOT NULL * * @throws IOException * in case of I/O error */ public InputStream open(URL url, BasicSupport support, boolean stable) throws IOException { // MUST NOT return null return open(url, support, stable, url); } /** * Open a resource (will load it from the cache if possible, or save it into * the cache after downloading if not). *
* The cached resource will be assimilated to the given original {@link URL}
*
* @param url
* the resource to open
* @param support
* the support to use to download the resource
* @param stable
* TRUE for more stable resources, FALSE when they often change
* @param originalUrl
* the original {@link URL} used to locate the cached resource
*
* @return the opened resource, NOT NULL
*
* @throws IOException
* in case of I/O error
*/
public InputStream open(URL url, BasicSupport support, boolean stable,
URL originalUrl) throws IOException {
// MUST NOT return null
try {
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);
if (downloadCache != null) {
downloadCache.save(in, originalUrl);
// ..But we want a resetable stream
in.close();
in = downloadCache.load(originalUrl, true, 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);
}
}
return in;
} catch (IOException e) {
throw new IOException("Cannot open the url: "
+ (url == null ? "null" : url.toString()), e);
}
}
/**
* Open the given {@link URL} without using the cache, but still update the
* cookies.
*
* @param url
* the {@link URL} to open
*
* @return the {@link InputStream} of the opened page
*
* @throws IOException
* in case of I/O error
*/
public InputStream openNoCache(URL url) throws IOException {
return downloader.open(url);
}
/**
* Open the given {@link URL} without using the cache, but still using and
* updating the cookies.
*
* @param url
* the {@link URL} to open
* @param support
* the {@link BasicSupport} used for the cookies
* @param postParams
* the POST parameters
* @param getParams
* the GET parameters (priority over POST)
* @param oauth
* OAuth authorization (aka, "bearer XXXXXXX")
*
* @return the {@link InputStream} of the opened page
*
* @throws IOException
* in case of I/O error
*/
public InputStream openNoCache(URL url, BasicSupport support,
Map