URLDNS
分析
URLDNS主要用到了两个数据结构:HashMap和URL至于为什么是这两个,我们看看他们俩各有什么特性吧~
HashMap
readObject-反序列化的入口
反序列化一个对象时,Java的
ObjectInputStream
会调用被反序列化对象的readObject
方法,以便读取对象的状态并恢复它的字段。在URLDNS中使用了HashMap.readObject()方法1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24// HashMap.readObject
private void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException {
// Read in the threshold (ignored), loadfactor, and any hidden stuff
s.defaultReadObject();
reinitialize();
if (loadFactor <= 0 || Float.isNaN(loadFactor))throw new InvalidObjectException("Illegal load factor: " + loadFactor);
s.readInt(); // Read and ignore number of buckets
int mappings = s.readInt(); // Read number of mappings (size)
if (mappings < 0) throw new InvalidObjectException("Illegal mappings count: " +mappings);
else if (mappings > 0) { // (if zero, use defaults)
...
// Read the keys and values, and put the mappings in the HashMap
for (int i = 0; i < mappings; i++) {
K key = (K) s.readObject();
V value = (V) s.readObject();
putVal(hash(key), key, value, false, false); //<-------flag: 在这里会调用hash(key)----------
}
}
}跟进putVal(hash(key), key, value, false, false); 方法
1
2
3
4
5//HashMap.hash
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);//<-------flag: 如果key不为空,则调用key.hashCode()方法
}
到这里我们可以看到 HashMap在执行反序列化过程中会循环调用key的hashCode()方法,