You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by de...@apache.org on 2001/04/25 16:35:39 UTC
cvs commit: xml-batik/sources/org/apache/batik/util ParsedURL.java SoftReferenceCache.java
deweese 01/04/25 07:35:39
Modified: sources/org/apache/batik/ext/awt/image/spi
ImageTagRegistry.java
sources/org/apache/batik/util ParsedURL.java
SoftReferenceCache.java
Log:
1) Fixed hashCode and equals on ParsedURL so they work, so the Image Cache
now works. I also improved the implementation to reduce URL data
copying (potentially important in the face of 'data' URLs).
2) Added a cleaning thread to the SoftReferenceCache. This cleans
out key's from the cache when the soft referenced value is cleared.
This keeps key cruft from accumulating in the cache.
Revision Changes Path
1.4 +2 -0 xml-batik/sources/org/apache/batik/ext/awt/image/spi/ImageTagRegistry.java
Index: ImageTagRegistry.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/ext/awt/image/spi/ImageTagRegistry.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ImageTagRegistry.java 2001/04/24 21:34:49 1.3
+++ ImageTagRegistry.java 2001/04/25 14:35:38 1.4
@@ -71,10 +71,12 @@
ret = cache.request(purl);
if (ret != null) {
+ // System.out.println("Image came from cache" + purl);
if (colorSpace != null)
ret = new ProfileRable(ret, colorSpace);
return ret;
}
+ // System.out.println("Image didn't come from cache: " + purl);
InputStream is = null;
boolean openFailed = false;
1.3 +84 -13 xml-batik/sources/org/apache/batik/util/ParsedURL.java
Index: ParsedURL.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/util/ParsedURL.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ParsedURL.java 2001/04/24 21:34:52 1.2
+++ ParsedURL.java 2001/04/25 14:35:38 1.3
@@ -97,19 +97,78 @@
return new URL(protocol, host, port, file);
}
+ public int hashCode() {
+ int hc = port;
+ if (protocol != null)
+ hc ^= protocol.hashCode();
+ if (host != null)
+ hc ^= host.hashCode();
+
+ // For some URLS path and ref can get fairly long
+ // and the most unique part is towards the end
+ // so we grab that part for HC purposes
+ if (path != null) {
+ int len = path.length();
+ if (len > 20)
+ hc ^= path.substring(len-20).hashCode();
+ else
+ hc ^= path.hashCode();
+ }
+ if (ref != null) {
+ int len = ref.length();
+ if (len > 20)
+ hc ^= ref.substring(len-20).hashCode();
+ else
+ hc ^= ref.hashCode();
+ }
+
+ return hc;
+ }
+
public boolean equals(Object obj) {
- if (! (obj instanceof ParsedURL))
+ if (obj == null) return false;
+ if (! (obj instanceof URLData))
return false;
- ParsedURL purl = (ParsedURL)obj;
- String s1 = toString();
- String s2 = purl.toString();
- return (s1.equals(s2));
- }
- public int hashCode() {
- return toString().hashCode();
+ URLData ud = (URLData)obj;
+ if (ud.port != port)
+ return false;
+
+ if (ud.protocol==null) {
+ if (protocol != null)
+ return false;
+ } else if (protocol == null)
+ return false;
+ else if (!ud.protocol.equals(protocol))
+ return false;
+
+ if (ud.host==null) {
+ if (host !=null)
+ return false;
+ } else if (host == null)
+ return false;
+ else if (!ud.host.equals(host))
+ return false;
+
+ if (ud.ref==null) {
+ if (ref !=null)
+ return false;
+ } else if (ref == null)
+ return false;
+ else if (!ud.ref.equals(ref))
+ return false;
+
+ if (ud.path==null) {
+ if (path !=null)
+ return false;
+ } else if (path == null)
+ return false;
+ else if (!ud.path.equals(path))
+ return false;
+
+ return true;
}
-
+
public boolean complete() {
try {
URL url = buildURL();
@@ -207,6 +266,22 @@
data = parseURL(urlStr);
}
+ public String toString() {
+ return data.toString();
+ }
+
+ public boolean equals(Object obj) {
+ if (obj == null) return false;
+ if (! (obj instanceof ParsedURL))
+ return false;
+ ParsedURL purl = (ParsedURL)obj;
+ return data.equals(purl.data);
+ }
+
+ public int hashCode() {
+ return data.hashCode();
+ }
+
public boolean complete() {
return data.complete();
}
@@ -232,10 +307,6 @@
public String getPortStr() {
return data.getPortStr();
- }
-
- public String toString() {
- return data.toString();
}
public InputStream openStream() throws IOException {
1.2 +62 -1 xml-batik/sources/org/apache/batik/util/SoftReferenceCache.java
Index: SoftReferenceCache.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/util/SoftReferenceCache.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SoftReferenceCache.java 2001/02/15 02:27:29 1.1
+++ SoftReferenceCache.java 2001/04/25 14:35:39 1.2
@@ -11,6 +11,8 @@
import java.util.Map;
import java.util.HashMap;
+import java.lang.ref.Reference;
+import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
/**
@@ -149,9 +151,68 @@
*/
protected final synchronized void putImpl(Object key, Object object) {
if (map.containsKey(key)) {
- SoftReference ref = new SoftReference(object);
+ SoftReference ref = new SoftReference(object, queue);
map.put(key, ref);
+ synchronized (refMap) {
+ refMap.put(ref, new Info(key, this));
+ }
this.notifyAll();
}
}
+
+ static class Info {
+ Object key;
+ SoftReference cacheRef;
+ public Info(Object key,
+ SoftReferenceCache cache) {
+ this.key = key;
+ this.cacheRef = new SoftReference(cache);
+ }
+
+ public Object getKey() { return key; }
+
+ public SoftReferenceCache getCache() {
+ return (SoftReferenceCache)cacheRef.get();
+ }
+ }
+
+ private static HashMap refMap = new HashMap();
+ private static ReferenceQueue queue = new ReferenceQueue();
+ private static Thread cleanup;
+
+ static {
+ cleanup = new Thread() {
+ public void run() {
+ while(true) {
+ Reference ref;
+ try {
+ ref = queue.remove();
+ } catch (InterruptedException ie) {
+ continue;
+ }
+
+ Object o;
+ synchronized (refMap) {
+ o = refMap.remove(ref);
+ }
+
+ // System.out.println("Cleaning: " + o);
+ if (o == null) continue;
+ Info info = (Info)o;
+ SoftReferenceCache cache = info.getCache();
+ if (cache == null) continue;
+ synchronized (cache) {
+ o = cache.map.remove(info.getKey());
+ if (ref != o)
+ // Must not have been ours put it back...
+ // Can happen if a clear is done.
+ cache.map.put(info.getKey(), o);
+ }
+ }
+ }
+ };
+ cleanup.start();
+ }
+
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org