Fix some perf/space problems, add a cover for e621
[fanfix.git] / src / be / nikiroo / fanfix / Cache.java
index 20d2885df2f5fd43ecebe72c2dbb80f222aa65bb..b290756d1bdcb632d22cc66caa7e812cfa26bd9e 100644 (file)
@@ -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
         */