FiniteSizedCache.java
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.pdfg.common;
import com.adobe.pdfg.common.IObjectRetriever;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class FiniteSizedCache {
private int size;
private IObjectRetriever retriever;
private int nEntries;
private int minUsageCount;
private Map cache;
private Object minUsageKey;
public FiniteSizedCache(int size, IObjectRetriever retriever) {
this.size = size;
this.retriever = retriever;
this.nEntries = 0;
this.minUsageCount = Integer.MAX_VALUE;
this.cache = new HashMap(this.size);
this.minUsageKey = null;
}
public Object retrieve(Object key) throws Exception {
Object value;
value = null;
CacheEntry entry = (CacheEntry)this.cache.get(key);
if (entry != null) {
value = entry.value;
++entry.usageCount;
if (key.equals(this.minUsageKey)) {
++this.minUsageCount;
for (Map.Entry mapEntry : this.cache.entrySet()) {
Object keyInCache = mapEntry.getKey();
CacheEntry entryInCache = (CacheEntry)mapEntry.getValue();
if (entryInCache.usageCount >= this.minUsageCount) continue;
this.minUsageCount = entryInCache.usageCount;
this.minUsageKey = keyInCache;
break;
}
}
} else {
value = this.retriever.retrieve(key);
if (this.size == this.nEntries) {
this.cache.remove(this.minUsageKey);
--this.nEntries;
this.minUsageKey = null;
}
entry = new CacheEntry();
entry.usageCount = 1;
entry.value = value;
this.cache.put(key, entry);
++this.nEntries;
if (this.minUsageCount >= 1 || this.minUsageKey == null) {
this.minUsageCount = 1;
this.minUsageKey = key;
}
}
return this.cloneObject(value);
}
private Object cloneObject(Object object) throws Exception {
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(outBytes);
out.writeObject(object);
byte[] serializedObject = outBytes.toByteArray();
ByteArrayInputStream inBytes = new ByteArrayInputStream(serializedObject);
ObjectInputStream in = new ObjectInputStream(inBytes);
return in.readObject();
}
private static class CacheEntry {
public int usageCount;
public Object value;
private CacheEntry() {
}
}
}