From: Niki Roo Date: Mon, 6 Apr 2020 10:20:06 +0000 (+0200) Subject: fix CacheMemory stackoverflow X-Git-Tag: fanfix-swing-0.0.1~21^2~2^2~2 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=4cd64dd5d8def0f05e39ec1b6abe118e6b52d4cc;p=fanfix-swing.git fix CacheMemory stackoverflow --- diff --git a/changelog.md b/changelog.md index 395fae05..21f865f5 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # nikiroo-utils +## Version WIP + +- new: IOUtils: openResource with anchor class +- new: ConfigItem: easier to extend +- fix: CacheMemory: fix stackoverflow bug +- fix: Bundles/TransBundles: def values + ## Version 5.1.0 - Downloader: new Offline mode for cache accesses only diff --git a/src/be/nikiroo/utils/CacheMemory.java b/src/be/nikiroo/utils/CacheMemory.java index 232b6324..de4fae38 100644 --- a/src/be/nikiroo/utils/CacheMemory.java +++ b/src/be/nikiroo/utils/CacheMemory.java @@ -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 data = new HashMap(); + private Map data; /** * Create a new {@link CacheMemory}. */ public CacheMemory() { + data = new HashMap(); } @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; } }