Merge commit '3519cb5c518d569235beaedfc3071cba45ec848d'
authorNiki Roo <niki@nikiroo.be>
Mon, 6 Apr 2020 10:33:51 +0000 (12:33 +0200)
committerNiki Roo <niki@nikiroo.be>
Mon, 6 Apr 2020 10:33:51 +0000 (12:33 +0200)
src/be/nikiroo/utils/CacheMemory.java
src/be/nikiroo/utils/IOUtils.java

index 232b63247f9ab0ccd9f20da405d33d4a0171adb8..de4fae3892568662fd808a96a4896eb564678ddf 100644 (file)
@@ -1,5 +1,6 @@
 package be.nikiroo.utils;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
@@ -12,12 +13,13 @@ import java.util.Map;
  * @author niki
  */
 public class CacheMemory extends Cache {
-       private Map<String, byte[]> data = new HashMap<String, byte[]>();
+       private Map<String, byte[]> data;
 
        /**
         * Create a new {@link CacheMemory}.
         */
        public CacheMemory() {
+               data = new HashMap<String, byte[]>();
        }
 
        @Override
@@ -44,7 +46,7 @@ public class CacheMemory extends Cache {
        @Override
        public InputStream load(String uniqueID, boolean allowTooOld, boolean stable) {
                if (check(uniqueID, allowTooOld, stable)) {
-                       return load(uniqueID, allowTooOld, stable);
+                       return load(getKey(uniqueID));
                }
 
                return null;
@@ -53,7 +55,7 @@ public class CacheMemory extends Cache {
        @Override
        public InputStream load(URL url, boolean allowTooOld, boolean stable) {
                if (check(url, allowTooOld, stable)) {
-                       return load(url, allowTooOld, stable);
+                       return load(getKey(url));
                }
 
                return null;
@@ -86,24 +88,37 @@ public class CacheMemory extends Cache {
        /**
         * Return a key mapping to the given unique ID.
         * 
-        * @param uniqueID
-        *            the unique ID
+        * @param uniqueID the unique ID
         * 
         * @return the key
         */
        private String getKey(String uniqueID) {
-               return "_/" + uniqueID;
+               return "UID:" + uniqueID;
        }
 
        /**
         * Return a key mapping to the given urm.
         * 
-        * @param url
-        *            thr url
+        * @param url the url
         * 
         * @return the key
         */
        private String getKey(URL url) {
-               return url.toString();
+               return "URL:" + url.toString();
+       }
+
+       /**
+        * Load the given key.
+        * 
+        * @param key the key to load
+        * @return the loaded data
+        */
+       private InputStream load(String key) {
+               byte[] data = this.data.get(key);
+               if (data != null) {
+                       return new ByteArrayInputStream(data);
+               }
+
+               return null;
        }
 }
index e3837e17ed76c8b4bdb9274edf9524182856542a..3d252eac126df091a7fa8abf55999d95af6f288f 100644 (file)
@@ -370,13 +370,30 @@ public class IOUtils {
                return errorAcc;
        }
 
+       /**
+        * Open the resource next to the given {@link Class}.
+        * 
+        * @param location
+        *            the location where to look for the resource
+        * @param name
+        *            the resource name (only the filename, no path)
+        * 
+        * @return the opened resource if found, NULL if not
+        */
+       public static InputStream openResource(
+                       @SuppressWarnings("rawtypes") Class location, String name) {
+               String loc = location.getName().replace(".", "/")
+                               .replaceAll("/[^/]*$", "/");
+               return openResource(loc + name);
+       }
+
        /**
         * Open the given /-separated resource (from the binary root).
         * 
         * @param name
-        *            the resource name
+        *            the resource name (the full path, with "/" as separator)
         * 
-        * @return the opened resource if found, NLL if not
+        * @return the opened resource if found, NULL if not
         */
        public static InputStream openResource(String name) {
                ClassLoader loader = IOUtils.class.getClassLoader();