X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FCache.java;h=b290756d1bdcb632d22cc66caa7e812cfa26bd9e;hp=75a0f5d52e2c194a8d7b614b005387c38a088824;hb=595dfa7a6a1dc8041b3a5a4fe7ee2fae89029a69;hpb=08fe2e33007063e30fe22dc1d290f8afaa18eb1d diff --git a/src/be/nikiroo/fanfix/Cache.java b/src/be/nikiroo/fanfix/Cache.java index 75a0f5d..b290756 100644 --- a/src/be/nikiroo/fanfix/Cache.java +++ b/src/be/nikiroo/fanfix/Cache.java @@ -15,7 +15,6 @@ import java.net.HttpURLConnection; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; -import java.nio.file.FileAlreadyExistsException; import java.util.Date; import java.util.Map; import java.util.zip.GZIPInputStream; @@ -26,7 +25,6 @@ import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.supported.BasicSupport; import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.MarkableFileInputStream; -import be.nikiroo.utils.StringUtils; /** * This cache will manage Internet (and local) downloads, as well as put the @@ -97,13 +95,14 @@ public class Cache { * @param stable * TRUE for more stable resources, FALSE when they often change * - * @return the opened resource + * @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); } @@ -122,13 +121,14 @@ public class Cache { * @param originalUrl * the original {@link URL} used to locate the cached resource * - * @return the opened 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 = load(originalUrl, false, stable); if (in == null) { @@ -139,6 +139,7 @@ public class Cache { + (url == null ? "null" : url.toString()), e); } + // Was just saved, can load old, so, will not be null in = load(originalUrl, true, stable); } @@ -159,8 +160,6 @@ public class Cache { * @param stable * TRUE for more stable resources, FALSE when they often change * - * @return TRUE if it was pre-downloaded - * * @throws IOException * in case of I/O error */ @@ -204,15 +203,12 @@ public class Cache { * in case of I/O error */ public void saveAsImage(URL url, File target) throws IOException { - URL cachedUrl = new URL(url.toString() - + "." - + Instance.getConfig().getString(Config.IMAGE_FORMAT_CONTENT) - .toLowerCase()); + URL cachedUrl = new URL(url.toString()); File cached = getCached(cachedUrl); if (!cached.exists() || isOld(cached, true)) { - InputStream imageIn = Instance.getCache().open(url, null, true); - ImageIO.write(StringUtils.toImage(imageIn), Instance.getConfig() + InputStream imageIn = open(url, null, true); + ImageIO.write(IOUtils.toImage(imageIn), Instance.getConfig() .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(), cached); } @@ -228,7 +224,7 @@ public class Cache { * @param uniqueID * a unique ID for this resource * - * @return the resulting {@link FileAlreadyExistsException} + * @return the resulting {@link File} * * @throws IOException * in case of I/O error @@ -267,7 +263,9 @@ public class Cache { * * @param url * the resource to open - * @return the opened resource + * + * @return the opened resource if found, NULL i not + * * @throws IOException * in case of I/O error */ @@ -302,7 +300,7 @@ public class Cache { conn.setRequestProperty("User-Agent", UA); conn.setRequestProperty("Cookie", generateCookies(support)); conn.setRequestProperty("Accept-Encoding", "gzip"); - if (support != null) { + if (support != null && support.getCurrentReferer() != null) { conn.setRequestProperty("Referer", support.getCurrentReferer() .toString()); conn.setRequestProperty("Host", support.getCurrentReferer()