package be.nikiroo.utils;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
* @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
@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;
@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;
/**
* 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;
}
}